请求方法';获取';不支持带Thymeleaf的Spring Booth
我正在使用Spring boot、Jpa和Thymeleaf构建一个非常简单的crud应用程序,但我遇到了一个“请求方法‘GET’not supported”的问题。每当我想访问/添加页面,通过该页面我可以添加一个新学生时,我就会出现这个错误。与此错误关联的代码段如下所示: 百里香叶表格:请求方法';获取';不支持带Thymeleaf的Spring Booth,spring,spring-boot,thymeleaf,Spring,Spring Boot,Thymeleaf,我正在使用Spring boot、Jpa和Thymeleaf构建一个非常简单的crud应用程序,但我遇到了一个“请求方法‘GET’not supported”的问题。每当我想访问/添加页面,通过该页面我可以添加一个新学生时,我就会出现这个错误。与此错误关联的代码段如下所示: 百里香叶表格: <h1>Form</h1> <form action="#" th:action="@{/add}" th:object="${addStudent}" method="
<h1>Form</h1>
<form action="#" th:action="@{/add}" th:object="${addStudent}"
method="post">
<p>Full name: <input type="text" th:field="*{fname}" /></p>
<p>Major: <input type="text" th:field="*{major}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
我得到的错误是:
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
谢谢,将控制器方法的
@PostMapping(“/any url”)
更改为@GetMapping(“/any url”)
或@RequestMapping(“/any url”)
简单地说,将上述控制器的方法更改为
@RequestMapping("/add")
public String addNewStudent( @ModelAttribute StudentEntity studentEntity, Model model) {
model.addAttribute("addStudent",studentRepository.save(studentEntity) );
return "/allstudents";
}
将控制器方法的
@PostMapping(“/any url”)
更改为@GetMapping(“/any url”)
或@RequestMapping(“/any url”)
简单地说,将上述控制器的方法更改为
@RequestMapping("/add")
public String addNewStudent( @ModelAttribute StudentEntity studentEntity, Model model) {
model.addAttribute("addStudent",studentRepository.save(studentEntity) );
return "/allstudents";
}
在控制器中,您只有一个映射到
POST
request”/add的方法。您必须将GET
请求映射到其他方法,或者将@PostMapping(“/add”)
更改为@RequestMapping(“/add”)
请注意:
@PostMapping
仅用于映射POST
请求。
@GetMapping
仅用于映射GET
请求。
@RequestMapping
映射所有请求类型在控制器中,您只有一个映射到POST
request”/add的方法。您必须将GET
请求映射到其他方法,或者将@PostMapping(“/add”)
更改为@RequestMapping(“/add”)
请注意:
@PostMapping
仅用于映射POST
请求。
@GetMapping
仅用于映射GET
请求。
@RequestMapping
映射所有请求类型您在如何设置它方面有一些问题。您可能想要的是:
@GetMapping("/add")
public String addNewStudent(Model model) {
model.addAttribute("studentEntity", new StudentEntity()); //create a new bean so that your form can bind the input fields to it
return "add"; //let's say add.html this is the name of your form
}
@PostMapping("/add")
public String addNewStudent( @ModelAttribute StudentEntity studentEntity, Model model) {
//call any service methods to do any processing here
studentRepository.save(studentEntity);
return "redirect:/allstudents"; //this would be your confirmation page
}
您的add.html
表单的内容如下:
<form th:object="${studentEntity}" th:action="@{/add}" method="post" action="allstudents.html">
<!-- input fields here --->
</form>
请注意,
th:object
是您在@GetMapping
方法中添加到模型中的对象 您对如何设置它有一些问题。您可能想要的是:
@GetMapping("/add")
public String addNewStudent(Model model) {
model.addAttribute("studentEntity", new StudentEntity()); //create a new bean so that your form can bind the input fields to it
return "add"; //let's say add.html this is the name of your form
}
@PostMapping("/add")
public String addNewStudent( @ModelAttribute StudentEntity studentEntity, Model model) {
//call any service methods to do any processing here
studentRepository.save(studentEntity);
return "redirect:/allstudents"; //this would be your confirmation page
}
您的add.html
表单的内容如下:
<form th:object="${studentEntity}" th:action="@{/add}" method="post" action="allstudents.html">
<!-- input fields here --->
</form>
请注意,
th:object
是您在@GetMapping
方法中添加到模型中的对象 我这样做了,似乎只有当我将return“/allstudents”替换为return“/add”时,它才会起作用,然后将我带到表单提交新记录。我最初放置return“/allstudents”是因为我想在成功提交表单后返回到allstudents页面?有什么建议吗?谢谢,我这样做了,而且似乎只有当我用return/add替换return/allstudents时,它才起作用,然后我才能进入表单提交新记录。我最初放置return“/allstudents”是因为我想在成功提交表单后返回到allstudents页面?有什么建议吗?谢谢,我这样做了,而且似乎只有当我用return/add替换return/allstudents时,它才起作用,然后我才能进入表单提交新记录。我最初放置return“/allstudents”是因为我想在成功提交表单后返回到allstudents页面?有什么建议吗?谢谢,我这样做了,而且似乎只有当我用return/add替换return/allstudents时,它才起作用,然后我才能进入表单提交新记录。我最初放置return“/allstudents”是因为我想在成功提交表单后返回到allstudents页面?有什么建议吗?谢谢你,汉克斯,这很有效!但是,我想知道是否有一种方法可以将Get和Post方法结合在一起?例如,在使用RequestMapping时,我不确定我是否理解。一种方法是响应用户正在搜索的页面,另一种方法是在用户单击提交按钮时根据表单上的method=“post”调用。那么您想在一个方法中同时使用这两种行为吗?请注意,@GetMapping
和@PostMapping
实际上是快捷方式:谢谢,这很有效!但是,我想知道是否有一种方法可以将Get和Post方法结合在一起?例如,在使用RequestMapping时,我不确定我是否理解。一种方法是响应用户正在搜索的页面,另一种方法是在用户单击提交按钮时根据表单上的method=“post”调用。那么您想在一个方法中同时使用这两种行为吗?请注意,@GetMapping
和@PostMapping
实际上是快捷方式: