【问题标题】:WebJars not accessible in JSP在 JSP 中无法访问 WebJars
【发布时间】:2015-08-10 11:01:09
【问题描述】:

我想做一个基于 Spring MVC、Semantic-UI 框架和 Gradle 构建工具的网页。问题是,当我尝试从 WebJars 导入它们时,JSP 页面无法访问语义文件。当我使用从 cdnjs 导入时,它工作得非常好。

好的,下面是我的代码:

@Configuration
@EnableWebMvc
@ComponentScan("com.web)
public class WebConfiguration extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}

index.jsp

    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://www.springframework.org/tags" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
      <!--
      <link rel='stylesheet' href='webjars/semantic-ui/2.0.7/semantic.min.css'>
      <script src='webjars/semantic-ui/2.0.7/semantic.min.js'></script> -->

      <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.0.7/semantic.min.css'>
      <script src='https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.0.7/semantic.min.js'></script>

    <title>Index Page</title>
  </head>
  <body>
  <spring:message code="Index.WelcomeText"></spring:message>
  <div class="ui animated button" tabindex="0">
      <div class="visible content">Next</div>
      <div class="hidden content">
          <i class="right arrow icon"></i>
      </div>
  </div>
  <div class="ui vertical animated button" tabindex="0">
      <div class="hidden content">Shop</div>
      <div class="visible content">
          <i class="shop icon"></i>
      </div>
  </div>
  <div class="ui animated fade button" tabindex="0">
      <div class="visible content">Sign-up for a Pro account</div>
      <div class="hidden content">
          $12.99 a month
      </div>
  </div>
  </body>
</html>

【问题讨论】:

  • 您确定semantic-ui WebJar 版本2.0.7 在类路径上吗?
  • 我猜是这样,我通过gradle得到它-编译'org.webjars:Semantic-UI:2.0.7',然后我可以在tomcat中通过地址访问semantic-ui文件:@ 987654321@。我不知道出了什么问题...
  • 也许您需要匹配您正在使用的 URL 中的大小写?

标签: java spring jsp webjars


【解决方案1】:

可能是您没有使用相对路径来执行此操作。尝试将它们注入索引文件。我有同样的问题。

【讨论】:

    【解决方案2】:

    正如预期的那样,您必须在 url 中添加 contextpath 以使浏览器识别资源

    <head>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/semantic-ui/2.0.7/semantic.min.css">
    <script src="${pageContext.request.contextPath}/webjars/semantic-ui/2.0.7/semantic.min.js"></script>
    <title>Index Page</title>
    </head>
    

    喜欢这个

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-20
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      相关资源
      最近更新 更多