使用spring mvc进行静态内容渲染 [英] static content rendering with spring mvc
问题描述
我刚开始尝试使用它。
我有一个jsp文件,其中包含css和图像,如下所示
I have a jsp file which includes css and images as follows
<link href="css/style.css" rel="stylesheet" type="text/css" />
现在我的网址为 / localhost / myapp / test / tst
映射到一个控制器,然后转发它来查看jsp
Now i have a url path as /localhost/myapp/test/tst
which is mapped to a controller which then forwards it to view jsp
view.jsp出现了
view.jsp is present it
/web-inf/jsp/view.jsp
但是当点击路径 / localhost / myapp / test / tst
时,css和图像路径被解析为
But when hit the path /localhost/myapp/test/tst
the css and images path gets resolved to
/localhost/myapp/test/tst/css/style.css
我希望它能以 /localhost/myapp/css/style.css
css和html出现在webapp的根目录
Actaully i want it to come as /localhost/myapp/css/style.css
css and html are present in root of webapp
我如何在春季配置中关注
How i have following in my spring config
<mvc:annotation-driven />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" />
推荐答案
您的CSS href当前是相对于您的页面的,您需要通过添加前导斜杠(加上下文等)使其相对于服务器根目录。添加contextPath也是一个好主意,这样如果/当你更改webapp名称或将其部署为root webapp时,资源仍然有效。
Your CSS href is currently relative to your page, you need to make it relative to the server root by adding a leading slash (plus context, etc). Its also a good idea to add the contextPath so that if/when you change the webapp name or deploy it as a root webapp, the resources still work.
<link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css" />
这篇关于使用spring mvc进行静态内容渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!