你的位置:首页 > ASP.net教程

[ASP.net教程]spring mvc 获取所有的controller和url映射关系


有时候需要根据url反查controller,如果能获取所有的url,则不用跟据url去代码里搜了,方便开发人员、调试人员或交接人。

关键对象:RequestMappingHandlerMapping 

Java代码 复制代码 收藏代码

  1. import java.util.ArrayList;  
  2. import java.util.HashMap;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.ui.Model;  
  9. import org.springframework.web.bind.annotation.RequestMapping;  
  10. import org.springframework.web.method.HandlerMethod;  
  11. import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;  
  12. import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;  
  13. import org.springframework.web.servlet.mvc.method.RequestMappingInfo;  
  14. import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;  
  15.   
  16. @Controller  
  17. public class MappingController {  
  18.   
  19.     @Autowired  
  20.     private RequestMappingHandlerMapping requestMappingHandlerMapping;  
  21.   
  22.     @RequestMapping(value = "/mappings")  
  23.     public String list(Model model) {  
  24.         List<HashMap<String, String>> urlList = new ArrayList<HashMap<String, String>>();  
  25.   
  26.         Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();  
  27.         for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {  
  28.             HashMap<String, String> hashMap = new HashMap<String, String>();  
  29.             RequestMappingInfo info = m.getKey();  
  30.             HandlerMethod method = m.getValue();  
  31.             PatternsRequestCondition p = info.getPatternsCondition();  
  32.             for (String url : p.getPatterns()) {  
  33.                 hashMap.put("url", url);  
  34.             }  
  35.             hashMap.put("className", method.getMethod().getDeclaringClass().getName()); // 类名  
  36.             hashMap.put("method", method.getMethod().getName()); // 方法名  
  37.             RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();  
  38.             String type = methodsCondition.toString();  
  39.             if (type != null && type.startsWith("[") && type.endsWith("]")) {  
  40.                 type = type.substring(1, type.length() - 1);  
  41.                 hashMap.put("type", type); // 方法名  
  42.             }  
  43.             urlList.add(hashMap);  
  44.         }  
  45.         model.addAttribute("list", urlList);  
  46.         return "/console/system/mappingList";  
  47.     }  
  48.   
  49. }  

import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;import org.springframework.web.servlet.mvc.method.RequestMappingInfo;import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;@Controllerpublic class MappingController {	@Autowired	private RequestMappingHandlerMapping requestMappingHandlerMapping;	@RequestMapping(value = "/mappings")	public String list(Model model) {		List<HashMap<String, String>> urlList = new ArrayList<HashMap<String, String>>();		Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();		for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {			HashMap<String, String> hashMap = new HashMap<String, String>();			RequestMappingInfo info = m.getKey();			HandlerMethod method = m.getValue();			PatternsRequestCondition p = info.getPatternsCondition();			for (String url : p.getPatterns()) {				hashMap.put("url", url);			}			hashMap.put("className", method.getMethod().getDeclaringClass().getName()); // 类名			hashMap.put("method", method.getMethod().getName()); // 方法名			RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();			String type = methodsCondition.toString();			if (type != null && type.startsWith("[") && type.endsWith("]")) {				type = type.substring(1, type.length() - 1);				hashMap.put("type", type); // 方法名			}			urlList.add(hashMap);		}		model.addAttribute("list", urlList);		return "/console/system/mappingList";	}}

 然后再在页面上遍历list即可【java框架源码下载】

Java代码 复制代码 收藏代码

  1. <table class="tableList" >  
  2. <tr>  
  3.         <th>类名</th>  
  4.         <th>方法名</th>  
  5.         <th>URL</th>  
  6.         <th>类型</th>  
  7. <tr>  
  8.     <c:forEach items="${list}" var="mvc" varStatus="status">  
  9.     <tr id="${status.index}">  
  10.         <td>${mvc.className}</td>  
  11.         <td>${mvc.method}</td>  
  12.         <td>  
  13.             <c:choose>  
  14.             <c:when test="${!fn:contains(mvc.url,'}') and (mvc.type=='GET' or mvc.type=='')}">  
  15.                 <a href="${ctx}${mvc.url}" target="_blank">${mvc.url}</a>  
  16.             </c:when>  
  17.             <c:otherwise>${mvc.url}</c:otherwise>  
  18.             </c:choose>  
  19.         </td>  
  20.         <td>${mvc.type}</td>  
  21.     </tr>  
  22.     </c:forEach>  
  23. </table>  

	<table >	<tr>			<th>类名</th>			<th>方法名</th>			<th>URL</th>			<th>类型</th>	<tr>		<c:forEach items="${list}" var="mvc" varStatus="status">		<tr id="${status.index}">			<td>${mvc.className}</td>			<td>${mvc.method}</td>			<td>				<c:choose>				<c:when test="${!fn:contains(mvc.url,'}') and (mvc.type=='GET' or mvc.type=='')}">					<a href="${ctx}${mvc.url}" target="_blank">${mvc.url}</a>				</c:when>				<c:otherwise>${mvc.url}</c:otherwise>				</c:choose>			</td>			<td>${mvc.type}</td>		</tr>		</c:forEach>	</table>

 

 

    

2
1