重写DispatcherServlet获取springmvc 所有RequestMapping的url_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 重写DispatcherServlet获取springmvc 所有RequestMapping的url

重写DispatcherServlet获取springmvc 所有RequestMapping的url

 2018/1/9 12:16:20  hbxflihua  程序员俱乐部  我要评论(0)
  • 摘要:1、重写DispatcherServletimportjava.util.HashSet;importjava.util.Map;importjava.util.Set;importorg.springframework.beans.factory.BeanFactoryUtils;importorg.springframework.context.ApplicationContext;importorg.springframework.web.method.HandlerMethod
  • 标签:MVC Map URL Servlet 获取Spring Spring APP

?

1、重写DispatcherServlet

class="java">
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@SuppressWarnings("serial")
public class MyDispatcherServlet extends DispatcherServlet {
	
	public static final Set<String> MAPPING_URLS = new HashSet<>();
	
	@Override
	protected void initStrategies(ApplicationContext context) {
		super.initStrategies(context);
		
		Map<String, HandlerMapping> matchingBeans =
				BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
		for(HandlerMapping handlerMapping : matchingBeans.values()) {  
	        if(handlerMapping instanceof RequestMappingHandlerMapping) {  
	            RequestMappingHandlerMapping rmhm = (RequestMappingHandlerMapping) handlerMapping;  
	            Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();  
	            for(RequestMappingInfo rmi : handlerMethods.keySet()) {  
	                PatternsRequestCondition prc = rmi.getPatternsCondition();  
	                MAPPING_URLS.addAll(prc.getPatterns());
	            }  
	        }  
	    }  
		System.out.println(">>>>>>>>>>>>>>>>");
		for (String string : MAPPING_URLS) {
			System.out.println(string);
		}
		System.out.println(">>>>>>>>>>>>>>>>");
		
	}
	
}

?

2、在web.xml中配置重写的MyDispatcherServlet

	<!-- MVC Servlet -->
	<servlet>
		<servlet-name>springServlet</servlet-name>
		<servlet-class>com.huatech.web.listener.MyDispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:/spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springServlet</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>

?

发表评论
用户名: 匿名