springmvc Restful风格

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添加隐藏域那么繁琐。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值