how to access a struts2 property value in jsp? - java

<s:iterator value="MyList" status="idx">
<% int index = %><s:property value = '%{#idx.index}'/><% ; %>
</s:iterator>
The above code does not work.
how can the integer value of the 'idx' be stored in the variable 'index'?

Instead of using java variables creating a struts variable works fine.
<s:set name="newVariable" value="%{#idx.index}" />
whenever you need to access the newly created struts variable. You can use this
<s:property value="#newVariable" />

you can access the list value in jsp using
<s:property value="dataname"/>
dataname is your pojo generated database columns

Related

How to retrieve dynamically generated name tag for s:set for Struts2?

How can I retrieve a dynamically generated name tag for <s:set> on Struts 2?
E.g.:
In my action class I have an object (currency) that have this fields: 'id' and 'symbol'.
So in my view page I can use this:
<s:iterator value="currency">
<s:set name="var_%{id}" value="symbol"/>
<s:set name="total_%{id}" value="%{0.0}"/>
</s:iterator>
So I can use this to create these PageContext variables like "var_BRL", "var_USD", "var_MXN" and "total_BRL", "total_USD", "total_MXN" etc. If I write the following code:
<s:property value="#var_USD"/> = <s:property value="#total_USD"/>
I'm able to retrive a result like:
USD = 0.0
I'm using these #total_XXX variables to sum some values under some conditions presented by another iterator, in a way that I'll have at the end of this other iteraction, a result of the total spent in every currency (BRL, USD, MXN etc).
But when I try to retrieve these values dynamically, nothing is rendered. Following is the code I'm using to retrieve the values from these variables, at the end of my page. I don't understand OGNL very well, so I've tryed different arrangements like the ones below and had no success with any of them:
<s:iterator value="currency">
<s:property value="#var_%{id}"/> = <s:property value="#total_%{id}"/>
<s:property value="#%{var_%{id}}"/> = <s:property value="#%{total_%{id}}"/>
<s:property value="%{#var_%{id}}"/> = <s:property value="%{#total_%{id}}"/>
</s:iterator>
Is there a solution to retrieve these values in the PageContext? Or can I only solve this inside my Action? I've searched so many posts but I couldn't find anything.
Thank you!
LZ
discussing with a friend, I found the proper OGNL expression to call for the dynamically created variables names at the end of my code:
<s:iterator value="currency">
<s:property value="#attr['var_'+id]"/> = <s:property value="#attr['total_'+id]"/><br>
</s:iterator>

passing string array as hidden value from one jsp to another jsp

I am trying to pass String array from one jsp to another. I am using JSTL in my JSP.
In my first JSP i am doing like this
<c:if test="${fn:length(empBean.additionalEmailAddr) gt 0}">
<c:forEach begin="0" end="${fn:length(empBean.additionalEmailAddr) - 1}" var="ind" >
<input type="hidden" name="inbdAdditionalEmailAddr" value="${empBean.additionalEmailAddr[ind]}"/>
</c:forEach>
</c:if>
and trying to access the values in another jsp as follows
<%
String[] inbdAddEmlAddr = request.getParameter("inbdAdditionalEmailAddr");
%>
and i am planning to use JSTL to print the array values.
In the second jsp i am getting type mismatch error. Please help.
Is this the right approach ? Any help is appreciated
Thanks
request.getParameter() returns a String which the code attempts to assign to a String[], causing the exception.
Use request.getParameterValues('inbdAdditionalEmailAddr'); to retrieve parameters as an array.
See the documentation.

How to retrieve the value from struts url tag and pass it to servlet

Hi I have a jsp with following code. The file has struts tag lib
<s:iterator value="TpData.vhp.actvh" var= "veh" status = "hellostatus">
<tr>
<s:url var = "ShowInfoAction" value="%{urlInfo.actionurl}" >
<s:param name = "hereInfo" value="#someclass.method"/>
<s:param name ="pincode" value="againsomeclass.pincode"/>
</s:url>
</tr>
</s:iterator>
The param name ="hereInfo" has unique values. Since this is in iterator the url constructed will have different parameters. As this url is constructed in the jsp i want it to be passed on to action class. Is this possible?
If you want a collection of similarly-named properties to be added to a map, use mapped param names, but the index is typed (e.g., Integer, String, etc.)
For example (from memory, but close):
<s:param name="hereInfo[%{hellostatus.index}]" value="#someclass.method"/>
Or a business-model ID etc., which would be more common.

How to access ValueStack objects within Struts iterator?

I have following code:
<s:iterator value="reviews">
<img src="<s:property value="#request.restaurant.portalImage.url" />" />
<s:property value="user.firstName" />
<s:property value="user.lastName" />
<s:property value="rating" />
<s:property value="review" />
</s:iterator>
reviews is a list of review objects which contain details of a review, such as rating and name of user.
My problem is that I'm not able to access any of the objects present on the ValueStack within the loop.
Outside the loop <s:property value="#request.restaurant.portalImage.url" /> works correctly. But within the loop it prints null.
AFAIK an iterator pushes it's collection on the ValueStack so that all OGNL expressions resolve against it. But I've used # which means I'm explicitly specifying the root object for resolution.
Why is it still not working?
I just spent my whole afternoon fighting against a similar issue.
In my case, the issue was that my iterator variable (In your case reviews) had a field with the same name as the outer variable.
No matter how hard I tried to break out of the local stack of the iterator, it would not access the outer variable.
If your reviews iterator has a field called restaurant its going to override the outer restaurant variable, no matter how hard you try :(
The solution I found was to simply rename the outer variable. (eg: theRestaurant)
Once I renamed it, it was no longer clashing with the inner variable, and I was able to access it from within the iterator.
I am not sure what object is contained in the collection you are using in the Iterator.As per the iterator when you iterate the collection using the S2 iterator tag it will push the object on the top of value stack, which means, say you have collection of user object on which you are iterating like
<s:iterator value="userCollection">
</s:iterator>
so when this will iterate, S2 iterator will push the user object on top of value stack and if we want to refer to the name property of user object, all we need to refer to the name property
<s:property name="userName"/>
since OGNL will try to resolve userName in the value stack and we already have user object as top level object in value stack.I suggest to go through the Iterator tag documentation and how it push values
S2 Iterator tag
You should change it like below:
<s:iterator value="reviews" var="varrequest">
<img src="<s:property value="#varrequest.restaurant.portalImage.url" />" />
<s:property value="#varrequest.user.firstName" />
<s:property value="#varrequest.user.lastName" />
<s:property value="#varrequest.rating" />
<s:property value="#varrequest.review" />
</s:iterator>
Using a iterator reference variable to fetch the unique index object.
I think it resolves your issue
s:iterator value="reviews" status="position">
<img src="<s:property value="#request.restaurant.portalImage.url"/>"/>
<s:property value="#position.user.firstName"/>
<s:property value="#position.user.lastName"/>
<s:property value="#position.rating"/>
<s:property value="#position.review"/>
</s:iterator>
your requirement is little confusing

inconsistent behavior while accessing session variables in jsp

My web server - tomcat sets two attributes in the session. In the jsp page, am retrieving them as
<% String age = (String) session.getAttribute("age"); %>
Am setting this value to a hidden field in the form in the same jsp file.
<input type="hidden" name="age" id="age" value="<%=age%>" />
And am trying to use this value in javascript file as below
document.cForm.age.value
But this value is null for most of the times. Sometimes, the variable to set to proper value? Is there any reason for this inconsistency? Please explain
Try to get the value on DOM load not inline. i mean inside a $(document).ready(function() {})

Categories