【问题标题】:In springboot css and js files not loading in jsp file在springboot css和js文件中没有加载到jsp文件中
【发布时间】:2018-07-22 10:36:08
【问题描述】:

在spring-boot应用中,所有cssjsimages都在下面的结构中:

当 URL 像这样 http://localhost:8080/request/

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

但是当网址是这样的http://localhost:8080/request/rt/homecss/image/js 没有加载)。

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

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

它将加载 css/js/images 请推荐

【问题讨论】:

  • 您是在创建 JAR 还是 WAR 存档?
  • 刚刚开始开发..from eclipse 应用程序正在运行..

标签: spring spring-mvc spring-boot


【解决方案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>

哪个会解析你的上下文名称

【讨论】:

    【解决方案2】:

    试试这个方法

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-11
      • 2017-05-07
      • 2016-08-09
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 2015-12-17
      • 2012-07-13
      相关资源
      最近更新 更多