posted 10 years ago
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.
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.
posted 10 years ago
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).
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).
posted 10 years ago
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?
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
Sam Gehouse
Ranch Hand
Posts: 281
posted 10 years ago
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,
