struts2: problem in assigning a variable value to checkbox - struts2

I am working on struts2. In my jsp page I want to assign the value of a string variable to a checkbox (when it is checked by user). I tried it many times like -
<% String code = "decompose"; %>
First example:
<tr><td>
<s:checkbox name="codeCkBox" fieldValue="%{‘code’}" onclick="submit()"/>
</td></tr>
Second example:
<tr><td>
<s:checkbox name="codeCkBox" value="%{‘code’}" onclick="submit()"/>
</td></tr>
Third example:
<tr><td>
<s:set name="setCkBoxValue" value="%{‘code’}"/>
<s:checkbox name="codeCkBox" fieldValue="# setCkBoxValue" onclick="submit()"/>
</td></tr>
But everytime when I tried to get this value by checkbox name it returns variable name i.e “code”.
Looking for a solution.
Thanks in advance.

Have you tried doing ${code} instead of ${'code'}?

did u include your struts directive? i ask because its only showing 'code' which could mean its ignoring the struts

<tr><td> <s:checkbox name="codeCkBox" value="%{#code}"
onclick="submit()"/> </td></tr>
try the above code. since code is the JSP variable therefore it should be accessed with a # in front of its name rather than with a quote. Hope this helps

Related

Use struts2 iterator or initialize arraylist

I have a List. The user can add n item number and description.
He will add first and then to add second will press add more button .
I am giving a call to action class storing the data in array and redirecting back to the page to get the next record.
When I am using the iterator the text boxes are getting added again.
If I dont use iterator my arraylist is getting initailized everytime I visit the action class.
I cannot make it static.It will helpful if anyone can help me either to not initailize the arraylist everytime or use iterator and not repeat the textfields.
<table>
<tr align="center">
<s:iterator value="preAdviceDetailsDO" id="preAdviceDetailsDO" status="outerStat">
<s:if test="#outerStat.may be some useful word like even odd first == true">
<td style="background: #CCCCCC">
<s:textfield value="%{itemNumber}" name="preAdviceDetailsDO[%{#outerStat.index}].itemNumber" onblur="checkBarcode();"/>
</td>
<td style="background: #CCCCCC">
<s:textfield value="%{itemDescription}" name="preAdviceDetailsDO[%{#outerStat.index}].itemDescription"/>
</td>
</s:if>
</s:iterator>
</tr>
</table>
<input type="button" value="Add More" onclick="addRow()" />
Thanks Dave Newton for editing. MohanaRao SV, I found the solution on it actually its a tweak.The problem I was facing in the above code was :
When the user see the form for the first time he will see two text field.He will enter the details and to add another record he will press add button. This time the arraylist is incremented. So the text fields on the form will increase to four.That is two earlier one and the new two text fields.And so on...
So now what I did is in the if condition I am checking if the index is last then I am displaying the fields and in the else part I am writing the same code for adding text field but I have set the css property display:none.
If I dont write the else part the arraylist is properly incremented but the details in the arraylist are not persisted.
So now only the required text fields are displayed.

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

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.

Struts2 How to create a table by iterating a list such that each row has a radio button?

I am trying to create a table to display some data using iterator over my list from action class. Each row has the properties of each object in the list.
<table class="TableA" cellpadding="5px" border="1">
<tr class="even">
<th></th>
<th>Col1</th>
<th>Col2</th>
</tr>
<s:iterator value="objectList" status="obj">
<tr
class="<s:if test="#obj.odd == true ">odd</s:if><s:else>even</s:else>">
<td></td>
<td><s:property value="objId" /></td>
<td><s:property value="objValue" /></td>
</tr>
</s:iterator>
I need a radio button in first cell of every row so that a row can be selected for edition or deletion.
I know this can be done using <input type="radio" ...>. But I need to use struts2's radio tag. But problem using this tag is that it generates a row for every radio tag (see here).
Is it possible ? If yes then how ?
I think the following syntax will be better for you as you want to select each row with radio button
<s:radio theme="simple" name="object_radio" list="#{objId:objId}"/>
This way you wont need the objId column too
This syntax will submit objId as the value of selected radio button.You can modify the syntax to change the display property of the radio button or make it blank
anu, your answer helped me a lot.
Also this link was of great help for me:
http://struts.apache.org/2.0.14/docs/how-do-i-render-a-single-radio-button.html
What I needed was a table where two of the columns were radio buttons, but each column was an independent group. Furthermore I did not want to show the key as the label/value for each radio button, since that information was displayed in the other columns. My rows were built iterating over a hashmap, where the key of the hashmap was the radio button value.
<s:iterator value="myHashMap">
<tr>
<td><s:radio name="selectedRadioValueFirstColumn" list="#{key:''}"/></td>
<td><s:radio name="selectedRadioValueSecondColumn" list="#{key:''}"/></td>
<%--....... (my other columns) .... --%>
<tr>
So I am setting the option value as the key of the hashmap, and the selected value will be stored in the string property "selectedRadioValueFirstColumn" and "selectedRadioValueSecondColumn".
Kaillash:
if i understand your requirements completely all you mean to say that when page getting displayed with struts2 radio button struts2 is automatically creating a row for each radio button.
if this is the case than that is default struts2 functionality,struts2 operates on template theme and the default theme (xhtml) will automatically generate some HTML markup for each struts2 tag.
if you want struts2 tags not to generate any extra markup use the theme as simple.it can be done per tag basis something like this
<s:radio key="personBean.gender" list="genders" theme="simple" />
or you can also set theme on the page basis..
create struts2.properties file in the root and put the following entry
struts.ui.theme=simple
i hope this will help you

Resources