简体   繁体   English

在中选择默认布尔值 <form:select> 在SpringMVC中

[英]Selecting default Boolean value in <form:select> in SpringMVC

I have a problem with from in Spring MVC. 我在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: 我的jsp文件看起来像这样:

<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 "---" 就像我说的,提交工作正常,但是当我创建这样的控制器时,“电子邮件”字段填充为“ test@example.com”,但是selectbox选择了第一个选项“ ---”

@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. 我还尝试将jsp中的“ true”更改为$ {true}或TRUE或1,但没有成功。 If I echo filterData in jsp using ${filterData.active} , the result is true , but appropriate option is not selected. 如果我使用${filterData.active}在jsp中回显${filterData.active} ,则结果为true ,但未选择适当的选项。 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. 您没有正确使用Spring标记。

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 您使用标记的方式,Spring将不知道何时将selected值添加到呈现的HTML option

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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