【发布时间】:2026-02-02 11:25:02
【问题描述】:
卡住了大约 4 个小时,想知道 Spring MVC/thymeleaf 应用程序中的错误在哪里。
我的本地目标是在主页提交登录/通过表单后呈现 admin.html。
控制器:
@Controller
public class HomeController {
@GetMapping("/")
public String getHome(Model m) {
m.addAttribute( "user",new User());
return "/home";
}
@PostMapping("/")
public String getSubmit(@ModelAttribute User user){
return "/admin";
}
}
home.html:
<form action="#" th:action="@{/admin}" th:object="${user}" method="post">
<p class="txt">Name: <input type="text" th:field="*{name}"/></p>
<p class="txt">Password: <input type="text" th:field="*{password}"/></p>
<p><input class="button" type="submit" value="Submit" />
<input class="button" type="reset" value="Reset" /></p>
</form >
用户类别:
@Data
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String password;
private boolean isAdmin;
private String address;
}
所以我搜索了很多想法,从 pom.xml 中删除了 spring 安全性,
尝试将 @RequestedMapping 与 RequestMethod.PUT 一起使用,- 没办法,它不起作用。
【问题讨论】:
-
th:action="@{/admin}" - 重定向到 api -> /admin 并且您在控制器中没有该 api?
标签: java spring spring-boot model-view-controller thymeleaf