如果等于胡子中的条件

时间:2019-07-18 07:54:46

标签: java spring-mvc mustache

在我的应用程序中,我有一个包含用户的数据库。用户具有名为Gender的字符串字段。我想制作一个表单,用户可以在其中编辑他的个人资料。应该填充现有数据。

我在Java代码中有这样的post方法:

@GetMapping("/edit/profile")
    public ModelAndView editProfile(Map<String, Object> model){
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();

        User user = userRepository.findByUsername(auth.getName());

        model.put("name", user.getName());
        model.put("last_name", user.getLastName());
        model.put("company", user.getCompany());
        model.put("address", user.getAddress());
        model.put("gender", user.getGender());
        model.put("birth_date", user.getBirthDate());

        return new ModelAndView("edit_profile", model);
    }

这是我的胡子edit_profile模板:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>To do list profile editing</title>
</head>
<body>
<p>Profile editing</p>

<form action="/api/users/update" method="post">
    <div><label> Name : <input type="text" name="name" value="{{name}}" /> </label></div>
    <div><label> Last Name : <input type="text" name="lastName" value="{{last_name}}" /> </label></div>
    <div><label> Gender : <br>
        {{#gender}}
            {{#"male"}}
                <input type="radio" name="gender" value="male" checked> Male<br>
                <input type="radio" name="gender" value="female"> Female
            {{/"male"}}
            {{#"female"}}
                <input type="radio" name="gender" value="male"> Male<br>
                <input type="radio" name="gender" value="female" checked> Female
            {{/"female"}}
        {{/gender}}
        {{^gender}}
            <input type="radio" name="gender" value="male"> Male<br>
            <input type="radio" name="gender" value="female"> Female
        {{/gender}}
    </label></div>
    <div><label> Company : <input type="text" name="company" value="{{company}}" /> </label></div>
    <div><label> Birth Date : <input type="date" name="birthDate" value="{{birth_date}}" /> </label></div>
    <div><label> Address : <input type="text" name="address" value="{{address}}" /> </label></div>
    <input type="hidden" name="_csrf" value="{{_csrf.token}}" />
    <div><input type="submit" value="Submit"/></div>
</form>
</body>
</html>

我想在以下地方实现逻辑:

if(gender)
   if(gender.equals("male"))
      male radio button checked
   if(gender.equals("female"))
      female radio button checked
else
   no radio buttons checked

现在它不能正常工作,请告诉我如果胡须中的条件相等,我该怎么做?

1 个答案:

答案 0 :(得分:0)

好吧,我猜是在模型中使用布尔值的最佳方法。

java:

            if(!user.getGender().isEmpty()) {
                if(user.getGender().equals("male"))
                    model.put("gender", false);
                else if(user.getGender().equals("female"))
                    model.put("gender", true);
            }

胡子:

        {{#gender}}
            <input type="radio" name="gender" value="male" > Male<br>
            <input type="radio" name="gender" value="female" checked> Female
        {{/gender}}
        {{^gender}}
            <input type="radio" name="gender" value="male" checked> Male<br>
            <input type="radio" name="gender" value="female"> Female
        {{/gender}}