Failed to compare strings in jsp - struts2

I have a url that looks like this
....aaa=bbb&tab=second
in jsp I compare:
<s:if test="%{#parameters['tab']=='second'}">
it returns false....
I can see the value 'second' from here:
<s:property value="#parameters['tab']"/>
but this displays false:
<s:property value="%{#parameters['tab']=='second'}"/>
Any idea why it does that?
Thank you,
Yuri

parameters returns string array not string. Yes, a named parameters might have multiple values like tab=first&tab=second that's why it's string array instead of string.
<s:if test="%{#parameters['tab'][0]=='second'}">
</s:if>

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.

How to get a href parameter value

How to get val value?
This is my index.jsp:
Admin
Member
<s:set var="v" value="#attr.val"></s:set>
<s:property value="#v"/>
You can get parameters by using special keyword named... wait for that... #parameters.
<s:property value="#parameters.val"/>

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('\'','')" />

struts 2 if to test value of iterator

Am using struts 2 iterator and if tag can anyone tell me how to test a particular value of iterator
Example: I got a List of type String with values
StrList = {"start","hello","hi","name","start","value",.."start",..}
I want to test for start if start is found i want to display HI and else Hello.
<s:if StrList.contains("start")>
Display HI
</s:if>
<s:else>
Display HELLO
</s:else>
Untested please see S2 web site tag lib documentation. Also assumed StrList was a public field, otherwise it should be called strList and have an appropriate getter/setter (as that would be following Java conventions).
Edit: I didn't read the question well enough... Use the var attribute of the iterator to give each iteration of the iterator a convinent handle, like so:
<s:iterator value='{"start","something","start","something else","start"}' var="curStr">
<s:if curStr.compareTo("start")> //or perhaps compareToIngnoreCase()
Display HI
</s:if>
<s:else>
Display HELLO
</s:else>
</s:iterator>

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