简体   繁体   中英

Selecting default Boolean value in <form:select> in SpringMVC

I have a problem with from in Spring MVC. Selecting something in form and submitting the form works perfectly, selecting default value for non-Boolean values also works, but I have problem selecting default value for Booleans.

My jsp file looks something like this:

<form:form commandName="filterData" id="user_filter_form" action="${listUrl}" method="POST">
    <form:label path="active">Active</form:label><br/>
    <form:select path="active">
        <option value="">--</option>
        <option value="true">Yes</option>
        <option value="false">No</option>
    </form:select>

    <form:label path="email">E-mail</form:label><br/>
    <form:input path="email" type="search"/>

    <button class="b not-ui" type="submit" style="margin-right: 25px">Search</button>
</form:form>

Model object is like this:

public class UserFilterData {
    private Boolean active;
    private String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }
}

As I said, submitting works fine, but when I create controller like this, "email" field is filled with "test@example.com", but selectbox has selected the first option "---"

@RequestMapping(value = "/filter")
public String filterAction(Model model) {
    UserFilterData data = new UserFilterData();
    data.setEmail("test@example.com");
    data.setActive(Boolean.TRUE);
    model.addAttribute("filterData", data);
    return "users/index;
}

I also tried change "true" in my jsp to ${true} or TRUE or 1, but without any success. If I echo filterData in jsp using ${filterData.active} , the result is true , but appropriate option is not selected. Does anyone have an idea what am I doing wrong?

You probably need to use the form options tag.

<form:option value="true" label=""/>
<form:option value="false" label=""/>

You are not using the Spring tags appropriately.

One simple way you could change your code would be:

<form:select path="active">
     <form:option value="">--</option>
     <form:options items="${activeValues}" itemLabel="active" itelLabel="active" >
</form:select>


@RequestMapping(value = "/filter")
public String filterAction(Model model) {
    UserFilterData data = new UserFilterData();
    data.setEmail("test@example.com");
    data.setActive(Boolean.TRUE);
    model.addAttribute("filterData", data);
    model.addAttribute("activeValues", Lists.newArrayList("yes", "no"));
    return "users/index;
}

The way you are using the tags, Spring won't know when to add the selected value to the rendered HTML option

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM