struts2 <s:iterator> with <s:param> not working - struts2

I am displaying the list values with tag.At the same time i want to provide a hyperlink for the displaying values to provide action for the displaying value.I am using <s:param> for that.but the values are not passing.I am writing like below
<s:iterator status="stat" value="transactionList">
<s:url id="open" action="openTransaction">
<s:param name="transactionCode" value="<s:property value='monthName'/>"/>
</s:url>
<tr class="gradeB">
<td>
<s:a href="%{open}"><s:property value='transactionCode'/></s:a>
</td>
<td><s:property value="monthName"/></td>
<td><s:property value="transactionDesc"/></td>
</tr>
</s:iterator>
Now the transactionCode property is displaying with hyperlinks and by clicking on that the action is forwarding to openTransaction method.but the value i passed with <s:param> is not going,it is giving null. In iteration for that particular transaction code i want to pass that particular transaction code only. In struts 1.x I used display tag table decorator for this purpose, it takes the current row object.Here also i want to pass the current row values to the action. Please help me.

If you want to use <s:property> tag to put a param inside an url you have to do it like that:
<s:url ...>
<s:param name="foo"><s:property value="bar"/></s:param>
</s:url>
The documentation of <s:param> explains the difference between using this way of putting a param and your way.
Note: When you declare the param tag, the value can be defined in either a value attribute or as text between the start and end tag. Struts behaves a bit different according to these two situations. This is best illustrated using an example:
<param name="color">blue</param> <-- (A) -->
<param name="color" value="blue"/> <-- (B) -->
In the first situation (A) the value would be evaluated to the stack as a java.lang.String object. And in situation (B) the value would be evaluated to the stack as a java.lang.Object object.
For more information see WW-808.
Edit: Also remember that if you are using 2.1.x or higher, the id attribute of <s:url> is deprecated and it has been replaced with var. You can read it here.

<c:url var="search" value="/image/search.action">
<c:forEach items="${filtersMap}" var="map">
<c:param name="filtersMap.${map.key}" value="${map.value}"/>
</c:forEach>
${filtersMap} is a map param from action

Related

Struts2 Iterate value from a textfield

I am trying to iterate over the textfield's value, for eg. 5. Is there a way to get the value?
I have a textfield with value 5:
<s:textfield
theme="simple"
cssClass="form-control"
name="instCount"
value="5"
style="width:25%;"
onkeyup="javascript:isNumber(this);"
/>
I need to iterate the value 5:
<s:if test='<s:property value="%{instCount}"/> > 0'>
<s:iterator value='<s:property value="%{instCount}"/>' var="count" status="countStatus">
</s:iterator>
</s:if>
You cannot nest JSP tags like that, and it appears that you haven't wrapped your head around OGNL or ELs in general, even though you use it correctly in the <s:property> tag.
Let's take a step back: what is this doing?
<s:property value="%{instCount}" />
It's referring to an action property named instCount.
How? Via the OGNL expression %{instCount}.
How does the <s:if> tag work? By evaluating an OGNL expression in the test property. Ah, OGNL expression, which we've already seen.
<s:if test='instCount > 0'>
How does the <s:iterator> tag work? By evaluating an OGNL expression in the value tag.
<s:iterator value='%{instCount}' etc...>
I would highly recommend taking some time to figure out the framework you're working in, even a minor reading of the documentation (and a basic understanding of how JSP works) will be highly beneficial, and avoid questions like this.

Struts2 use <s:select> with <s:iterator>

Is it possible to define a Struts Select field, <s:select>, with an Iterator for options, <s:iterator>?
e.g., I don't want to use the Key/Value/List properties,
<s:select id="criteriaRequestStatusList" name="searchRequestCriteria.requestStatusList"
list="requestStatuses"
listValue="description" listKey="id" />
because I have some special symbols such as coming from the server side and they aren't escaped.
The following works regarding escaping , but it's an HTML Select. I don't want to use this either, because it doesn't populate the form values properly on load.
<select id="criteriaRequestStatusList" name="searchRequestCriteria.requestStatusList" class="requestor input-block-level" name="requestors" multiple="multiple">
<s:iterator value="requestStatuses">
<option value="${id}">${description}</option>
</s:iterator>
</select>
My goal is,
<s:select ..>
<s:iterator>
You can set the value on the POHTML select using the normal value attribute.
The <s:select> tag expects values to be ready-to-use; personally I'd transform them on the server side before exposing them to the view layer.
You might be able to use OGNL in the listTitle property (I might have the property name wrong) to un-entity it; I don't recall.

How do you compare 2 struts param in a struts if

I'm am still pretty new to struts and am having trouble trying to compare two struts params in a struts if statement. I am trying find if the current year param is equal to the checkYear param
If they are equal I want to execute some code. If they are not equal then I would like to execute some other code..
Sample code:
<s:param name="currentYear">
<s:date name="%{new java.util.Date()}" format="yyyy" />
</s:param>
<s:param name="checkYear">
<s:date format="yyyy" name="#year"/>
</s:param>
<s:if test="${checkYear == legendYear}">
code executed
</s:if>
I don't really understand the purpose of the '$','%', or '{}' in the test part of the if statement if or how I need to apply it to have it check the params.
Thank you in advance!
Inside Struts2 tags, you use OGNL. Refer to the small language guide to see how it works.
In OGNL, %{} means you want to force the evaluation of an expression; it is needed somewhere, while it is superfluos somewhere else. In the <s:if/>, it is not needed, because test="" will evaluate its content by default.
${} does not exists in OGNL, it is JSP EL. Search on StackOverflow for some nice answer on that if you are curious.
{} alone in OGNL is List Projection... you probably don't need it now.

Displaytag does not recognize struts element

I'm trying to pass column names as action parameter and use it inside of display tag.
the display:column does not grab any value in value stack, but just print whatever is in title attribute as plain string.
With following code:
<display:table name="pList" uid="chartRow" id="sTable" pagesize="10" class="table" requestURI="/some.action">
<s:iterator value="columns" var="column" status="cStatus">
<display:column title="${column}">
<s:property value="#attr.sTable[#column]" />
</display:column>
</s:iterator>
</display:table>
I get ${column} as column names for whole table.
Any suggestion?
sturts-2.2
displaytag-1.2
After much wasting of my time with jstl, el all that.
<%# page isELIgnored="false" %>
resolved my issue.

Creating navigation links in Struts 2

Is there some way to replicate rails' "link-to-unless-current"?
Ie. if i have a list of links in my navigation, and I want to mark the current page with a different css style (or even have it be plain text, not a link)?
Now I just made a method in my action-class (getPage()) which returns a name that I assign for each action, and test for that when building my navigation... Works but not pretty, and I have to manually set the page name (couldn't struts somehow automaticalyy get it from the context).
Using Struts 2, Tiles, JSP + Struts taglibs.
To get the current url you you can use the Url tag without any params, actions, etc:
<s:url var="currenturl" includeParams="get" escapeAmp="false"/>
Then assuming you construct your navigation Url something like:
<s:url var="url" action="someAction" escapeAmp="false">
<s:param name="id" value="id"/>
</s:url>
Then you can then use a normal if tag to test whether the current url matches on that you've constructed.
<s:if test="#url eq #currenturl">
</s:if>
For example, to change the class, you can do a conditional inline to your anchor definition:
class="current"</s:if>>XXX
Use the includeParams="get" if the parameters in your URL are meaningful for navigation otherwise exclude that and use an if like:
<s:if test="#url.startsWith(#currenturl)">

Resources