tags:

views:

383

answers:

1

I try to create a jsp tag file, which will call a helper class to print the input object. So I created a file /WEB-INF/tags/formatter.tag

<%@ tag import="package.Formatter"%>
<%@ attribute name="value" required="true" type="java.lang.Object" %>              
<%=Formatter.format(pageContext.getAttribute("value"))%>

So that I can call it in JSP like:

<t:formatter value="${obj}" />

But I found that it will not work inside a loop, e.g.

<c:forEach items="${list}" var="i">
   <t:formatter value="${i.property}"/>
</c:forEach>

I suspect that I should not get the attribute from pageContext. But I'm not sure. Any one know about this?

A: 

Try the following:

First, convert your formatter to be a standard Java bean (i.e. make the format method non static, have no arg constructor, etc.). The, change the tag to be:

<%@ tag import="package.Formatter" %>
<%@ attribute name="value" required="true" type="java.lang.Object" %> 
<jsp:useBean id="formatter" class="package.Formatter" />             
${formatter.format(value)}
David Rabinowitz
Still the same problem.
jackysee
Please try the new solution
David Rabinowitz
In my case, I need to pass two parameter. e.g. ${formatter.format(value,2)}. This results in JSP error: unable to parse EL function
jackysee