【问题标题】:Spring MVC absolute URL problemSpring MVC 绝对 URL 问题
【发布时间】:2011-02-01 01:43:18
【问题描述】:

背景

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

http://localhost:8080/myapplication/

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

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

问题

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

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

应该是这样的:

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

我曾经在使用Struts 时使用html:rewrite 标记来解决此问题。 Spring MVC 中是否有任何等效的标签/技术?

感谢您的宝贵时间。

【问题讨论】:

    标签: java html spring-mvc


    【解决方案1】:

    使用 JSTL c:url 标签。

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

    您也可以使用 pageContext 作为上下文路径的前缀。

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

    【讨论】:

    • 为避免使用${pageContext.request.contextPath} 混淆所有链接,请使用&lt;base&gt; 标签。
    猜你喜欢
    • 2011-05-02
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 2020-07-04
    • 2014-12-11
    • 2021-03-10
    相关资源
    最近更新 更多