JSTL c:set and Struts s:set are undesirably formatting numbers - struts2

<c:set var="xmlDocumentId" value="${id}" scope="request" />
<s:set var="xmlDocumentId" value="%{id}" scope="request" />
are formatting id based on locale, setting xmlDocumentId to "12,345" while:
<c:out value="${id}" />
<s:property value="%{id}" />
are outputting "12345".
Any ideas how to break this behavior?

Because you are getting value with getText or <s:text> tag your long value gets formatted according to locale. To prevent this from happening convert your long to string.
With <s:set> tag you can call toString() method directly in value attribute.
<s:set var="xmlDocumentId" value="id.toString()" scope="request" />
For the concrete formatting algorithm take a look at java.text.MessageFormat class and its subformat method.

Once you know how to format numbers in Java, in Struts2 <s:property/> tag you can use getText() to format your number in the desired way, for example :
<s:property value="getText('{0,number,#,##0}',{id})"/>

Try this
<s:text name="id" > <s:param name="value" value="id"/> </s:text>

Related

How to pass a value of a column as a parameter from another column in displaytag using struts2

In the below code, I am trying to pass 2 parameters from <display:column> tag. I have to pass code and level properties to an action(as shown below). I am not able to pass value of another column as parameter from a column. Here I am not getting value of level property in code property.
<s:form action="levelHierarchy">
<display:table id="searchList" name="searchList" pagesize="8"
export="false" requestURI="/getComponentDetails" sort="list">
<display:column property="code" title="Code" sortable="true" paramId="levelId" href="levelHierarchy.action?level=${searchList.level}"></display:column>
<display:column property="description" title="Description" sortable="true" />
<display:column property="level" title="Level" sortable="true" />
<display:setProperty name="paging.banner.placement" value="bottom" />
</display:table>
</s:form>
Inside the form you don't have any fields to submit. You should define at least hidden fields to the columns that contains the values you want to pass to the action. You also need the use of uid attribute of the <display:table tag to access the row values.
<display:table uid="row" id="searchList" name="searchList" pagesize="8" export="false" requestURI="/getComponentDetails" sort="list" >
<display:column property="code" title="Code" sortable="true" paramId="levelId" href="levelHierarchy.action?level=${searchList.level}">
<s:hidden name="submitList[%{#attr.row_rowNum - 1}]" value="%{#attr.row.code}"/>
</display:column>
<%-- Other columns like that --%>
</display:table>
in the action you should create the property placeholder for the submitted values.

Struts2 : Double Iteration over Parameters does not work?

Hello I am a young Software developer,
and I struggled the last 5 days with my code.
Here is my code in JSP:
<s:iterator value="getListeDanach()" status="stat">
<li>
<s:url id="URL_ListeDanach" action="uebersicht_umblaettern">
<s:param name="angeklickteSeitenzahl" value="getListeDanach()[#stat.index]" />
<s:bean name="org.apache.struts2.util.Counter" var="counter">
<s:param name="last" value="3" />
</s:bean>
<s:iterator value="#counter" status="stat1">
<s:property value="#stat1.index" />
<s:param name="%{optionaleParamName4}" value="#optionaleParamValue4" />
</s:iterator>
</s:url>
<s:a href="%{URL_ListeDanach}" cssClass="naviTab">
<s:property value="getListeDanach()[#stat.index]" />
</s:a>
</li>
</s:iterator>
My problem is, the first Iteration works great but the 2nd Iteration works half. In the 2nd case the property works, but the param doesn´t work! Al Variables are available. If i take the param Tag of the 2nd Iteration and place it in the first, it works great! But that isn´t what I want.
This is not an answer.
Here's the JSP, cleaned up, and using more S2 functionality. It was impossible to read the original.
<s:iterator value="listeDanach" status="stat" var="outerItem">
<li>
<s:url id="URL_ListeDanach" action="child">
<s:param name="angeklickteSeitenzahl" value="outerItem" />
<%-- What are you trying to do here? --%>
<s:bean name="org.apache.struts2.util.Counter" var="counter">
<s:param name="last" value="3" />
</s:bean>
<%-- What are you trying to do here? There's nothing to iterate over. --%>
<s:iterator value="#counter" status="stat1">
<s:property value="#stat1.index" />
<s:param name="%{optionaleParamName4}" value="#optionaleParamValue4" />
</s:iterator>
</s:url>
<s:a href="%{URL_ListeDanach}">
<s:property value="outerItem" />
</s:a>
</li>
</s:iterator>
In the bean i have a loop with 3 "rounds", in the 2nd iterator i use the var=counter to iterate three times over the property and over the dynamic parameter.
The property shows in HTMl in every loop of the first iterator this result : 0 1 2;
This is how it should work(the property is just there, to test the functionality of the 2nd itarator.)
But in the 2nd case, the parameter-Tag is fully ignored or something like that. For those who want to know the logic behind the code. It is a side navigation bar. listeDav or = list behind the actual number, and listeDanach= list after the actual number. 1 2 3 4 [5] 6 7 8 9.... When the param Tag in the 2nd itarator functions well, I would make the param tag dynamically with the iterated index.
SO in short, what I want is: Every time the first Iterator has his loop, I want to create dynamic parameters. This parameters are defined in the JSP before and are fully supported! I want to use the index "#stat1.index" to make it work.
Something like this :
s:param name="%{optionaleParamName[#stat1.index]}" value="#optionaleParamValue[#stat1.index]" />.....
i have already defined the String behind "#optionaleParamValue[0], behind #optionaleParamValue[1] and behind #optionaleParamValue[2] and soo on... ... and all this is for reusing the actual JSP.
As you can mention, a side navigation bar, can be used in many other cases in the programm.
Greetings

