POST方法没有被调用

时间:2015-10-26 22:19:20

标签: java spring

这是我的Spring控制器:

@Controller
public class ReportsController {

  @RequestMapping(value = "/reports.html", method = RequestMethod.GET)
  public String getReportsPage(HttpServletRequest request, HttpServletResponse response, ModelMap map) {
    map.addAttribute("searchCriteria", new SearchCriteria());
    return "reports";
  }

  @RequestMapping(value = "/reports.html", method = RequestMethod.POST)
  public String generateReport(@ModelAttribute SearchCriteria searchCriteria, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
    log.info(searchCriteria.toString());
    return "reports";
  }

}

这是我的JSP文件中的表单:

<form:form modelAttribute="searchCriteria" class="form-inline">
<div class="form-group">
  <label for="startDate">Start Date</label>
 <form:input path="startDate" />
</div>
<div class="form-group">
  <label for="endDate">End Date</label>
  <form:input path="endDate" />
</div>
<button type="submit">Generate Report</button>
</form:form>

我尝试将methodaction添加到form标记并分别设置为posthttp://bjpeter.company.com:8080/appName/reports.html,但POST方法确实如此不被打电话。

每当我转到该页面时都会调用GET方法,这就是我想要的,但是当我提交表单时,我的控制器中的POST方法没有被调用,并且返回了一个空白页面。

任何想法都将不胜感激!

1 个答案:

答案 0 :(得分:0)

您需要在表单上正确设置操作。 检查一下: http://www.tutorialspoint.com/spring/spring_mvc_form_handling_example.htm

然后它会起作用。像这样:

<form:form method="POST" action="/reports">

此外,您需要指定一个View Resolver,配置前缀和后缀,从链接中阅读教程。

相关问题