我正在尝试使用POST将表单连接到另一个页面,但我一直收到错误:
Request method 'POST' not supported
我在控制器中的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控制器上的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>
任何想法都会很棒。
UPDATE 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;
}
}
答案 0 :(得分:2)
我认为你是从handleNext控制器重定向到url 使用post方法但目标控制器(重定向消息的那个) 只有一个Get方法, 所以改变得到发布内部游戏控制器,然后再试一次。 如有任何问题,请告诉我。
答案 1 :(得分:2)
如果您正在使用return&#34;重定向:/&#34; + GameController.PAGE_NAME;到另一个控制器然后指定该控制器的完整URL路径。现在,您将重定向到同一个控制器而不是另一个控制器。
答案 2 :(得分:2)
不支持请求方法'POST'
当出现错误时,春天会抛出此错误:
POST
,并且您没有任何控制器方法来处理对该URL的POST请求。@ModelAttribute
。来
支持对象似乎没有在此处设置名称字段:
在<spring:bind
内你应该使用status
对象,如:
status.value
:获取bean或属性的实际值。status.expression
:用于检索bean或属性的表达式。status.errorMessages
:由验证产生的错误消息数组使用<spring:bind..
标记时无需使用<form:form..
,两者几乎完成相同的工作,而<spring:bind..
则用于执行某些自定义操作。因此,请在<form:sorm..
&amp;中选择一个。 <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
属性在input元素中很重要,因为spring使用input元素的名称绑定bean属性值。
或使用不<form:form..
的{{1}}:
<spring:bind..
并在Controller的GET处理程序方法中添加<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>
bean实例,密钥为IndexBacking
,以便在jsp中创建工作indexBacking
,如下所示:
<spring:bind..
控制器中的和POST处理程序方法如下所示:
@RequestMapping(value="/indexBackingUrl", method = RequestMethod.GET)
public String handleNext(Model model) {
model.addAttribute("indexBacking", new IndexBacking("Jon"));
return "indexBacking";
}
修改:在表单中使用@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"
bean和CommonsMultipartResolver
&amp; commons-io-x.x.jar
应该可用于弹出绑定工作,否则您将获得commons-fileupload-x.x.x.jar
值,在您的情况下null
不需要,因为您没有使用任何文件上传等。
<小时/> 另见: