VNN456
VNN456

Reputation: 147

Struts2 radio: preset value if model is not set

I have a radio tag in my form page. By default, "No" is selected.

<s:radio label="Yes NO" name="accept" list="#{'Y':'Yes','N':'No'}" value="'N'"/>

But if I select Yes and save the form to the database and re-render, then the radio is still showing 'No' while the database and the accept property are set to: 'Yes'.

What am I missing?

Upvotes: 1

Views: 222

Answers (1)

Andrea Ligios
Andrea Ligios

Reputation: 50281

With value="'N'" you are forcing the value to ALWAYS be N.

You need to read the value dynamically from an attribute, pre-setting it in the action:

private String accept;

public String getAccept(){
    if (accept==null) { accept = "N"; }
    return accept;
}
<s:radio label="Yes NO" name="accept" list="#{'Y':'Yes','N':'No'}" value="%{accept}"/>

or even cleaner, remove the value attribute at all:

<s:radio label="Yes NO" name="accept" list="#{'Y':'Yes','N':'No'}" />

and it will preset it with the name attribute.

Upvotes: 1

Related Questions