SpringMVC之映射到方法

Posted on 2016-06-14 00:18  上善其若水,厚德载物  阅读(474)  评论(0编辑  收藏  举报

springMVC实例一

请求中要包含name,但是不能包含age的写法params={"name","!age"}

当传age的时候

实例二:

先看删除和更新

HiddenHttpMethodFilter在Spring3.0中将post请求转换为put和delete请求,

查看HiddenHttpMethodFilter源码

@Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {

        String paramValue = request.getParameter(this.methodParam);
        if ("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) {
            String method = paramValue.toUpperCase(Locale.ENGLISH);
            HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);
            filterChain.doFilter(wrapper, response);
        }
        else {
            filterChain.doFilter(request, response);
        }
    }
public static final String DEFAULT_METHOD_PARAM = "_method";

    private String methodParam = DEFAULT_METHOD_PARAM;

所以把name为_method的属性设置为DELETE和PUT就可以了

在web.xml中配置HiddenHttpMethodFilter,要写在DispatcherServlet前面

查询和保存:

运行页面:

Copyright © 2024 上善其若水,厚德载物
Powered by .NET 8.0 on Kubernetes