带有附加值的JavaSpring-post请求

带有附加值的JavaSpring-post请求,java,html,spring,post,thymeleaf,Java,Html,Spring,Post,Thymeleaf,我已经实现了一个注册过程,您可以通过post请求将用户数据发送到控制器 post请求工作正常,但是现在我想传递另一个值角色,从表单到控制器,它不是用户模型的属性 那部分不起作用。 有人知道为什么吗 HTML: 以及: th:field=${role}表示模型对象中字段的名称,而不是其值。您可能想写th:value=${role}而不是这个。谢谢您的回答,不幸的是这不起作用。您是否删除了th:field=${role}?是管理员用户在这种情况下还需要name=角色,因为name属性用于表单发布我甚

我已经实现了一个注册过程,您可以通过post请求将用户数据发送到控制器

post请求工作正常,但是现在我想传递另一个值角色,从表单到控制器,它不是用户模型的属性

那部分不起作用。 有人知道为什么吗

HTML:

以及:


th:field=${role}表示模型对象中字段的名称,而不是其值。您可能想写th:value=${role}而不是这个。

谢谢您的回答,不幸的是这不起作用。您是否删除了th:field=${role}?是管理员用户在这种情况下还需要name=角色,因为name属性用于表单发布我甚至尝试了一个普通的输入类型=文本,以查看它是否与选择选项有关
<form action="add_user" method="post" class="form-horizontal" th:object="${user}">
                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-10">
                            <input th:field="*{username}" class="form-control" placeholder="Person ID" type="text" name="id" id="id"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-10">
                            <input th:field="*{firstName}" class="form-control" placeholder="First Name" type="text" name="firstname" id="firstname"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-10">
                            <input th:field="*{lastName}" class="form-control" placeholder="Last Name" type="text" name="lastname" id="lastname"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-10">
                            <input th:field="*{password}" class="form-control" placeholder="Password" type="password" name="password" id="password"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-10">
                            <select th:field="${role}" class="form-control" id="role">
                                <option value="1">Admin</option>
                                <option value="2" >User</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-10">
                            <button type="submit" class="btn btn-success" value="Submit">Save</button>
                        </div>
                    </div>
                </form>
    @RequestMapping(value = "/users", method = RequestMethod.GET) 
    public String showUsers(Model model) 
        model.addAttribute("user", new User());
        model.addAttribute("role", new Long(2));
        return "users";
}
@RequestMapping(value = "/add_user", method = RequestMethod.POST)
public String handleNewUser(@ModelAttribute("user") User user, BindingResult bindingResult, Model model, long role) {
    if (user != null) {
        System.out.println(role);
        userService.save(user);
    }
    return "redirect:/users";
}