How to set the radio button value checked by default in struts2? - struts2

The code I am using is:
<table>
<tr>
<td>
<s:radio name="dlrMaintenanceVO.dbsVO.substitutionMethod" id="substitution" label="" list="#{'M':'Moves History'}" />
<br/>
<s:radio name="dlrMaintenanceVO.dbsVO.substitutionMethod" id="substitution" label="" list="#{'R':'Reports History'}" value="R"/>
</td>
</tr>
</table>
This should make radio button with R value as checked by default. But this thing is not happening. Any suggestions.

Please change value to value="'R'"

I'm okay with following code to set default value in radio button in struts 2.Type checked="true" you want to default in radio tag.
<table>
<tr>
<td>
<s:radio name="dlrMaintenanceVO.dbsVO.substitutionMethod" id="substitution" label="" list="#{'M':'Moves History'}" checked="true"/>
<br/>
<s:radio name="dlrMaintenanceVO.dbsVO.substitutionMethod" id="substitution" label="" list="#{'R':'Reports History'}" value="R"/>
</td>
</tr>
</table>

Make sure that R should have correct value set in your action class.All you need to set the required value in your action class for R and based on the value it will select the appropriate radio button

Related

struts2 iterating dropdown(s:select) inside <td> tag

I am iterating a list object in jsp file which is coming from action class.
Using table,tr,td tags in order to dispay value in tabular format as follows.
<s:iterator value="userList" >
<tr>
<td><s:property value="name" /> </td>
<td><s:property value="gender" /> </td>
<td><s:select name="country" list="countryList" value="%{country}" /> </td>
<td><s:property value="aboutYou"/> </td>
<td><s:property value="malingList /> </td>
<tr>
</s:iterator>
I need to display country drop down with selected value. Which is displaying but not in a proper format. Name and gender is showing in first line, country drop down showing in second line and aboutYou and mailing list showing in third line.
I tried to display drop down value inside tag but not displaying.
What i need to display is all five fields in one single line(tr)
Requesting you please help me.
Thanks in advance.

Struts 2 select tag - Using the tag (with the same list) multiple times on a page does not work

I need to be able to use the same drop down list multiple times on a page. The first time i use the 'list' on the select tag, it works fine. The same list does not populate the second select tag i use it on. Here are the details.
In the action class, i populate the ArrayList containing values i need to populate the select tag.
setNames(new SomeDAO().getNames());
In the JSP
<s:select list="names"
id="nameList"
listKey="nameId"
listValue="userName"
/>
This select list populates just fine. If I use the following select tag on the same page (using the same list), it fails to print.
<s:select list="names"
id="rName"
listKey="nameId"
listValue="userName" />
If i replace the 'list' above with #{'test':'test'} (hardcoded list), the tag shows up fine. Looks like the property i set in the Action is getting cleared after the first use. Is that whats happening or am i doing something wrong? I get an 'IllegalStateException: Response already committed' error
Edit 1:
setNames() is used in the action method that deals with the JSP page. It is a simple getter function.
In the JSP, here is what i have.
<tr>
<td align="left" class="td-plain">Add New:</td>
<td class="td-plain">
<s:select list="names"
id="addNameID"
name="addUserNameID"
listKey="reinsId"
listValue="reinsName"
headerKey=""
headerValue="--User Name--"
/>
</td>
<td class="td-plain"><input id="addTreatyNumber" type="text" /></td>
<td class="td-plain"><input id="addReinsPercentage" type="text" /></td>
<td class="td-plain"><input id="addFlatDollarRetentionAmt" type="text" /></td>
<td class="td-plain">
<%if(finance){ %>
<input type="button" class="greyButton" value="Add" onclick="addReinsInfo()"/>
<%}else{ %>
None
<%} %>
</td>
</tr>
and then later down on that page, i have
<tr id='<s:property value="caseGroupId"/>:<s:property value="treatyId"/>'>
<td class="td-plain"><input type='checkbox' id='<s:property value="caseGroupId"/>:<s:property value="treatyId"/>'/></td>
<td class="td-plain">
<s:select list="names"
id="rName"
name="dName"
listKey="reinsId"
listValue="reinsName"
headerKey=""
headerValue="--User Name--"
/>
</td>
<td class="td-plain_"><s:textfield id="tNumber" value="%{treatyNumber}"/></td>
<td class="td-plain_"><s:textfield id="tPercentage" value="%{reinspercentage}"/></td>
<td class="td-plain_"><s:textfield id="rAmount" value="%{flatDollarRetentionAmt}"/></td>
<td class="td-plain"><input type="button" value="Delete" class="greyButton"/></td>
</tr>
If I change the 'list' in the second tag to
list="#{'Test':'Test'}"
the tag shows up fine. Please let me know if i can provide any further info.
Edit 2: I was able to get this to work by setting the drop down list values to the session.
As asked by #Dave and #Quaternion, post the relevant Java/JSP code.
But until then... I've noted that you are not using name attribute in Struts Selects;
while you can refer multiple times from different tags to the same source (the list attribute), to populate the Select from the same list of objects, you should instead specify a different name for each one, to define which variable (sent back to the Action) contains the selected value of which Select;
this may not be the answer to the current question but it will show up to you soon.

