[英]Spring MVC Request method 'POST' not supported -> HTTP 405
Got stuck for ~4 hours wondering where is the mistake in Spring MVC/thymeleaf app.卡住了大约 4 个小时,想知道 Spring MVC/thymeleaf 应用程序中的错误在哪里。
My local goal is to render admin.html after submitting login/pass form at home page.我的本地目标是在主页提交登录/通过表单后呈现 admin.html。
Controller: Controller:
@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:主页.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 >
User class:用户 class:
@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;
}
So I have googled loads of ideas, removed spring security from pom.xml,所以我搜索了很多想法,从 pom.xml 中删除了 spring 安全性,
tried to use @RequestedMapping with RequestMethod.PUT, - no way, it doesn't work.尝试将 @RequestedMapping 与 RequestMethod.PUT 一起使用, - 没办法,它不起作用。
Your form references th:action="@{/admin}"
as target.您的表单引用th:action="@{/admin}"
作为目标。 Your controller does not map /admin
but only root /
.您的 controller 没有 map /admin
但只有 root /
。
You have to change your target to /
.您必须将目标更改为/
。
If you want to render a template /admin
then your return is correct.如果你想渲染一个模板/admin
那么你的回报是正确的。 If you want to redirect to /admin
so a new controller may handle this, so you have to write redirect:/admin
instead.如果你想重定向到/admin
,那么一个新的 controller 可能会处理这个问题,所以你必须改写redirect:/admin
。
Your controller should be like this:你的 controller 应该是这样的:
@Controller
public class HomeController {
@GetMapping("/")
public String getHome(Model m) {
m.addAttribute("user", new User());
return "/home";
}
@PostMapping("/admin")
public String getSubmit(User user) {
return "/admin";
}
}
home.html shoud be like: home.html 应该是这样的:
<form 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 >
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.