葡京娱乐开元棋牌_开元棋牌靠谱吗_f1娱乐开元棋牌先锋网 葡京娱乐开元棋牌_开元棋牌靠谱吗_f1娱乐开元棋牌片段及技术文章聚合

Spring MVC映射 *.do过滤


    contextConfigLocation
    classpath:applicationContext*.xml



  	org.springframework.web.context.ContextLoaderListener



	springMvc
	org.springframework.web.servlet.DispatcherServlet
	  
	      spring mvc 配置文件
	      contextConfigLocation
	      
	    
        1 


      springMvc
      *.do

上面是简单的Spring的xml配置,这样配置的好处是Spring框架只会拦截xxx.do这样的url,想.css , .js ,  .jpg等静态资源不回被拦截

我们试着在Controller里面写控制类

@Controller
public class First {

	@RequestMapping("/home.do")
	public String home12(HttpServletRequest request) {
		System.out.println(request.getRequestURL());

		return "ok";
	}
}

我们发现可以正常请求

@Controller
public class First {

	@RequestMapping("/home")
	public String home12(HttpServletRequest request) {
		System.out.println(request.getRequestURL());

		return "ok";
	}
}

我们发现@RequestMapping("/home")这样也是可以请求到的

 我就想去看看到底Spring 做了什么

跟了半天源葡京娱乐开元棋牌_开元棋牌靠谱吗_f1娱乐开元棋牌最后在org.springframework.web.servlet.mvc.condition.PatternsRequestCondition 

这个类中发现,Spring做了一个特殊的处理

pattern:/home   (@RequestMapping("/home"))	 
lookupPath:/home.do  前台请求

private String getMatchingPattern(String pattern, String lookupPath) {
		if (pattern.equals(lookupPath)) {
			return pattern;
		}
                //useSuffixPatternMatch:true
		if (this.useSuffixPatternMatch) {
			if (!this.fileExtensions.isEmpty() && lookupPath.indexOf('.') != -1) {
				for (String extension : this.fileExtensions) {
					if (this.pathMatcher.match(pattern + extension, lookupPath)) {
						return pattern + extension;
					}
				}
			}
			else {
				boolean hasSuffix = pattern.indexOf('.') != -1;
				if (!hasSuffix && this.pathMatcher.match(pattern + ".*", lookupPath)) {
					return pattern + ".*";
                                  //pattern:最后会变为/home.*    最后在去匹配控制层@RequestMapping("/home.*")的值
                                  //所以以后再写controller层 映射时可省略后面.do

				}
			}
		}
		if (this.pathMatcher.match(pattern, lookupPath)) {
			return pattern;
		}
		if (this.useTrailingSlashMatch) {
			if (!pattern.endsWith("/") && this.pathMatcher.match(pattern + "/", lookupPath)) {
				return pattern +"/";
			}
		}
		return null;
	}

原文地址:Spring MVC映射 *.do过滤