Property value in <s:textfield> struts2

How to set propety value in <s:textfield>
I tried <s:textfield name="customerName" label="Customer Name" value='<s:property value="userInfo.customerName"' /> but it didn't work.
Please help
You can not use a tag inside of a tag. Use OGNL instead!
<s:textfield name = "customerName"
label = "Customer Name"
value = "%{userInfo.customerName}"/>
Use OGNL (Object Graph Navigation Language) to get value in any kind of field of struts. If you will not get value please check setter and getter of the variable.
<s:textfield name="transValueChange" id="transValueChange"
value ="%{transValue}" theme="simple" maxLength="30"
onkeypress="return isNumberKey(event)">
</s:textfield>
OR
<s:select name="propCode" id="propCode" list="propClassMasMap" theme="simple"
value="%{propCode}" onchange="" cssClass="text">
</s:select>
I used this:
<table>
<s:label>User Name:</s:label>
<s:textfield name="user.userid" cssClass="tb5" type="text" placeholder="User Name" value="%{#session.userid}" disabled="true" />
</table>
The disabled attribute is not mandatory, I used it to disable the textfield that way the values will be retrieved from the DB and will be disable for the user to make changes on it.

calculate in struts 2 tag?

I have an iterate and i want to calculate sum of the values like this :
<s:iterator value="myValues" status="myStatus">
<s:property value="value" />
</s:iterator>
<s:property value="total.here" />
I want to show the sum of "value" in "total.here".
Sorry for my bad english.
Thank you very much.
Assuming myValues is an array or a list of integral values accessible from your action:
<s:set var="total" value="%{0}" />
<s:iterator value="myValues">
<s:set var="total" value="%{top + #attr.total}" />
</s:iterator>
<s:property value="%{'' + #attr.total}" />
Samuel_xL's answer is right. But, in general, if you can edit your action class, I'd advice to make the calculation there instead of doing it in jsp.

Struts2 taglib, compare with null

I am trying to know why this code is not working when I compare a String with null in a JSP.
<s:set name="myvar" value="%{'teststring' != null}" /> <!-- always true -->
myvar value is ${myvar}
Above code works fine, and prints "myvar value is true".
But doing any of these
<s:property value="myvar" />
<s:property value="%{myvar}" />
throws a ClassCastException
Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
So I do not know how to solve it, as I need to disable some inputs based on that variable value, ie.
<s:select ... disabled="%{myvar}" />
Thank you very much for your help.
This seems to work:
<s:property value="%{#myvar}" />
Try this
<s:property value="%{myvar.toString}" />
<s:select ... disabled="%{myvar.toString}" />

Resources