Calling methods from inside a tag using jsp 2.0
My searching has led me to believe that you cannot call methods from in the jsp 2.0 tags. Is this correct? Here is what I am trying to do.
I have an object that keeps track of various ui information (i.e. tab order, whether or not to stripe the current row and stuff like that). I am trying out the new (to me) jsp tag libraries like so:
jsp page:
<jsp:useBean id="rowState" class="com.mypackage.beans.RowState" />
<ivrow:string rowState="${rowState}" />
my string tag:
...
<%@ attribute type="com.mypackage.beans.RowState" name="rowState" required="true" %>
...
<c:choose>
<c:when test="${rowState.stripeToggle}">
<tr class="ivstripe">
</c:when>
<c:otherwise>
<tr>
</c:otherwise>
</c:choose>
I can access the getter method jst fine. It tells me wether or not to have a white row or a gray row. Now I want to toggle the state of my object but I get the following errors when I try this ${rowState.toggle()}:
org.apache.jasper.JasperException: /WEB-INF/tags/ivrow/string.tag(81,2) The function toggle must be used with a prefix when a default namespace is not specified
Question 1:
Searching on this I found some sites that seemed to say you can't call methods inside tag files...is this true?...how should I do this then? I tried pulling the object out of the pageContext like this:
<%@ page import="com.xactsites.iv.beans.*" %>
...
<%
RowState rowState = (RowState)pageContext.getAttribute("rowState");
%>
I get the following error for this:
Generated servlet error:
RowState cannot be resolved to a type
Question 2:
How come java can't find RowState. I seem to recall in my searching reading that page directives aren't allowed in tag files...is this true? How do I import files then?
I realized that these are probably newbie questions so please be kind, I am new to this and still learning. Any responses are welcome...including links to good resources for learning more.
# 1
You are correct in that JSTL can only call getter/setter methods, and can call methods outside of those. In particular no methods with parameters.
You could do a couple of things though some of them might be against the "rules"
1 - Whenever you call isStripeToggle() you could "toggle" it as a side effect. Not really a "clean" solution, but if documented that you call it only once for each iteration would work quite nicely I think.
2 - Write the "toggle" method following the getter/setter pattern so that you could invoke it from JSTL.
ie public String getToggle(){
toggle = !toggle;
return "";
}
Again its a bit of a hack, but you wouldn't be reusing the isStriptToggle() method in this way
The way I normally approach this is using a counter for the loop I am in.
Mostly when you are iterating on a JSP page you are using a <c:forEach> tag or similar which has a varStatus attribute.
<table>
<c:forEach var="row" items="${listOfThings}" varStatus="status">
<tr class="${status.index % 2 == 0 ? 'evenRow' : 'oddRow'}">
<td>${status.index}></td>
<td>${row.name}</td>
</tr>
</c:forEach>
# 4
Question 2:
How come java can't find RowState. I seem to recall in my searching reading that page directives aren't allowed in tag files...is this true? How do I import files then?
Sorry, I guess I mis-interpreted your question a little.
I haven't used tag files much.
You are correct, you can't use <%@ page %> directive in a tag file. Its not a JSP page.
you can use the <%@ tag %> directive which serves much the same purpose, and lets you import classes to use in scriptlet code.
<%@ tag import="com.mypackage.beans.RowState" %>
That should let you use scriptlet code in your tag file.
# 6
Yes JSTL/EL does that by design.
They did add the ability to call static functions, but you still can't invoke methods apart from getters/setters.
The point is that EL is NOT java, and is not a fully fledged programming language. JSTL/EL is intended to help write JSP pages for display.
If you are trying to call methods then you are probably doing something that doesn't need to be on a JSP page.