您好,我想在表单中重新设置登录名和密码值
<form action="#" modelAttribute="authentification" th:action="@{/user/add}" method="post">
<div class="form-group">
<label for="exampleInputEmail1">prenom</label>
<input type="text" class="form-control" id="prenom" name="prenom" placeholder="prenom">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<button type="button" class="btn btn-link">Nouveau Client je M'inscris</button>
</form>
确切地说,我想恢复我输入的密码和 prenom 的值,但我没有找到执行此操作的好方法
Controller :
@Autowired
UserImplementation ui;
@PostMapping("/add")
public String authentification( Model m ,@PathVariable final String nom)
{
List<Utilisateur> user;
user =ui.getuser();
m.addAttribute("Utilisateur", ui.getuser());
for(Utilisateur u : user)
{
if(u.getPrenom().equals(nom))
{
return"clienthome";
}
else {
int i =10;
m.addAttribute("i", i);
return"index";
}
}
return "index";
}
最佳答案
您的 PostMapping
方法 authentification
中预计参数错误。您可以执行以下操作,以便能够在 Controller 方法中获取 HTML 输入数据。
这里我假设您的 prenom
和 password
属性包含在名为 User
的对象中。
@PostMapping("/add")
public String authentification(@ModelAttribute("authentification") User user) {
// Your mentioned business logic
}
如果您的 prenom
和 password
属性不包含在对象中,而只是您尝试获取的 String
,那么您可以执行以下操作:
@PostMapping("/add")
public String authentification(@RequestParam String prenom, @RequestParam String password) {
// Your mentioned business logic
}
两个字符串 prenom
和 password
的名称必须等于 HTML 文件中特定属性的 name
标记(即 name="prenom"
和 name="password"
),否则您将收到 null
作为 Controller 方法中不同命名属性的参数。
如果您想要将 HTML 名称绑定(bind)到 HTML 属性,但想要将 Controller 方法参数命名为不同的名称,则 @RequestParam
注释提供了一种指定预期 HTML 的方法将其绑定(bind)到的参数名称。例如。 @RequestParam("prenom") String SomethingDifferent
会将 somethingDifferent
绑定(bind)到名为 prenom
的 HTML 属性。
关于java - 如何在我的 Controller Spring Boot 中获取输入表单中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59010487/