springmvc Restful风格
@Controller
@RequestMapping(value = "/agreement")
public class AgreementTemplateController {
@RequestMapping(value="/agreementTemplate", method=RequestMethod.GET)
public String add(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
System.out.println("---GET---");
return "/agreement/agreementTemplateAdd";
}
@RequestMapping(value="/agreementTemplate", method=RequestMethod.POST)
public String save(AgreementTemplate agreementTemplate, HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
System.out.println("---POST---");
return "redirect:/agreementTemplateList";
}
@RequestMapping(value="/agreementTemplate/{agreementNo}", method=RequestMethod.DELETE)
public String save(@PathVariable(value="agreementNo") String agreementNo, HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
System.out.println("---DELETE---");
return "redirect:/agreementTemplateList";
}
}
以上,Controller。
<div><a href="agreement/agreementTemplate">add</a></div>
以上,get提交。
<form id="agreementTemplateAddForm" method="post" action="agreementTemplate">
<div><button type="submit">Add</button></div>
</form>
以上,post提交。
<html>
<head>
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
function deleteAgreementTemplate(agreementNo) {
var form = $("#deleteAgreementTemplateForm");
form.attr("action","agreementTemplate/" + agreementNo);
form.submit();
}
</script>
</head>
<body>
<div>
<form id="deleteAgreementTemplateForm" action="" method="post">
<input type="hidden" name="_method" value="DELETE" />
<div><input type="button" onclick="deleteAgreementTemplate('${agreementTemplate.agreementNo}');" value="Delete" />
</form>
</div>
</body>
</html>
以上,form提交只能是get或post,所以delete提交需要带一个hidden隐藏域,name为“_mothod“,value为delete类型告知后台实际上是delete请求。
<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.xml进行配置HiddenHttpMethodFilter。
/* */ public class HiddenHttpMethodFilter
/* */ extends OncePerRequestFilter
/* */ {
/* */ public static final String DEFAULT_METHOD_PARAM = "_method";
/* */ private String methodParam;
/* */
/* */ public HiddenHttpMethodFilter()
/* */ {
/* 54 */ methodParam = "_method";
/* */ }
/* */
/* */ public void setMethodParam(String methodParam)
/* */ {
/* 62 */ Assert.hasText(methodParam, "'methodParam' must not be empty");
/* 63 */ this.methodParam = methodParam;
/* */ }
/* */
/* */
/* */ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
/* */ throws ServletException, IOException
/* */ {
/* 70 */ String paramValue = request.getParameter(methodParam);
/* 71 */ if (("POST".equals(request.getMethod())) && (StringUtils.hasLength(paramValue))) {
/* 72 */ String method = paramValue.toUpperCase(Locale.ENGLISH);
/* 73 */ HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);
/* 74 */ filterChain.doFilter(wrapper, response);
/* */ }
/* */ else {
/* 77 */ filterChain.doFilter(request, response);
/* */ }
/* */ }
/* */ }
以上,HiddenHttpMethodFilter的doFilterInternal的处理。
PUT请求和DELETE请求处理方法相同,当然也可以使用Ajax提交,这样可以直接提交PUT和DELETE请求实现restful风格,而不用如同上述中配置web.xml添加隐藏域那么繁琐。