aspose file tools*
The moose likes JSP and the fly likes JSTL 1.1 - Get value from method of an Obj and set it to a variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSTL 1.1 - Get value from method of an Obj and set it to a variable" Watch "JSTL 1.1 - Get value from method of an Obj and set it to a variable" New topic
Author

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

Sam Gehouse
Ranch Hand

Joined: Jul 21, 2003
Posts: 281
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

Joined: Jan 10, 2002
Posts: 61652
    
  67

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).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61652
    
  67

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

Joined: Jan 10, 2002
Posts: 61652
    
  67

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

Joined: Jul 21, 2003
Posts: 281
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

Joined: Jul 21, 2003
Posts: 281
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

Joined: Dec 28, 2006
Posts: 893
You could create a getter which returns the length of the String myString within the bean




Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61652
    
  67

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.
 
wood burning stoves
 
subject: JSTL 1.1 - Get value from method of an Obj and set it to a variable