Links (href) not relative to application in Spring MVC?

蹲街弑〆低调 提交于 2019-12-12 02:47:02

问题


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

However, these now don't work unless I put the servlet name in front like so:

"<a href=\"/ServletName/rule\">Manage rules</a><br/>"

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?

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)

回答1:


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)

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>


来源:https://stackoverflow.com/questions/9297754/links-href-not-relative-to-application-in-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!