【问题标题】:static content rendering with spring mvc使用spring mvc进行静态内容渲染
【发布时间】:2012-11-28 16:25:48
【问题描述】:

我是 spring 新手,正在尝试。

我有一个 jsp 文件,其中包含如下 css 和图像

<link href="css/style.css" rel="stylesheet" type="text/css" />

现在我有一个 url 路径为 /localhost/myapp/test/tst,它被映射到一个控制器,然后将它转发到查看 jsp

view.jsp 存在

/web-inf/jsp/view.jsp

但是当点击路径 /localhost/myapp/test/tst 时,css 和图像路径被解析为

/localhost/myapp/test/tst/css/style.css

实际上我希望它以/localhost/myapp/css/style.css 的形式出现 css 和 html 存在于 webapp 的根目录中

我如何在我的 spring 配置中关注

 <mvc:annotation-driven />
    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/css/**" location="/css/" />

【问题讨论】:

  • 你为什么不用&lt;link href="/css/style.css"

标签: java spring spring-mvc


【解决方案1】:

您的 CSS href 当前是相对于您的页面的,您需要通过添加前导斜杠(加上上下文等)使其相对于服务器根目录。添加 contextPath 也是一个好主意,这样如果/当您更改 webapp 名称或将其部署为根 webapp 时,资源仍然有效。

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

【讨论】:

    猜你喜欢
    • 2020-01-26
    • 2013-06-12
    • 1970-01-01
    • 2017-06-20
    • 2017-01-14
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 2016-02-16
    相关资源
    最近更新 更多