【发布时间】:2017-12-14 19:03:19
【问题描述】:
如上,如何传递参数?我已成功地将应用程序中上一页的 ID 参数传递到包含表单的页面的 URI。然后我想将此表单的内容连同相同的 ID 参数一起发布到我的数据库中。
我在我的应用程序中使用 Spring Security,我怀疑这可能是问题所在,因为如果我不尝试传递 ID 参数(即将我的表单数据对象发送到服务),我的表单的 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;
}
【问题讨论】:
标签: java spring model-view-controller spring-boot spring-security