JSP 中的 Spring MVC 请求 URL
我正在使用 Spring MVC 编写一个 Web 应用程序。我正在为控制器等使用注释。一切都工作正常,除了应用程序中的实际链接(表单操作、 标签等)。当前,我有这个(显然是缩写):
//In the controller
@RequestMapping(value="/admin/listPeople", method=RequestMethod.GET)
//In the JSP
<a href="/admin/listPeople">Go to People List</a>
当我直接输入“http://localhost:8080/MyApp/admin/listPeople”这样的URL时,页面会正确加载。但是,上面的链接不起作用。它丢失了应用程序名称“MyApp”。
有谁知道是否有一种方法可以配置 Spring 以在其中添加应用程序名称?
如果您需要查看我的 Spring 配置,请告诉我。我正在使用带有视图解析器等的标准调度程序 servlet。
I am writing a web application using Spring MVC. I am using annotations for the controllers, etc. Everything is working fine, except when it comes to actual links in the application (form actions, <a>
tags, etc.) Current, I have this (obviously abbreviated):
//In the controller
@RequestMapping(value="/admin/listPeople", method=RequestMethod.GET)
//In the JSP
<a href="/admin/listPeople">Go to People List</a>
When I directly enter the URL like "http://localhost:8080/MyApp/admin/listPeople", the page loads correctly. However, the link above does not work. It looses the application name "MyApp".
Does anyone know if there is a way to configure Spring to throw on the application name on there?
Let me know if you need to see any of my Spring configuration. I am using the standard dispatcher servlet with a view resolver, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要在链接前面添加上下文路径。
You need to prepend context path to your links.
c:url
标记会将上下文路径附加到您的 URL。例如:或者,我也更喜欢在 Spring MVC 应用程序中尽可能使用相对 URL。因此,如果页面位于
/MyApp/index
,则链接会将我带到
listPeople页面。
如果您位于 URL 层次结构的较深处,这也适用。您可以使用
..
向上遍历一个级别。所以在/MyApp/admin/people/aPerson
页面上,使用就会回到列表页面
The
c:url
tag will append the context path to your URL. For example:Alternately, I prefer to use relative URLs as much as possible in my Spring MVC apps as well. So if the page is at
/MyApp/index
, the link<a href="admin/listPeople">
will take me to thelistPeople
page.This also works if you are deeper in the URL hierarchy. You can use the
..
to traverse back up a level. So on the page at/MyApp/admin/people/aPerson
, using<a href="../listPeople">
will like back to the list page我更喜欢使用 BASE 标签:
然后,您的所有链接都可以是这样的:
I prefer to use BASE tag:
Then, all your links can be like:
因为我刚刚试图找到这个问题的答案,这是第一个谷歌结果。
现在可以使用 MvcUriComponentsBuilder 来完成
这是 Spring MVC 4.0 版本的一部分
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.html
需要的方法是 fromMappingName
来自文档:
As i have just been trying to find the answer to this question and this is the first google result.
This can be done now using the MvcUriComponentsBuilder
This is part of the 4.0 version of Spring MVC
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.html
The method needed is fromMappingName
From the documentation :
我通常将tomcat配置为使用上下文根“/”或将war部署为ROOT.war。无论哪种方式,战争名称都不会成为 URL 的一部分。
I usually configure tomcat to use context root of "/" or deploy the war as ROOT.war. Either way the war name does not become part of the URL.
您可以使用 servletRelativeAction。我不确定有哪些版本可用(我目前使用的是 4.0.x),而且我还没有看到太多这方面的文档,但是如果您查看支持 spring 表单的代码,您可能会猜到。只需确保您传递的路径以“/”开头。
示例:
参见 org.springframework.web.servlet.tags.form.FormTag:
You could use a servletRelativeAction. I'm not sure what versions this is available in (I'm using 4.0.x currently) and I haven't seen much documentation on this, but if you look at the code backing the spring form you can probably guess. Just make sure the path you pass it starts with a "/".
Example:
See org.springframework.web.servlet.tags.form.FormTag:
因为已经好几年了,我想我应该为其他寻找这个的人提供资金。例如,如果您正在使用注释并具有如下所示的控制器操作:
在您的 .jsp(视图)中添加此指令
并简单地使用
位置
value
的值应与@RequestMapping
匹配的 code> 控制器操作中的参数和var's
值是您用于href
的变量名称HIH
Since it's been some years I thought I'd chip in for others looking for this. If you are using annotations and have a controller action like this for instance:
in your .jsp (view) you add this directive
and simply use
where
value
's value should match@RequestMapping
's argument in the controller action andvar's
value is the name of the variable you use forhref
HIH