![](/img/trans.png)
[英]How to pass service bean into spring boot controller using xml configuration
[英]How to pass parameters in Spring Boot controller class (app is using Spring Security)
如上所述,如何傳遞參數? 我已成功將ID參數從應用程序中的上一頁傳遞到包含表單的頁面的URI。 然后,我想發布此表單的內容以及相同的ID參數,以保存到我的數據庫中。
我正在我的應用程序中使用Spring Security,我懷疑這可能是問題所在,因為如果我不嘗試傳遞ID參數(即,將表單數據對象發送到Service類),則表單的POST方法將按預期工作成功將數據保存到數據庫)。 但是,一旦我添加了也要傳遞ID參數的代碼,就會收到HTTP 405錯誤(“不支持請求方法'POST'”)。
我的網頁的URI,其中包含以下格式: http:// localhost:8080 / acceptOffer?id =(idvalue)
形成:
<form autocomplete="off" action="#" th:action="@{/acceptOffer}"
th:object="${offer}" method="POST" class="form-horizontal"
role="form">
Note
<label th:if="${#fields.hasErrors('note')}" th:errors="*{note}"
class="validation-message"></label>
<input type="text" th:field="*{note}" placeholder="Type Here"
class="form-control" />
<button type="submit">Accept Offer</button>
</form>
控制器方式:
@RequestMapping(value={"/acceptOffer?id={id}"}, method = RequestMethod.POST)
public ModelAndView acceptOffer(@Valid Offer offer, @PathVariable String id, BindingResult bindingResult){
ModelAndView modelAndView = new ModelAndView();
offerService.setId(Integer.valueOf(id));
offerService.saveOffer(offer);
modelAndView.addObject("successMessage", "Your offer of acceptance has been received");
modelAndView.addObject("user", new User());
modelAndView.setViewName("acceptOffer");
return modelAndView;
}
如果使用路徑變量,則將控制器代碼更改為
@RequestMapping(value={"/acceptOffer/{id}"}, method = RequestMethod.POST)
public ModelAndView acceptOffer(@Valid Offer offer, @PathVariable String id, BindingResult bindingResult){
ModelAndView modelAndView = new ModelAndView();
offerService.setId(Integer.valueOf(id));
offerService.saveOffer(offer);
modelAndView.addObject("successMessage", "Your offer of acceptance has been received");
modelAndView.addObject("user", new User());
modelAndView.setViewName("acceptOffer");
return modelAndView;
}
並稱它為
我沒有按照最初打算的方式解決此問題,但通過以下方法解決了該問題:
我知道這不太理想,但是可以用...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.