Search code examples
jspjakarta-eestrutsjsp-tags

How do I pass a bean to a JSP tag as a parameter?


I've created a custom JSP tag that is supposed to accept a list of products to render, but I'm having trouble figuring out how to pass the list to the tag. The product list exists as a page-scoped bean. The web app is written in Struts 1.2.x using the Struts taglib.

Here's a simplified version of my code:

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}" />

However, when I view the page, I get an exception:

Unable to convert string "${relatedProducts}" to class "[Lcom.foo.ProductIf;" for attribute "products": Property Editor not registered with the PropertyEditorManager

So it seems the ${} syntax is not what I need to be doing, since it is being interpreted as a literal string anyway. I've also tried passing in the name of the bean without the ${} with the same result. What is the proper syntax?

(Note: Please forgive me if this question is built on a shaky foundation of bad assumptions, but I'm new to Java EE development and there's a fair amount of fumbling-in-the-dark going on.)


Solution

  • Read http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html:

    To deactivate the evaluation of EL expressions, you specify the isELIgnored attribute of the page directive:

    <%@ page isELIgnored ="true|false" %> 
    

    The valid values of this attribute are true and false. If it is true, EL expressions are ignored when they appear in static text or tag attributes. If it is false, EL expressions are evaluated by the container.

    The default value varies depending on the version of the web application deployment descriptor. The default mode for JSP pages delivered using a Servlet 2.3 or earlier descriptor is to ignore EL expressions; this provides backward compatibility. The default mode for JSP pages delivered with a Servlet 2.4 descriptor is to evaluate EL expressions; this automatically provides the default that most applications want.

    (emphasis mine)

    So, either switch to a servlet 2.4 or higner descriptor, if your container supports it, or activate EL using <%@ page isELIgnored = "false" %>.