Update operation is not performing -Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

huangapple go评论95阅读模式
英文:

Update operation is not performing -Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

问题

我正尝试开发一个使用Spring Boot + Thymeleaf的应用程序,我能够从MySQL数据库中检索在个人资料选项卡中已登录用户的详细信息,但是当我尝试更改一两个字段的细节(更新),然后点击更新按钮时,它显示了一个错误消息 - Fri Sep 04 20:39:47 IST 2020,错误内容如下:

出现意外错误(类型=方法不允许,状态=405)。
不支持请求方法 'POST'

请看我的控制器代码(我在类的顶部使用了 @RestController 注解):

  1. @RequestMapping(value = "/profile", method = RequestMethod.PUT)
  2. public ModelAndView updateProfile(@ModelAttribute Customer customer, HttpSession session) {
  3. ModelAndView model = new ModelAndView();
  4. Customer exist = cRepo.findByCustEmail(customer.getCustEmail());
  5. if (exist != null) {
  6. if (exist.getCustEmail().equals(session.getAttribute("emailsession"))) {
  7. cRepo.save(customer);
  8. model.addObject("msg", "用户详细信息已成功更新!");
  9. model.setViewName("profile");
  10. }
  11. } else {
  12. model.addObject("exist", "请输入正确的电子邮件地址!");
  13. String email = (String) session.getAttribute("emailsession");
  14. Customer cust = cRepo.findByCustEmail(email);
  15. model.addObject("customer", cust);
  16. model.setViewName("profile");
  17. }
  18. return model;
  19. }

Thymeleaf 代码(HTML):

  1. <div align="center" class="alert alert-success" th:if="${msg}" th:utext="${msg}"></div>
  2. <div align="center" class="alert alert-danger" th:if="${exist}" th:utext="${exist}"></div>
  3. <!-- Modal HTML -->
  4. <div id="myModal">
  5. <div class="modal-dialog modal-login">
  6. <div class="modal-content">
  7. <div class="modal-header">
  8. <h4 class="modal-title">个人资料详情</h4>
  9. </div>
  10. <div class="modal-body">
  11. <form name="myForm" th:action="@{/profile}" th:object="${customer}" method="post">
  12. <div class="form-group">
  13. <i class="fa fa-id-card"></i>
  14. <input name="id" type="text" class="form-control" placeholder="输入ID" th:field="${customer.custId}" disabled="true" required="required" />
  15. </div>
  16. <!-- 其他表单字段... -->
  17. <div class="form-group">
  18. <input type="submit" class="btn btn-primary btn-block btn-lg" value="更新" />
  19. </div>
  20. </form>
  21. </div>
  22. </div>
  23. </div>
  24. </div>

我希望用户登录并访问时能够查看自己的个人资料(这一部分我已经实现了),当用户想要更新一些字段(1-2个,根据选择)并点击更新时,应能够更新详细信息(而不是创建新用户或记录),因为当我在类的顶部使用 @Controller 注解时,这段代码可以正常工作并更新数据,而不是创建新用户。

英文:

I'm trying to develop an application in spring boot + thymeleaf, and I'm able to retrieve the logged in user details in the profile tab from the MySQL database, but when I try to change one or two field details (update) and hit the update button it is showing me an error message - Fri Sep 04 20:39:47 IST 2020
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'POST' not supported

