gpt4 book ai didi

java spring - 具有附加值的发布请求

转载 作者:行者123 更新时间:2023-12-02 13:10:13 28 4
gpt4 key购买 nike

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

post 请求工作正常,但是现在我想将另一个值(角色,Long)从表单传递到 Controller ,该值不是用户模型的属性。

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

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>

Controller :

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

最佳答案

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

关于java spring - 具有附加值的发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43981944/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com