我直接
<form action="updateUser" method="put">
<input type="hidden" name="id" value="${company.id }"/>
账号:<input type="text" name="username" value="${company.username }"/><p/>
状态:<input type="text" name="status" value="${company.status }"/><p/>
<input type="submit" value="修改"/>
</form>
tomcat 报错404,
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
后来才知道,form不支持put。
解决办法:
在web.xml增加过滤器
<!-- 增加HiddenHttpMethodFilte过滤器:给普通浏览器增加 put|delete请求方式 -->
<filter>
<filter-name>HiddenHttpMethodFilte</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilte</filter-name>
<!-- 过滤所有:/* -->
<url-pattern>/*</url-pattern>
</filter-mapping>
前端调用接口的时候,多增加一个_method属性,即可
<form action="updateUser" method="post">
<input type="hidden" name="_method" value="put" />
<input type="hidden" name="id" value="${company.id }"/>
账号:<input type="text" name="username" value="${company.username }"/><p/>
状态:<input type="text" name="status" value="${company.status }"/><p/>
<input type="submit" value="修改"/>
</form>
controller代码:
@PutMapping("/updateUser")
@ResponseBody
public ModelAndView updateUser(HttpServletResponse response,User user) {
ModelAndView modelAndView = new ModelAndView();
userService.updateUser(user);
modelAndView.setViewName("redirect:/listAll");
return modelAndView;
}