首页 文章

如何将bean作为参数传递给JSP标记?

提问于
浏览
1

我've created a custom JSP tag that is supposed to accept a list of products to render, but I'我无法弄清楚如何将列表传递给标签 . 产品列表作为页面范围的bean存在 . Web应用程序使用Struts taglib在Struts 1.2.x中编写 .

这是我的代码的简化版本:

renderProducts.tag

<%@ tag language="java" pageEncoding="ISO-8859-1" body-content="empty" %>
<%@ attribute name="products" required="false" type="ProductIf[]" %>
<logic:iterate id="product" name="${products}" type="ProductIf">
    <!-- Render the product -->
</logic:iterate>

ProductDetail.jsp

<bean:define id="relatedProducts" name="productMgr" property="relatedProducts />
<my:renderProducts products="${relatedProducts}" />

但是,当我查看页面时,我得到一个例外:

无法将字符串“$ ”转换为类“[Lcom.foo.ProductIf;”属性“products”:Property Editor未向PropertyEditorManager注册

所以似乎 ${} 语法不是我需要做的,因为无论如何它被解释为文字字符串 . 我也试过传递没有 ${} 的bean的名称,结果相同 . 什么是正确的语法?

(注意:请原谅我,如果这个问题 Build 在糟糕假设的基础之上,但我是Java EE开发的新手,而且还有相当多的笨拙的蠢货 . )

1 回答

  • 1

    阅读http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html

    要取消激活EL表达式的评估,请指定page指令的isELIgnored属性:<%@ page isELIgnored =“true | false”%>
    此属性的有效值为true和false . 如果为true,则EL表达式在静态文本或标记属性中出现时将被忽略 . 如果为false,则由容器评估EL表达式 . 默认值因Web应用程序部署描述符的版本而异 . 使用Servlet 2.3或更早版本描述符传递的JSP页面的默认模式是忽略EL表达式;这提供了向后兼容性 . 使用Servlet 2.4描述符传递的JSP页面的默认模式是评估EL表达式;这会自动提供大多数应用程序所需的默认值 .

    (强调我的)

    因此,要么切换到servlet 2.4或更高的描述符,如果容器支持它,要么使用 <%@ page isELIgnored = "false" %> 激活EL .

相关问题