how to use jstl variable in a jsp page
Hi all,
I am new to JSTL and i want to access the value of jstl in jsp.
when i use the tag like below it is displaying the value ""
<c:out value="${ack}"/>
But as ack is string, i want to convert it into int. and i have written like
<c:set var="ackvalue" value="${ack}"/>
and int ack = Integer.parseInt(ackvalue);
But it is showing error like varialbe can not be resolved: ackvalue
can anybody please help me regarding this?
Waiting for your warm response.
Thanks in advance
JSTL doesnt work with scripting variables (variables in scriptlets).
JSTL sets/gets variables in scope(page,request,session, application).
Thus
<%
int ack = Integer.parseInt(ackvalue);
pageContext.setAttribute("ackValue", new Integer(ack)); //set the Ineteger object in page scope
%>
<c:out value = "${ackValue}"/>
Seeing that you use the integer conversion for display purposes (ie in a c:out tag), it converts the Integer back to String. So you may as well have
<%pageContext.setAttribute("ackValue", ack);%>
<c:out value = "${ackValue}"/>
Does that help?
ram.
Instead you can use the <bean:define ../> tag in struts as below ..
<bean:define id="ackvalue" value="333"/>
<%
int ack = Integer.parseInt(ackvalue);
%>
The value is : <%=ack%>
Cheers
-Rohit
Hi,
Thanks for your immediate reply.
I am able to display the ackvalue when i am using
<c:set var="ackvalue" value="${ack}"/>
and <c:out value="${ackvalue}"/>
But my actual requirement is to use the ack value in switch statement. if i can assign the value to a String variable, then it si easy for me to proceed. Please help me in this regard.
Thanks,
> Hi,
> Thanks for your immediate reply.
> I am able to display the ackvalue when i am using
> <c:set var="ackvalue" value="${ack}"/>
> and <c:out value="${ackvalue}"/>
> But my actual requirement is to use the ack value in
> switch statement. if i can assign the value to a
> String variable, then it si easy for me to proceed.
> Please help me in this regard.
>
> Thanks,
You can use the c:choose, c:when and c:otherwise tags
<c:choose>
<c:when test = "${ackvalue == 1}">
//do stuff
</c:when>
<c:when test = "${ackvalue == 2}">
//do some other stuff
</c:when>
<c:otherwise>
//stuff
</c:otherwise>
</c:choose>
ram.