开发者

Creating "yes" and "no" radio buttons using struts2 and freemarker

In my struts2 application I'd like to bind a boolean property (lets call it frobbed with an isFrobbed() getter) on my controller to a pair of "yes" and "no" radio buttons (I know that I could use a checkbox, but for various reasons we would prefer an explicit "yes" and "no").

With a checkbox, I'd do something along the lines of this:

 <@s.checkbox name="frobbed" label="would you like this to be frobbed?" />

However, with radio buttons I am totally lost. I know there's a struts2 tag named "radio" but I'm not sure how it's 开发者_运维知识库used. For example, this doesn't work.

<@s.radio name="frobbed"  value="true"  label="Please frob this object" />
<@s.radio name="frobbed"  value="false" label="No frobbing, thanks." />

I'd like this to be bound to the value of 'frobbed' in the controller so that, for example, when the value is true the "yes" radiobutton is automatically selected.

Any ideas?


This syntax works with FreeMarker for a boolean choice:

<@s.radio label="" name="frob" list="#@java.util.LinkedHashMap@{true:'Frob it',false:'Leave unfrobbed'}" />


When the choice is a simple Yes/No, True/False - I prefer to define the choices in-situ within the jsp. Something like ..

<s:radio label="Frobbed?" list="#{true:'Frob Me',false:'Leave me Frobless'}" name="frobbed" value="%{frobbed}"></s:radio>

Regards


Normally the <s:radio> tag is used with a list of options (or a map). For example, with a map, the key would be the radio values (like Boolean.TRUE and Boolean.FALSE in this case) and the value would be the radio button label ("Frob away", "I choose to remain frobbedless").

<s:radio list="yesNo" name="frobbed"/>

The yesNo attribute value is that list or map.

(With a list you'd expose an object with the label and value and use the "listKey" and "listValue" <s:radio> attributes. (IIRC))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