问题
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