![](/img/trans.png)
[英]Spring MVC (Neither BindingResult nor plain target object for bean name) after Form Validation error
[英]Spring “Neither BindingResult nor plain target object for bean name” error when submitting form on view with multiple forms
目前,我遇到了一個令人討厭的問題,無法找到擺脫它的方法。 我在我的項目中使用Thymeleaf和Spring Boot。 我在這里和其他論壇上瀏覽各種主題,但沒有運氣解決上述問題。 我有一個視圖,其中包含兩個而不是嵌套的表單,分別帶有提交按鈕和單獨的操作:
<form id="bidForm" th:action="@{'/auction/bid/' + ${auctionDto.id}}"
th:object="${bidDto}" method="post">
/* more html code here */
</form>
<form id="buyoutForm" th:action="@{'/buyout/confirm-buyout'}"
th:object="${buyoutDto}" method="post">
/* more html code here */
</form>
該視圖是通過以下方法從控制器生成的:
@GetMapping("/get/{id}")
public ModelAndView getAuction(@PathVariable String id) {
ModelAndView modelAndView = new
ModelAndView(Templates.AuctionTemplates.ITEM);
modelAndView.addObject("auctionDto", getAuctionService.getOne(id));
modelAndView.addObject("bidDto", new BidDto());
modelAndView.addObject("buyoutDto", new BuyoutDto());
return modelAndView;
}
th:action中指定的方法映射在兩個不同的控制器中。 具有“ bidForm”動作的控制器:
@Controller
@RequestMapping("/auction")
public class AuctionController {
@PostMapping("/bid/{id}")
public ModelAndView bid(@PathVariable String id,
@ModelAttribute("bidDto")
@Valid BidDto bidDto, BindingResult bindingResult, Principal principal) {
/* more code here */
}
}
對於“ buyoutForm”:
@Controller
@RequestMapping("/buyout")
public class BuyoutController {
@PostMapping("/confirm-buyout")
public ModelAndView confirmBuyout(@Valid @ModelAttribute("buyoutDto")
BuyoutDto buyoutDto) {
ModelAndView modelAndView = new ModelAndView();
return modelAndView;
}
}
現在,問題來了:當我使用相應的按鈕提交“ bidForm”時,出現錯誤:
Neither BindingResult nor plain target object for bean name "buyoutDto"
我不知道為什么Spring在“ bid”方法中期望“ BuyoutDto”類的對象。 這是我第一次遇到這個問題。 你能幫我解決嗎? 問候,盧卡斯。
我認為發生此錯誤是因為在您的出價方法中,您沒有返回對象buyoutDto。 從此方法返回時,是否會在同一頁面(具有兩種形式的頁面)中再次進入?
例如,您的代碼應如下所示
@PostMapping("/bid/{id}")
public ModelAndView bid(@PathVariable String id,
@ModelAttribute("bidDto")
@Valid BidDto bidDto, BindingResult bindingResult, Principal principal) {
/* more code here */
modelAndView.addObject("buyoutDto", new BuyoutDto());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.