Spring MVC无法调用控制器。

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

Spring mvc from doesn't call controller

问题

  1. 我是新手使用Spring框架,我只是在创建自己的项目以便理解。这个流程是登录 -> 客户列表 -> 选择一个客户 -> 宠物列表。
  2. 在第一页上有一个带有表单的按钮,用于添加客户,它能正常工作。然而,类似的代码在第二个页面上用于添加宠物的按钮却不能正常工作。当我尝试保存新的宠物时,它返回HTTP 400 - 错误的请求,控制台中没有任何信息。
  3. pet-form.jsp:
  4. <form:form action="${pageContext.request.contextPath}/pet/savePet"
  5. modelAttribute="pet" method="POST">
  6. <!-- 需要将此数据与客户ID关联起来 -->
  7. <form:hidden path="owner.id" />
  8. ......
  9. <td><input type="submit" value="保存" class="save" /></td>
  10. </tr>
  11. </tbody>
  12. </table>
  13. PetController.java:
  14. @Controller
  15. @RequestMapping("/pet")
  16. public class PetController {
  17. ......
  18. @PostMapping("/savePet")
  19. public String savePet(@ModelAttribute("pet") Pet thePet) {
  20. System.out.println("新的宠物 " + thePet.toString());
  21. thePet.setOwner(customerService.getCustomer(thePet.getOwner().getId()));
  22. System.out.println("控制器 " + thePet);
  23. // 使用我们的服务保存客户
  24. petService.savePet(thePet);
  25. return "redirect:/pet/showListPets";
  26. }
  27. }
  28. 有什么想法是出了什么问题吗?
  29. 提前感谢。
英文:

I'm new using spring framework and I'm just creating my own project in order to catch the idea. The flow of this is loging -> customer list -> select one cusstomer -> Pet list.
I have a button on first page with a form in order to add a customer and it is working properly. However, similar code to a second button in pet page to add a pet is not working. When I try to save the new pet, it is returning HTTP 400 – Bad Request and nothing in the console

pet-form.jsp :

  1. &lt;form:form action=&quot;${pageContext.request.contextPath}/pet/savePet&quot;
  2. modelAttribute=&quot;pet&quot; method=&quot;POST&quot;&gt;
  3. &lt;!-- need to associate this data with customer id --&gt;
  4. &lt;form:hidden path=&quot;owner.id&quot; /&gt;
  5. ......
  6. &lt;td&gt;&lt;input type=&quot;submit&quot; value=&quot;Save&quot; class=&quot;save&quot; /&gt;&lt;/td&gt;
  7. &lt;/tr&gt;
  8. &lt;/tbody&gt;
  9. &lt;/table&gt;

PetController.java

  1. @Controller
  2. @RequestMapping(&quot;/pet&quot;)
  3. public class PetController {
  4. .....
  5. @PostMapping(&quot;/savePet&quot;)
  6. public String savePet(@ModelAttribute(&quot;pet&quot;) Pet thePet) {
  7. System.out.println(&quot;New Pet &quot; + thePet.toString());
  8. thePet.setOwner(customerService.getCustomer(thePet.getOwner().getId()));
  9. System.out.println(&quot;Controller&quot; + thePet);
  10. // save the customer using our service
  11. petService.savePet(thePet);
  12. return &quot;redirect:/pet/showListPets&quot;;
  13. }

Any ideas what is happening?

Thanks in advance

答案1

得分: 0

我解决了我的问题,但是我不明白为什么,所以,如果有人能够友好地解释一下给我..那会更好:)
问题通过将BindingResult参数添加到控制器方法中得以解决:

  1. @PostMapping("/savePets")
  2. public String savePets(@ModelAttribute("pet") Pet thePet, BindingResult result) {
英文:

I resolved my question, however I dont understand why, so , if someone is so kind to explain me.. better Spring MVC无法调用控制器。
The problem was solved adding BindingResult paramenter to the controller method :

  1. @PostMapping(&quot;/savePets&quot;)
  2. public String savePets(@ModelAttribute(&quot;pet&quot;) Pet thePet, BindingResult result) {

Thanks

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

发表评论

匿名网友

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

确定