Spring Mvc Url和參數名稱忽略大小寫

在開發過程當中Spring Mvc 默認 Url和參數名稱都是區分大小寫的

  好比:www.a.com/user/getUserInfo?userId=1 java

     www.a.com/user/getuserInfo?userId=1 web

       www.a.com/user/getUserInfo?userid=1 spring

            www.a.com/user/getuserinfo?userid=1 app

  這些都認爲不一樣的地址和參數,在實際中用戶根本不區分這些,因此咱們要忽略大小寫ide

URL忽略大小寫

import org.springframework.context.annotation.Configuration;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * Created by tianwei on 2017/6/22.
 */
@Configuration
public class SpringWebConfig extends WebMvcConfigurationSupport {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        AntPathMatcher pathMatcher = new AntPathMatcher();
        pathMatcher.setCaseSensitive(false);
        configurer.setPathMatcher(pathMatcher);
    }

}

參數名稱忽略大小寫

import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.web.filter.OncePerRequestFilter;

public class CaseInsensitiveRequestParameterNameFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        filterChain.doFilter(new CaseInsensitiveParameterNameHttpServletRequest(request), response);
    }

    public static class CaseInsensitiveParameterNameHttpServletRequest extends HttpServletRequestWrapper {
        private final LinkedCaseInsensitiveMap<String[]> map = new LinkedCaseInsensitiveMap<>();

        @SuppressWarnings("unchecked")
        public CaseInsensitiveParameterNameHttpServletRequest(HttpServletRequest request) {
            super(request);
            map.putAll(request.getParameterMap());
        }

        @Override
        public String getParameter(String name) {

            String[] array = this.map.get(name);
            if (array != null && array.length > 0)
                return array[0];
            return null;
        }

        @Override
        public Map<String, String[]> getParameterMap() {
            return Collections.unmodifiableMap(this.map);
        }

        @Override
        public Enumeration<String> getParameterNames() {
            return Collections.enumeration(this.map.keySet());
        }

        @Override
        public String[] getParameterValues(String name) {
            return this.map.get(name);
        }

    }

}

定義Bean

    <!--輸入參數忽略大小寫-->
    <bean id="caseInsensitiveRequestFilterProxy" class="org.springframework.web.filter.DelegatingFilterProxy">
        <property name="targetBeanName" value="caseInsensitiveRequestFilter"/>
    </bean>
    <bean id="caseInsensitiveRequestFilter"
          class="com.hantianwei.util.CaseInsensitiveRequestParameterNameFilter">
    </bean>

web.xml 增長Filter

  <filter>
    <filter-name>caseInsensitiveRequestFilterProxy</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>caseInsensitiveRequestFilterProxy</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

到此再次運行項目就能夠了,最上面的URL訪問的是同一頁面了this

相關文章
相關標籤/搜索