see my controller code (I'm using @RestController annotated on top of the class)-

  1. @RequestMapping(value = &quot;/profile&quot;, method = RequestMethod.PUT)
  2. public ModelAndView updateProfile(@ModelAttribute Customer customer, HttpSession session) {
  3. ModelAndView model = new ModelAndView();
  4. Customer exist = cRepo.findByCustEmail(customer.getCustEmail());
  5. if(exist != null) {
  6. if(exist.getCustEmail().equals(session.getAttribute(&quot;emailsession&quot;))) {
  7. cRepo.save(customer);
  8. model.addObject(&quot;msg&quot;, &quot;User Details has been successfully updated!!&quot;);
  9. model.setViewName(&quot;profile&quot;);
  10. }
  11. }else {
  12. model.addObject(&quot;exist&quot;, &quot;Please enter correct email address!&quot;);
  13. String email = (String) session.getAttribute(&quot;emailsession&quot;);
  14. Customer cust = cRepo.findByCustEmail(email);
  15. model.addObject(&quot;customer&quot;, cust);
  16. model.setViewName(&quot;profile&quot;);
  17. }
  18. return model;
  19. }

Thymleaf code (html) -

  1. &lt;div align=&quot;center&quot; class=&quot;alert alert-success&quot; th:if=&quot;${msg}&quot; th:utext=&quot;${msg}&quot;&gt;&lt;/div&gt;
  2. &lt;div align=&quot;center&quot; class=&quot;alert alert-danger&quot; th:if=&quot;${exist}&quot; th:utext=&quot;${exist}&quot;&gt;&lt;/div&gt;
  3. &lt;!-- Modal HTML --&gt;
  4. &lt;div id=&quot;myModal&quot;&gt;
  5. &lt;div class=&quot;modal-dialog modal-login&quot;&gt;
  6. &lt;div class=&quot;modal-content&quot;&gt;
  7. &lt;div class=&quot;modal-header&quot;&gt;
  8. &lt;h4 class=&quot;modal-title&quot;&gt;Profile Details&lt;/h4&gt;
  9. &lt;/div&gt;
  10. &lt;div class=&quot;modal-body&quot;&gt;
  11. &lt;form name=&quot;myForm&quot; th:action=&quot;@{/profile}&quot; th:object=&quot;${customer}&quot; method=&quot;post&quot;&gt;
  12. &lt;div class=&quot;form-group&quot;&gt;
  13. &lt;i class=&quot;fa fa-id-card&quot;&gt;&lt;/i&gt;
  14. &lt;input name=&quot;id&quot; type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;Enter Id&quot; th:field=&quot;${customer.custId}&quot; disabled=&quot;true&quot; required=&quot;required&quot; /&gt;
  15. &lt;/div&gt;
  16. &lt;div class=&quot;form-group&quot;&gt;
  17. &lt;i class=&quot;fa fa-user&quot;&gt;&lt;/i&gt;
  18. &lt;input name=&quot;name&quot; type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;Enter Name&quot; th:field=&quot;${customer.custName}&quot; required=&quot;required&quot; /&gt;
  19. &lt;/div&gt;
  20. &lt;div class=&quot;form-group&quot;&gt;
  21. &lt;i class=&quot;fa fa-envelope&quot;&gt;&lt;/i&gt;
  22. &lt;input name=&quot;email&quot; type=&quot;email&quot; class=&quot;form-control&quot; placeholder=&quot;Enter Email&quot; th:field=&quot;${customer.custEmail}&quot; required=&quot;required&quot; /&gt;
  23. &lt;/div&gt;
  24. &lt;div class=&quot;form-group&quot;&gt;
  25. &lt;i class=&quot;fa fa-lock&quot;&gt;&lt;/i&gt;
  26. &lt;input name=&quot;password&quot; type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;Enter Password&quot; th:field=&quot;${customer.custPassword}&quot; required=&quot;required&quot; /&gt;
  27. &lt;/div&gt;
  28. &lt;div class=&quot;form-group&quot;&gt;
  29. &lt;input type=&quot;submit&quot; class=&quot;btn btn-primary btn-block btn-lg&quot; value=&quot;Update&quot; /&gt;
  30. &lt;/div&gt;
  31. &lt;/form&gt;
  32. &lt;/div&gt;
  33. &lt;/div&gt;
  34. &lt;/div&gt;
  35. &lt;/div&gt;

I want when user login and visit he/she should be able to check his/her profile(which I'm able to do working code) and when the user wants to update few fields(1-2 based on choice) and hit update he/she should be able to update the details (not create new user or record) because when I use @Controller on top of class then this code work and create new user instead update.

答案1

得分: 1

你的控制器使用了@RequestMapping(value = "/profile", method = RequestMethod.PUT)进行了注解,这使它成为了一个PUT端点。然而,你的请求明显是一个POST请求。如果我们看一下你的HTML表单,它包含了method="post"。HTML表单只支持GET和POST作为有效的方法,所以你需要将你的端点更新为一个POST端点。

简而言之:

  1. @RequestMapping(value = "/profile", method = RequestMethod.PUT)

更新为

  1. @RequestMapping(value = "/profile", method = RequestMethod.POST)
英文:

Your controller is annotated with @RequestMapping(value = &quot;/profile&quot;, method = RequestMethod.PUT) which makes it a PUT endpoint. However, your request is clearly a POST. If we look at your html form it contains method=&quot;post&quot;. HTML forms only support GET and POST as valid methods so you need to update your endpoint to be a POST endpoint.

tldr;

  1. RequestMapping(value = &quot;/profile&quot;, method = RequestMethod.PUT)

to

  1. RequestMapping(value = &quot;/profile&quot;, method = RequestMethod.POST)

答案2

得分: 1

你在<form>中请求映射是POST,但控制器已设置为接受PUT请求。

  1. <form name="myForm" th:action="@{/profile}" th:object="${customer}" method="post">
  1. @RequestMapping(value = "/profile", method = RequestMethod.PUT)

保持这些内容类似,两者应该相同。

英文:

You request mapping in <form> is POST but Controller has set to accept request as PUT.

  1. &lt;form name=&quot;myForm&quot; th:action=&quot;@{/profile}&quot; th:object=&quot;${customer}&quot; **method=&quot;post&quot;**&gt;
  2. @RequestMapping(value = &quot;/profile&quot;, method = **RequestMethod.PUT**)

Just keep these in similar way both should be same.

答案3

得分: 1

  1. 请检查我找到的内容并解决这个问题
  2. @RequestMapping(value = "/profile", method = RequestMethod.POST)
  3. public ModelAndView updateProfile(@ModelAttribute Customer customer, HttpSession session) {
  4. ModelAndView model = new ModelAndView();
  5. Customer exist = cRepo.findByCustEmail(customer.getCustEmail());
  6. if (exist != null) {
  7. if (exist.getCustEmail().equals(session.getAttribute("emailsession"))) {
  8. exist.setCustId(exist.getCustId());
  9. exist.setCustName(customer.getCustName());
  10. exist.setCustEmail(customer.getCustEmail());
  11. exist.setCustPassword(customer.getCustPassword());
  12. cRepo.save(exist);
  13. model.addObject("msg", "用户详情已成功更新!");
  14. model.addObject("customer", exist);
  15. model.setViewName("profile");
  16. }
  17. } else {
  18. model.addObject("exist", "请输入正确的电子邮件地址!");
  19. String email = (String) session.getAttribute("emailsession");
  20. Customer cust = cRepo.findByCustEmail(email);
  21. model.addObject("customer", cust);
  22. model.setViewName("profile");
  23. }
  24. return model;
  25. }
英文:

Please check what I find and resolve this.

  1. @RequestMapping(value = &quot;/profile&quot; ,method = RequestMethod.POST)
  2. public ModelAndView updateProfile(@ModelAttribute Customer customer, HttpSession session) {
  3. ModelAndView model = new ModelAndView();
  4. Customer exist = cRepo.findByCustEmail(customer.getCustEmail());
  5. if(exist != null) {
  6. if(exist.getCustEmail().equals(session.getAttribute(&quot;emailsession&quot;))) {
  7. **exist.setCustId(exist.getCustId());
  8. exist.setCustName(customer.getCustName());
  9. exist.setCustEmail(customer.getCustEmail());
  10. exist.setCustPassword(customer.getCustPassword());**
  11. cRepo.save(exist);
  12. model.addObject(&quot;msg&quot;, &quot;User Details has been successfully updated!!&quot;);
  13. model.addObject(&quot;customer&quot;, exist);
  14. model.setViewName(&quot;profile&quot;);
  15. }
  16. }else {
  17. model.addObject(&quot;exist&quot;, &quot;Please enter correct email address!&quot;);
  18. String email = (String) session.getAttribute(&quot;emailsession&quot;);
  19. Customer cust = cRepo.findByCustEmail(email);
  20. model.addObject(&quot;customer&quot;, cust);
  21. model.setViewName(&quot;profile&quot;);
  22. }
  23. return model;
  24. }

huangapple
  • 本文由 发表于 2020年9月4日 23:24:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63743981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定