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.

[2400 byte] By [tadchristiansena] at [2007-11-27 8:10:29]
# 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>

evnafetsa at 2007-7-12 19:54:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks for the reponse! I had thought about doing one of your first two solutions, I just thought that was a little hacky. Any ideas on why method calls aren't allowed? You final solution won't work for my situation but thank for your help! I will probably do the getToggle solution and hope that some day methods will be allowed.

tadchristiansena at 2007-7-12 19:54:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Maybe my question was missed....so why can't we call methods from inside tags? It seems like there are a lot of cases where you would want to do that. Does anyone know why they are not allowed? Will they be included in the future?
tadchristiansena at 2007-7-12 19:54:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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.

evnafetsa at 2007-7-12 19:54:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Thanks for the info. So why does JSTL only allow you to call getter/setter methods? Is it because of a principle that is trying to be followed or is there a chance the functionality will be there in the future?
tadchristiansena at 2007-7-12 19:54:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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.

evnafetsa at 2007-7-12 19:54:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Ok, they makes more sense to me. Thanks for taking the time to answer my questions!
tadchristiansena at 2007-7-12 19:54:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...