如何在视图中显示所有控制器和映射 [英] How to show all controllers and mappings in a view
问题描述
我有一个非标准的Spring MVC项目.用XML响应.是否可以创建一个视图(jsp页面),以显示所有接受的(必需的和不是必需的)控制器,映射和参数.
I have a none standard Spring MVC project. Responding with XMLs. Is it possible to create a view (jsp page) showing all controllers, mappings and parameters that are accepted (required and not).
基于答案,我有:
@RequestMapping(value= "/endpoints", params="secure", method = RequestMethod.GET)
public @ResponseBody
String getEndPointsInView() {
String result = "";
for (RequestMappingInfo element : requestMappingHandlerMapping.getHandlerMethods().keySet()) {
result += "<p>" + element.getPatternsCondition() + "<br>";
result += element.getMethodsCondition() + "<br>";
result += element.getParamsCondition() + "<br>";
result += element.getConsumesCondition() + "<br>";
}
return result;
}
我没有从@RequestParam
推荐答案
使用Spring 3.1中的RequestMappingHandlerMapping
,您可以轻松浏览端点.
With RequestMappingHandlerMapping
in Spring 3.1, you can easily browse the endpoints.
控制器:
@Autowire
private RequestMappingHandlerMapping requestMappingHandlerMapping;
@RequestMapping( value = "endPoints", method = RequestMethod.GET )
public String getEndPointsInView( Model model )
{
model.addAttribute( "endPoints", requestMappingHandlerMapping.getHandlerMethods().keySet() );
return "admin/endPoints";
}
视图:
<%@ page session="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head><title>Endpoint list</title></head>
<body>
<table>
<thead>
<tr>
<th>path</th>
<th>methods</th>
<th>consumes</th>
<th>produces</th>
<th>params</th>
<th>headers</th>
<th>custom</th>
</tr>
</thead>
<tbody>
<c:forEach items="${endPoints}" var="endPoint">
<tr>
<td>${endPoint.patternsCondition}</td>
<td>${endPoint.methodsCondition}</td>
<td>${endPoint.consumesCondition}</td>
<td>${endPoint.producesCondition}</td>
<td>${endPoint.paramsCondition}</td>
<td>${endPoint.headersCondition}</td>
<td>${empty endPoint.customCondition ? "none" : endPoint.customCondition}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
您也可以使用Spring< 3.1,使用DefaultAnnotationHandlerMapping
而不是RequestMappingHandlerMapping
.但是您不会获得相同级别的信息.
You can also do this with Spring < 3.1, with DefaultAnnotationHandlerMapping
instead of RequestMappingHandlerMapping
. But you won't have the same level of information.
使用DefaultAnnotationHandlerMapping
,您将只有端点路径,而没有有关其方法,消耗,参数...的信息.
With DefaultAnnotationHandlerMapping
you will only have the endpoints path, without information about their methods, consumes, params...
这篇关于如何在视图中显示所有控制器和映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!