我正在尝试使用 POST 将表单连接到另一个页面,但我一直收到错误:
Request method 'POST' not supported
Controller 中的 handleNext 方法如下所示:
@RequestMapping(value = PAGE_NAME, method = RequestMethod.POST)
public String handleNext(ModelMap map, HttpServletRequest request,
@ModelAttribute("indexBacking") IndexBacking bo, BindingResult result) {
return "redirect:/" + GameController.PAGE_NAME;
}
Game Controller 上的 GET 方法如下所示:
@RequestMapping(value = PAGE_NAME, method = RequestMethod.GET)
public String handleBasicGet(ModelMap map, HttpServletRequest request) {
return MODEL_NAME;
}
我的 Web.xml 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
任何想法都会很棒。
更新 index.jsp 具有以下对支持对象的引用
<form:form action="index.htm" enctype="multipart/form-data"
method="post" commandName="indexBacking" accept-charset="UTF-8">
<table id="main">
${page_contents}
<tr>
<td></td>
<td>
<spring:bind path="name">
<form:input path="name" value="Name" />
<form:errors cssClass="vmessage"
element="div" path="name" />
</spring:bind>
</td>
<td>
<input type="submit" value="Submit" />
</td>
<td></td>
</tr>
</table>
</form:form>
并且支持对象似乎没有在此处设置名称字段:
public class IndexBacking {
private String name;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
最佳答案
Request method 'POST' not supported
当以下事情出错时,这个错误 spring 会抛出:
- HTML 表单操作有一些 URL,其方法为
POST
并且您没有任何 Controller 方法来处理对该 URL 的 POST 请求。 - 缺失或无效
@ModelAttribute
在 POST 处理程序方法中。
来到
the backing object doesn't seem to set the name field here:
内部<spring:bind
你应该使用 status
像这样的对象:
-
status.value
:获取bean或属性的实际值。 -
status.expression
: 用于检索 bean 或属性的表达式。 -
status.errorMessages
:一组错误消息,由验证产生
并且无需使用 <spring:bind..
当你使用 <form:form..
标签,两者几乎做同样的工作,而 <spring:bind..
用于执行一些自定义操作。所以选择<form:sorm..
中的任何一个& <spring:bind..
在您的情况下,您可以使用 <spring:bind..
解决问题喜欢:
<c:url value="/indexBackingUrl" var="pstUrl"/>
<form action="${pstUrl}" method="post" >
<table id="main">
<tr>
<td></td>
<td>Name:
<spring:bind path="indexBacking.name">
<input type="text"
name="${status.expression}"
value="${status.displayValue}"/>
<c:if test="${status.error}">
Error codes:
<c:forEach items="${status.errorMessages}" var="error">
<c:out value="${error}"/>
</c:forEach>
</c:if>
</spring:bind>
</td>
<td>
<input type="submit" value="Submit" />
</td>
<td></td>
</tr>
</table>
</form>
注意: name
属性在输入元素中很重要,因为 spring 使用输入元素的名称绑定(bind) bean 属性值。
或使用<form:form..
没有<spring:bind..
喜欢:
<form:form action="${pstUrl}" method="post" modelAttribute="indexBacking">
<form:label path="name">Name:</form:label>
<form:input path="name"/>
<form:errors path="name" element="div"/>
</form:form>
并在 Controller 的 GET 处理程序方法中添加 IndexBacking
键为 indexBacking
的 bean 实例制作作品<spring:bind..
在 jsp 中,如:
@RequestMapping(value="/indexBackingUrl", method = RequestMethod.GET)
public String handleNext(Model model) {
model.addAttribute("indexBacking", new IndexBacking("Jon"));
return "indexBacking";
}
Controller 中的 POST 处理程序方法如下所示:
@RequestMapping(value="/indexBackingUrl", method = RequestMethod.POST)
public String handleNextPost(ModelMap map, HttpServletRequest request,
@ModelAttribute("indexBacking") IndexBacking bo, BindingResult result) {
System.out.println(bo);
return "redirect:/someOtherUrl";
}
编辑:当您使用 enctype="multipart/form-data"
时在表格中,那么你应该注册CommonsMultipartResolver
bean 和commons-io-x.x.jar
& commons-fileupload-x.x.x.jar
应该可以用来制作 Spring 绑定(bind),否则你会得到 null
值,在你的情况下 enctype="multipart/form-data"
不需要,因为您不使用任何文件上传等。
另请参阅:
关于java - 不支持发布方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23203215/