我们如何使用Spring获取应用程序根目录的绝对路径?
我有一个可以在 http://serverA/m/ 或 http://serverA/mobile/。我有一个带有搜索表单的共享标头,需要转到 http://serverA/installationName /搜索。
但是,如果我使用
如果我使用
我尝试过
但它们似乎都不起作用。
:url value="search"> 和 & lt c:url value="/search">
给我“无法解析控制器 URL /search”,即使我有一个使用 @RequestMapping("/search") 定义的控制器
I have an app that may run at http://serverA/m/ or http://serverA/mobile/. I have a shared header with a search form that needs to go to http://serverA/installationName/search.
However, if I use <form action="/search">
it goes to the root of the server, not the tomcat application.
If I use <form action="search">
it goes to a path relative to the current page. (i.e http://serverA/m/someOtherPage/search
I've tried <c:url value="search">
and <c:url value="/search">
but neither of them seem to work.
In intelliJ, <c:url value="/search">
gives me "Cannot resolve controller URL /search" even though I have a controller defined with @RequestMapping("/search")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
就是这样。忽略 IDE 告诉您的内容。他们不擅长这个。尝试运行它。Using
<c:url>
is the way. Ignore what the IDE tells you. They are not good at that. Just try to run it.博佐是对的。我也使用过 HTML BASE 标签:
如果您可以将此标签放在几个地方(最好仅在一个 JSP 中),您可以使代码更简洁。
Bozho is right. I have used HTML BASE tag too:
If you can put this tag in a few places (ideally in only one JSP) you can get your code cleaner.
您还可以(除了其他响应者提示之外)还使用 Spring JSP 标记 (spring:url),该标记是在 JSTL c:url 标记之后建模的(请参阅 Bozhos 回复)。顶级域名参考:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/spring.tld.html#spring.tld.url
以及这个mvc的底部:resources 块的示例使用:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources
You can (apart from other responders hints) also use Spring JSP tag (spring:url) which is modeled after the JSTL c:url tag (see Bozhos reply). The tld reference:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/spring.tld.html#spring.tld.url
And the bottom of this mvc:resources block for an example use:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources
如果您的表单标记是 jsp 标记(可能是),您将无法直接在属性中嵌入 c:url 标记。
在这种情况下,我会执行以下操作:
you will not be able to imbed the c:url tag directly in the attribute, if your form tag is a jsp tag (perhaps, <sf:form>).
In that situation I do the following: