链接(href)与 Spring MVC 中的应用程序无关? [英] Links (href) not relative to application in Spring MVC?
问题描述
我正在将一个在端口 8080 上使用简单 java HTTPServer 的项目更改为使用 Spring MVC servlet 框架的项目.
I am in the process of changing a project which used a simple java HTTPServer on port 8080 to that which uses the Spring MVC servlet framework.
应用程序中有很多链接,如下所示:
There are lots of links in the application like so:
<a href=\"/rule\">Manage rules</a><br/>
使用以下请求效果很好:
Which worked fine using requests such as:
http://localhost:8080/send
但是,除非我像这样将 servlet 名称放在前面,否则这些现在不起作用:
However, these now don't work unless I put the servlet name in front like so:
"<a href=\"/ServletName/rule\">Manage rules</a><br/>"
只是想知道有什么办法可以解决这个问题,还是只需更改所有 href 链接以在它们前面添加 servlet 名称即可?
Just wondering is there any way around this, or is it a matter of just changing all the href links to add the servlet name in front of them?
请注意,我不必在方法开始时在 @RequestMapping 调用中添加 servlet 名称,只有它的链接有问题.例如.无需在前面添加 servlet 名称即可正常工作
Note that I dont have to add the servlet name in my @RequestMapping calls at the start of methods, its only links that are the problem. E.g. this works fine without adding the servlet name in front
@RequestMapping(value = "/send", method = RequestMethod.GET)
推荐答案
如果您在视图层中使用 JSP,请使用具有链接标记的标记库(或自己编写).基本上,链接标签必须通过做
If you use JSPs in your view layer, use a tag library that has a link tag (or write one yourself). Basically, the link tag has to create the url by doing
HttpServletResponse.encodeURL(originalUrl)
标签JSTL 会这样做,但它只生成 URL,而不生成链接标记,但它可以像这样让您到达那里:
The <c:url>
tag in JSTL does that, but it only generates the URL, not the link tag, but it can get you there like this:
<c:url value="your/relative/url" var="somevar" />
<a href="${somevar}">Link Text</a>
这篇关于链接(href)与 Spring MVC 中的应用程序无关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!