Java Spring MVC绝对URL问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2521606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:46:39  来源:igfitidea点击:

Spring MVC absolute URL problem

javahtmlspring-mvc

提问by craftsman

Background

背景

I am developing an application (with Spring MVC) with its base path as:

我正在开发一个应用程序(带有Spring MVC),其基本路径为:

http://localhost:8080/myapplication/

I have a stylesheet /css/style.cssthat I am trying to refer with absolute pathin a JSP as:

我有一个样式表/css/style.css,我试图absolute path在 JSP 中将其引用为:

<link rel="stylesheet" href="/css/style.css" type="text/css" media="screen, projection">

Problem

问题

The stylesheet never loads in the browser. When I follow the stylesheet link through browser's view sourcefeature, the link appears to be:

样式表永远不会在浏览器中加载。当我通过浏览器的查看源功能跟踪样式表链接时,该链接似乎是:

http://localhost:8080/css/style.css

Which should have had been:

本来应该是:

http://localhost:8080/myapplication/css/style.css

I used to fix this issue with html:rewritetag while working with Struts. Is there any equivalent tag/technique in Spring MVC?

我曾经html:rewrite在使用Struts. 中是否有任何等效的标签/技术Spring MVC

Thanks for your time.

谢谢你的时间。

采纳答案by Chandra Sekar

Use the JSTL c:url tag.

使用 JSTL c:url 标记。

<c:url value="/css/style.css" var="url" />
<link rel="stylesheet" href="${url}" type="text/css" media="screen, projection">

You can also use the pageContext to prefix the context path.

您还可以使用 pageContext 为上下文路径添加前缀。

<link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css" type="text/css" media="screen, projection">