File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSTL 1.1 - Get value from method of an Obj and set it to a variable

 
Sam Gehouse
Ranch Hand
Posts: 281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I get the value from the method of an Object (e.g., length of a String by calling myString.length()) and set the value of the length to a variable?

In the code below, my objective is to set myVar with the length() value obtained from myString of myForm object.

Code is somewhat like:

<c:choose>
<c:when test="${someCondition}">
<c:set var="${myVar}" value="${myForm.myString}"></c:set>
</c:when>
<!-- More <c:when> conditions -->
</c:choose>

myForm is an object which has myString as attribute with corresponding public getter and setter methods.

I get syntax error if "${myForm.myString}" is replaced with value="${myForm.myString.length}

I want to get the length of myString and set that value to myVar for future use in JSP.

Any help will be appreciated.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot call general functions like length() for any bean with the EL. By very explicit design, the EL is limited to only accessing bean-patterned properties.

Howver, since obtaining the length of something is a common need, the JSTL defines an EL function just for that purpose:



If someValue is a String, its length is returned. (It could also be an array or other collection).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a work-around if I cannot use fn? I cannot use fn:length() as I am advised because of the level of JSp support
Any help will be appreciated.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. If you are using JSP 1.2 and JSTL 1.0 there is no way to obtain the length of the string using the JSTL an EL.

You will either need to obtain the length in the servlet controller and pass it as a scoped variable to the JSP, or resort to polluting your page with scriptlets.

At this point in time, JSP 2.0 has been out long enough that there are very few valid reasons to still be running an old version. What container are you using and why is it not JSP 2.0 capable?
 
Sam Gehouse
Ranch Hand
Posts: 281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WAS 5.x that uses JDK 1.3 that supports JSP 1.2 (not 2.0). As such, I am limited to JSTL 1.0.
 
Sam Gehouse
Ranch Hand
Posts: 281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using JSTl 1.0, I want to do something like:

<c:forEach var="myVar" items="${myList}" varStatus="status">
<c:set var="len" value=""></c:set>
</c:forEach>

myVar.myString will get me a String. I want to populate len with the length of the myString String.
 
Remko Strating
Ranch Hand
Posts: 893
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create a getter which returns the length of the String myString within the bean


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also write your own custom tag to do so, but if your bean lends itself to a new length property, that's not a bad way to go.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic