使用springmvc提交put请求时提示 Request method 'POST' not supported

来源:互联网 发布:腾讯mac管家 编辑:程序博客网 时间:2024/06/05 23:00

出错时编写的代码如下:

①web.xml

  <!-- 配置springmvc的 DispatcherServlet ctrl+alt+向上键 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 配置 :把POST请求转为DELETE、PUT请求 ctrl+shift+t -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
</web-app>

②请求界面

<form:form action="emp" method="post" modelAttribute="employee">
<!-- path属性对应name属性值 -->
<c:if test="${employee.id == null }">
LastName:<form:input path="lastName"/>
</c:if>
<c:if test="${employee.id != null }">
<form:hidden path="id"/>
<!-- 对于_method不能使用form:hiddden 标签,因为modelAttribute中没有_method这个属性 -->
<input type="hidden" name="_method" value="put"/>
</c:if>

...........

</form>

③处理器

@ModelAttribute
public void getEmployee(@RequestParam(value="id",required=false) Integer id,
Map<String,Object> map){
if(id != null){ 
map.put("employee", employeeDao.get(id));
}
}

@RequestMapping(value="/emp", method=RequestMethod.PUT)
public String update(Employee employee){
employeeDao.save(employee);
return "redirect:/emps";
}

⑥以上完成后运行时出现错误提示 Request method 'POST' not supported

这是因为请求页面的form标签的action不是绝对路径导致的,因此,在编写时,应尽量写成绝对路径!!!

<form:form action="${pageContext.request.contextPath }/emp" method="post" modelAttribute="employee">

0 0
原创粉丝点击