在springboot css和js文件中没有加载jsp文件

时间:2018-02-12 09:43:33

标签: spring spring-mvc spring-boot

在spring-boot应用程序中,指向所有 css js images 的结构如下:

enter image description here

当URL类似于此http://localhost:8080/request/

时,JSP页面将加载所有 css / js / images

但是当网址如下http://localhost:8080/request/rt/home时 ( css / image / js 未加载)。

当我从查看源页面点击bootstrap.min.css的网址时,它显示如下: http://localhost:8080/request/rt/public/css/bootstrap.min.css

" RT"在网址之间添加。 如何省略" rt"从路径。 这样就会得到像http://localhost:8080/request/public/css/bootstrap.min.css

这样的路径

它将加载 css / js / images 请建议

2 个答案:

答案 0 :(得分:1)

如果没有看到您的jsp,根据您对其行为的陈述,我会说您以这种方式声明样式表和资源,而不是以/开头:

<link href="public/css/bootstrap.min.css" rel="stylesheet">

<script src="public/css/bootstrap.min.js"></script>

这意味着资源相对于浏览器的url栏的虚拟目录。

您应该以这种方式声明您的引用:

<link href="/request/public/css/bootstrap.min.css" rel="stylesheet">

<script src="/request/public/css/bootstrap.min.js"></script>

为避免硬编码上下文路径,您可以使用:

<link href="${pageContext.request.contextPath}/public/css/bootstrap.min.css" rel="stylesheet">

<script src="${pageContext.request.contextPath}/public/css/bootstrap.min.js"></script>

将解析您的上下文名称

答案 1 :(得分:0)

尝试这种方式

<link href="/resources/css/bootstrap.min.css" rel="stylesheet">

<link href="${pageContext.request.contextPath}/resources/css/bootstrap.min.css" rel="stylesheet">