【问题标题】:java spring - post request with additional valuejava spring - 具有附加值的发布请求
【发布时间】:2017-05-15 22:24:44
【问题描述】:

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

post 请求工作正常,但是现在我想将另一个值(role,Long)从表单传递给不是用户模型属性的控制器。

那部分不工作。 有谁知道为什么?

HTML:

<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";
}

【问题讨论】:

    标签: java html spring post thymeleaf


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答,不幸的是,这不起作用。
    • 你删除了th:field="${role}"吗?
    • 在这种情况下name="role"也是需要的,因为“name”属性用于表单发布
    • 我什至尝试了一个普通的输入 type=text 看看它是否与选择选项有关
    猜你喜欢
    • 1970-01-01
    • 2018-06-11
    • 2014-12-10
    • 2021-05-03
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 2020-01-19
    • 2013-07-15
    相关资源
    最近更新 更多