tags:

views:

672

answers:

2

I have the following code in a jsp page (Struts backed):

<c:if test="${USERINFO.someproperty == 'test'}">
...
</c:if>

But what I would like to do is compare only a substring of someproperty, like e.g. if the suffix ends with "st". I am using JSTL 1.0 so the endsWith() function from JSTL 1.1 is not available (as far as I know) and furthermore, I would prefer a solution that doesn't involve me writing a custom Java class for this.

I was thinking something along these lines:

<c:set var="someproperty"><%= USERINFO.someproperty.substring(USERINFO.someproperty.length()-2, 2) %></c:set>
<c:if test="${someproperty == 'st'}">
...
</c:if>

But of course USERINFO is not accessible to Java like that.

Any ideas?

A: 

Try the following:

<c:set var="someproperty"><%= ((UserInfo)pageContext.getAttribute("USERINFO")).someproperty.substring(USERINFO.someproperty.length()-2, 2) %></c:set>
<c:if test="${someproperty == 'st'}">
...
</c:if>
Nick Holt
+1  A: 

Or just use the String Taglib if you want to avoid scriptlets: http://jakarta.apache.org/taglibs/doc/string-doc/intro.html

<c:set var="someproperty">
    <str:substring start="${USERINFO.someproperty.length-2}" end="2">The tree is green.</str:substring>
</c:set>

<c:if test="${someproperty == 'st'}">
    ...
</c:if>
raq
You must avoid scriptlets. String Taglib is the way to go. +1
Adeel Ansari
The string taglib worked, but I used the str:right tag instead of substring, since I only wanted the suffix.Thanks! :)
AsGoodAsItGets