Struts2 Iterate value from a textfield - struts2

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.

Related

struts 2 Undefined attribute name "var"

I am trying to iterate over two 2D arrays Org_Positions_IdTitle and Org_Apps and print out a field but I keep getting the Attribute var invalid for tag iterator according to TLD also in my jsp page the var1 and var2 are underlined and it says on the left Undefined attribute name "var".
I would be so thankful if you can help me with that.
<s:iterator value="Org_Positions_IdTitle" var="arr1" >
<s:iterator value="Org_Apps" var="arr2" >
<s:if test="#arr1[0] == #arr2[1] ">
<s:property value="#arr1[1]" />
</s:if>
</s:iterator>
</s:iterator>
Which version of Struts 2 do you use?
If your version less then 2.1.x you should use id attribute, according to documentation: http://struts.apache.org/release/2.1.x/docs/iterator.html

struts2 s:select value ognl expression

In Struts2 tutorial for s:select tag I've seen:
<s:select label="Pets"
name="petIds"
list="petDao.pets"
listKey="id"
listValue="name"
multiple="true"
size="3"
required="true"
value="%{petDao.pets.{id}}"
/> ^ ^
and my question: why value="%{petDao.pets.{id}}"? why not simply value="%{petDao.pets.id}"? what do those trailing curly braces mean?
This is an OGNL list projection to get all the id values as a list from petDao.pets, meaning all values in this <s:select> will be pre-selected.
It isn't necessary; I suspect it was the result of an error in the tag's source file.
It works with it, but isn't needed, will fail IDE validation (if the IDE supports S2 and/or OGNL, e.g., IntelliJ), and I've made a note to update.
The principal reason is because %{} syntax is used to force OGNL evaluation where Struts would otherwise be treating the value as a String literal.
For example,
<s:property value="name" />
will look for a name property in the value stack i.e. is a value retrieved by calling getName().
If you wanted to force it to use literal value "name", you will need to use the %{} syntax -
<s:property value="%{'name'}" />
Source:
http://www.coderanch.com/t/420711/Struts/Struts

Removing single quotes in struts2

I have the following code in my JSP.
<td style="text-align:center;"><s:property value="accountCode"/></td>
when i get the values from the back-end, i am passing it with the single quotes(eg. '1234'). However, when i display it, i want to remove these quotes and display just the number.(eg.1234). How do i do this?
I tried <s:property value="accountCode.replace('\'','')"/> and <s:property value="accountCode.replaceAll('\'','')"/>, but it does not work!(Did not show me the value itself!)
If using replaceAll method is OK, then
<s:property value="accountCode.replaceAll('\\'', '')" />.
Use the escapeJavaScript attribute of the tag:
<s:property value="accountCode" escapeJavaScript="true"/>
EDIT:
Use this:
<s:property value="accountCode.replaceAll('\'','')" />

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

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

What's the Strut's 2 equivalent of the Struts 1 logic:empty tag?

What's the Strut's 2 equivalent of the Struts 1 logic:empty tag?
<logic:empty name="foo">
Foo is null or the empty string
</logic:empty>
Thanks.
There is no struts2 tag to do this, there are more possibilities and more expressiveness with OGNL than the struts1 tags, however there does not seem to be a way to check a string for both null and the empty string as succinctly.
The following works:
<s:if test="(myString == null || myString.equals(''))">
myString is blank or null
</s:if>
<s:else>
The value is <s:property value="myString"/>
</s:else>
The test relies on short circuting, so test of null can not be changed with the test for equality.
If the need to test for this comes up often there may be a design issue. With proper validation in place you should not have uninitialized objects for which the view depends but I suppose there are always exceptions.
To add to Quaternion's answer:
You can always add a method in your action for checking particular conditions, eg MyAction.isPropertyXEmpty() an put it in the <if test=...> condition
Recall that in Struts2 properties are more type-rich/expressive than in Struts. Don't use Strings if another type is more appropiate. And you can initialize them to non-null values (eg., empty strings) to avoid the null problems.
To expand on Steven's comment, you can import with
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
then use:
<c:if test="${empty myStruts2Var}">
or
<c:if test="${not empty myStruts2Var}">
For strings, I would use:
<s:if test="myString in {null, ''}">
For collections, I would use:
<s:if test="null == myCollection || myCollection.empty">
Hope this is beneficial for readability.

Resources