我正在使用Spring MVC 3来设置和处理表单,但是我遇到了这个问题,这是我在使用Spring MVC 3提交表单时从未遇到的问题,我无法理解其背后的原因。
当我提交表单时,我得到 HTTP状态405 - 请求方法'POST'不支持错误。 以下是我用来实现它的.jsp表单和控制器。
newProblem.jsp
<form:form commandName="problem" >
<form:label path="problem" >
<span class="label">Problem <form:errors path="problem" /></span>
</form:label>
<form:textarea path="problem" cssClass="editor" id="editor"/>
<input type="submit" value="create" />
</form:form>
ProblemManagementController.java
//Add new problem (GET)
@RequestMapping(method=RequestMethod.GET, value="/admin/problems/new")
public String setUpForm(Model model) {
model.addAttribute("problem", new Problem());
return "newProblem";
}
//Add new problem (POST)
@RequestMapping(method=RequestMethod.POST, value="/admin/problems/new")
public String processForm(@Valid Problem problem, Errors result) {
if(result.hasErrors()) {
return "newProblem";
}
problemManagementService.createProblem(problem);
return "redirect:/admin/problems";
}
//Display all problems
@RequestMapping(value="/admin/problems")
public String displayAllProblems(Model model) {
List<Problem> allProblems = problemManagementService.getAllProblems();
model.addAttribute("allProblems", allProblems);
return "allProblems";
}
有人可以帮我理解为什么会收到此错误?
答案 0 :(得分:0)
您忘记在<form:form>
代码中添加操作。
试试这个:
<form:form action="admin/problems/new" commandName="problem" >
<form:label path="problem" >
<span class="label">Problem <form:errors path="problem" /></span>
</form:label>
<form:textarea path="problem" cssClass="editor" id="editor"/>
<input type="submit" value="create" />
</form:form>