獲取SSM專案中所有的URL
阿新 • • 發佈:2019-01-08
1、在springmvc配置加上兩個bean:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
2、在controller注入RequestMappingHandlerAdapter:
@Autowired
RequestMappingHandlerMapping requestMappingHandlerMapping;
3、controller新增mapping:
@RequestMapping("/index.action")
@ResponseBody
public Object index(HttpServletRequest request) {
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);
}
HashMap<String, Object> result = new HashMap<String, Object>();
result.put("str", urlList);
return result;
}
注:如果例項化RequestMappingHandlerMapping出錯,大概是你專案不僅僅只有一個例項,此時請為你寫的bean設定id