Struts 2 textfield label doesn't show in table

The following jsp file fragment using struts2 tags doesn't show the textfield label. How can I fix this? Is it possible to have the label displayed by using only struts2 tags?
<s:form action="voucher" theme="simple">
<table width=50% border="5">
<tr>
<td>
<s:textfield label="Enter the voucher hrn" name="voucher.SN" />
</td>
<td>
<s:submit value="Check state" />
</td>
</tr>
<tr>
<td>
<s:radio list="#{'used': 'Mark as used', 'available': 'Mark as available'}" name="selections" value="%{'used'}"></s:radio>
</td>
<td>
<s:submit value="Update state" />
</td>
</tr>
</table>
</s:form>
It is because you are using simple theme in your form. Remove theme="simple" from <s:form> tag then Struts2 will do table layout as well field errors and labels for you.
BTW if you really need to use your custom layout then use <s:label> tags for labels.

Iterator, set, if-else tags of Struts2(Getting the value of s:radio and applying if-else )

I have a jsp page in which I have two radio buttons and a select tag. Now, if the first radio button is clicked than I want to make that select tag disable but I am fail to do this , I have tried using "disabled" property. Following is the code.
Jsp Page
<table align="center">
<s:iterator value="FirstObjectList" status="AuthorTypeStatus">
<tr>
<td>
<s:radio name="radio_SelectedValue" list="{ObjectName}" listKey="ObjectKey" listValue="ObjectName" value="DefaultObject"/>
</td>
</tr>
</s:iterator>
<s:if test="%{#radio_SelectedValue == 'ObjectName1'}">
<s:set name="isSelectDisabled" value="false"/>
</s:if>
<s:else>
<s:set name="isSelectDisabled" value="True"/>
</s:else>
<tr >
<td colspan="2">
Select Parent Discover Lab
</td>
</tr>
<tr>
<td>
<s:select name="select_SelectedValue" headerKey="DefaultObject" headerValue="ParentObject" list="ObjectList" listKey="ObjectListKey" listValue="ObjectListValue" disabled="%{isSelectDiabled}">
<s:iterator value="ObjectList">
</s:iterator>
</s:select>
<s:submit value="Continue">
</s:submit>
</td>
</tr>
All the getter and setter methods are fixed..is this possible without javascript?
I'm a little confused by your code block, but I think I understand what you're aiming for. Something like this should work
<tr>
<td>
<s:radio name="radio_SelectedValue" list="%{radioList}" listKey="ObjectKey" listValue="ObjectName" value="DefaultObject"/>
</td>
</tr>
<s:if test="%{radio_SelectedValue.equals('myChosenValueFromTheList')}">
<s:select name="select_SelectedValue" headerKey="-1" headerValue=" " list="ObjectList" listKey="ObjectList.Key" listValue="ObjectList.Value" />
</s:if><s:else>
<s:select name="select_SelectedValue" headerKey="-1" headerValue=" " list="ObjectList" listKey="ObjectList.Key" listValue="ObjectList.Value" disabled="disabled" />
</s:else>
I'm not sure what you're doing with the iterators with these tags, they handle the list propagation themselves. Simply tell the tag what list to use and struts will populate the tags appropriately. You can then do a conditional based on the value of the radio when the page renders and perform magic on the select tag as shown above.

why struts UI tags can not be put together in the same row?

I need 2 textareas in the same row and tried as below but it is printing in the next row.
All struts tags are have same problem.Is there any alternative method ? Please suggest.
<table>
<tr>
<td>
<s:textarea name="1"/>
</td>
<td>
<s:textarea name="2"/>
</td>
</tr>
</table>
This is due to Struts2's default rendering based on its default theme. I find this tutorial gives a good idea of Struts2 theming/templating: http://www.mkyong.com/struts2/working-with-struts-2-theme-template/
If you don't want to use the default and want to lay it out as you have it, try:
<table>
<tr>
<td>
<s:textarea name="1" theme="simple"/>
</td>
<td>
<s:textarea name="2" theme="simple"/>
</td>
</tr>
</table>

Resources