WebJars无法在JSP中访问

时间:2015-08-10 11:01:09

标签: java spring jsp webjars

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

好的,我的代码如下:

@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>

2 个答案:

答案 0 :(得分:0)

可能是你没有使用相对路径来做到这一点。尝试在索引文件中注入它们。我有同样的问题。

答案 1 :(得分:0)

不出所料,您必须在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>

像这样