spring mvc 获取所有的controller和url映射关系

187 篇文章 0 订阅
97 篇文章 0 订阅

转自: http://jayung.iteye.com/blog/2240864


有时候需要根据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. }  

 然后再在页面上遍历list即可

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>  

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值