【发布时间】:2012-09-20 18:53:13
【问题描述】:
使用 Spring 3.1.2
如何从 jsp 中引用 CONTROLLER(不是方法)RequestMapping 的注释值,以便我可以构建相对于控制器的 URL。如果引用了方法级别的请求映射,则会导致我的链接不起作用,因此它们无论如何都不能成为 this 的一部分。
例如(注意这个控制器的映射是“/foo/test”):
@Controller
@RequestMapping("/foo/test")
public class FooController {
@RequestMapping(value="/this", method=RequestMethod.GET)
public String getThis() {
return "test/this";
}
@RequestMapping(value="/that", method=RequestMethod.GET)
public String getThat() {
return "test/that";
}
@RequestMapping(value="/other", method=RequestMethod.GET)
public String getOther() {
return "test/other";
}
}
...以及来自我的 this.jsp:
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<s:url value="${controllerRequestMapping}" var="baseUrl"/>
<a href="${baseUrl}/that">That</a>
<a href="${baseUrl}/other">Other</a>
我需要访问 RequestMapping 的原因是因为我的视图可以从多个控制器访问。注意这个控制器的映射是“/bar/test”!
@Controller
@RequestMapping("/bar/test")
public class BarController {
@RequestMapping(value="/this", method=RequestMethod.GET)
public String getThis() {
return "test/this";
}
@RequestMapping(value="/that", method=RequestMethod.GET)
public String getThat() {
return "test/that";
}
@RequestMapping(value="/other", method=RequestMethod.GET)
public String getOther() {
return "test/other";
}
}
我的一些控制器级别的请求映射也有路径变量,所以只获取请求映射的字符串值是行不通的。必须解决:
@Controller
@RequestMapping("/something/{anything}/test/")
public class SomethingController {
...
}
更新
也许如果有一种方法可以通过在 Spring URL 标记之前附加控制器请求映射来修改上下文路径,那将解决问题。
contextPath = contextPath/controllerRequestMapping
然后,我可以这样做,因为我相信 Spring 会自动检索当前上下文路径:
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<a href="<s:url value="/that"/>">That</a>
<a href="<s:url value="/other"/>">Other</a>
这当然是最佳选择!想法?想法...?
提前致谢!
【问题讨论】:
标签: java spring spring-mvc