我正在尝试使用 spring3 hibernate3 和tiles2。
@RequestMapping(value = "/capturedetails", method = RequestMethod.GET)
public String getcapturedetails(Model model, HttpSession session,
HttpServletRequest request) {
Customer customer=new Customer();
model.addAttribute("customer", customer);
return "capturedetails";
}
@RequestMapping(value = "/capturedetails", method = RequestMethod.POST)
public String addcustomer(
@ModelAttribute("Customer") Customer customer, Model model,
HttpSession session, HttpServletRequest request) {
custBarcodeService.saveCustomer(customer);
model.addAttribute("customer ", new Customer());
return "capturedetails";
}
提交请求控制后,它总是选择 GET 方法而不是 POST... 我该如何解决这个问题?
最佳答案
我过去也遇到过类似的问题。就我而言,我试图从 postman 使用 json
主体向端点发出 POST 请求,该端点正在接收 x-www-form-urlencoded
格式的数据 Controller 侧。
请注意,如果您在后置 Controller 方法中使用@ModelAttribute
,它将接收x-www-form-urlencoded
格式的数据。如果是这种情况,那么可能的解决方案是
使用
@RequestBody
让post请求方法接收json
数据:@RequestMapping(value = "/capturedetails", method = RequestMethod.POST) public String addcustomer(@RequestBody Customer customer, Model model, HttpSession session, HttpServletRequest request) { custBarcodeService.saveCustomer(customer); model.addAttribute("customer ", new Customer()); return "capturedetails"; }
从 REST 客户端发送
x-www-form-urlencode
格式的数据
关于java - 提交请求时控件始终选择 GET 方法而不是 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42853548/