如果未选中,则在UI中显示带有默认值的枚举值 [英] display Enum Values in UI with default value if not selected

查看:84
本文介绍了如果未选中,则在UI中显示带有默认值的枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的问题分为两个部分。第一部分是在UI上显示枚举,第二部分是处理不在服务端的枚举列表中的所选值。

The question here is kind of divided into 2 parts. First part is more of displaying the enum on the UI and the second part deals with handling the selected value not in the enum list on the service side.

首先,我定义了一个Enum类,如下所示。

To begin with.I have an Enum class Gender defined as below.

public enum Gender {

    Male("M"),
    Female("F");

    private String name;

    private Gender(String name) {
        this.name = name;

    }
        public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

上面的枚举类在User Object中被引用为下面。

The above enum class is referenced in the User Object as below.

public class UserBO {

    private Gender gender;


    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

//other getters and setters

}

在控制器中,我通过执行此操作返回了此userObject和性别下拉列表。

In the controller I return this userObject and the dropdown lists for the genders by doing this.

@Controller
public class UserController {

    @Autowired
    private IUserService userServiceImpl;

    @RequestMapping(value = "/user/{userId}/official", method = RequestMethod.GET)
    public String getUserOfficialInfo(@PathVariable("userId") Integer userId, Model model) throws ServiceBusinessException {

                    UserBO userBO = userServiceImpl.findUserByUserId(userId);
                    model.addAttribute("user", userBO);
                    model.addAttribute("userId", userId);
                    model.addAttribute("genders", EnumSet.allOf(Gender.class));
                    return "official";
    }

现在,性别不是用户对象中的必填字段,因此可能会发生对于某些用户而言,此字段完全没有填充。我应该如何在UI上处理这种情况。我在UI上使用百里香。

Now gender is not a required field in the user object, so it may happen that for some users this field is not populated at all. How should I be handling this condition on the UI. I am using thymeleaf on UI.

我在百里香上尝试过的代码是

the code which I have tried on thymeleaf is this

<label class="control-label col-xs-2">Marital Status</label>
  <div class="col-xs-2">
        <select id="maritalStatus" name="maritalStatus">
            <option th:each="status : ${maritalStatuses}"
                                    th:value="${status}"
                                    th:text="${status}"
th:selected="${user.maritalStatus == null} ? 'Please select a Value' : ${status.equals(user.maritalStatus.name)}">
            </option>
        </select>
</div>

有人可以建议我如何处理吗?

Can someone please advice on how can i handle this ?

也要回答问题的第二部分,说用户未在表单提交中选择任何性别,因为它不是必填字段。如何在UI上进行处理,以使服务在收到性别列表中未包含的性别时也不会引发异常。

Also coming to the second part of the question, say the user does not select any gender on form submit since its not a required field. How should that be handled on the UI, so that service does not throw an exception if it receives a gender not in the gender enum list.

推荐答案

最简单的方法是使用 th:object ,如下所示:

Easiest is to use th:object in the form as shown below:

<form th:object="${user}">
    <div class="form-group">
        <label class="control-label required"></label>
        <select id="maritalStatus" class="form-control required" th:field="*{maritalStatus}">
            <option value=""></option>
            <option th:each="status : ${maritalStatuses}" th:value="${status}" 
                th:text="${status}"></option>
        </select>
    </div>
    <div class="form-group">
        <label class="control-label"></label>
        <select id="gender" class="form-control" th:field="*{gender}">
            <option value=""></option>
            <option th:each="e : ${genders}" th:value="${e.name}" 
                th:text="${e}"></option>
        </select>
    </div>
</form>

这篇关于如果未选中,则在UI中显示带有默认值的枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