0

I am migrating checkbox from Struts1 to Struts2. So I changed the code from Struts1

<html:checkbox style="border:0px" property="eDir" disabled="true"/>

to

<s:checkbox theme="simple" style="border:0px" name="eDir" disabled="true"></s:checkbox>

but I don't get the checkbox as checked even if the value is true

Below are the html script of both

Struts1 html:

<input type="checkbox" name="eDir2ColTrue" value="on" checked="checked" disabled="disabled" style="border:0px">

Struts2 html:

<input type="checkbox" name="eDir2ColTrue" value="true" disabled="disabled" id="eDir2ColTrue" style="border:0px">
<input type="hidden" id="__checkbox_eDir2ColTrue" name="__checkbox_eDir2ColTrue" value="true" disabled="disabled">

The field is boolean .I am getting its value in action class as true from model object..but still its unchecked.

Update:

I tried this but its not compiling

<s:checkbox theme="simple" style="border:0px" key="eDir" disabled="true" <s:if test='${eDir}' >checked="checked"</s:if>></s:checkbox>

Update1:

I tried this but there is no checked attribute so it is not checked despite its value being true

<s:checkbox theme="simple" style="border:0px" key="eDir" disabled="true" value="%{#eDir}"></s:checkbox>

Update2:

When I print, i.e. <c:out value="${eDir}"></c:out>, I get the value as true but the same I put it in the value attribute, it still doesn't have a checked attribute.

<s:checkbox theme="simple" style="border:0px" key="eDir" disabled="true" value="${eDir}"></s:checkbox>
Roman C
  • 49,761
  • 33
  • 66
  • 176
sweety
  • 105
  • 1
  • 6
  • Are you trying out the this example http://www.mkyong.com/struts2/struts-2-scheckbox-checkbox-example/ – Santhosh Nov 05 '14 at 09:08

1 Answers1

1

Struts2 checkbox tag behave differently than its analog in Struts1.

And, as you have mentioned Struts2 creates two input fields for a single checkbox - one for checked value, and other for unchecked value.

Then passing one of them that is checked. The default stack of interceptors include checkbox interceptor that handles submitted values.

If you are using Struts2 checkbox and want to use field value to be not boolean then you should use fieldValue attribute.

<s:checkbox theme="simple" cssStyle="border:0px" name="eDir" disabled="true" fieldValue="on"/>
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • field Value is boolean,i get the value as true in html but still there is no checked attribute – sweety Nov 05 '14 at 10:11
  • It can't be boolean because `value="on"` is generated. The second is boolean because `value="true"`. – Roman C Nov 06 '14 at 13:32
  • when i explicitly specify value="true", it checks the checkbox.but the same thing i do it with jstl i.e. value="${eDir}" it is unchecked and i when i print on jsp ${eDir} it gives me true.what can be the issue?? – sweety Nov 07 '14 at 04:01
  • you can't do such things because Struts tags don't have JSTL functionality. – Roman C Nov 07 '14 at 09:07