java - WebJars 无法在 JSP 中访问

标签 java spring jsp webjars

我想制作一个基于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>

最佳答案

正如预期的那样,您必须在 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>

像这样

关于java - WebJars 无法在 JSP 中访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31917824/

相关文章:

java - 使用Spring @ControllerAdvice的全局错误处理程序不起作用

java - 根据移动和桌面浏览器切换布局

javascript - 从iFrame中获取内容,错误在哪里?

java - 如何从 3.5.2 发行版重建 hibernate3.jar?

java - "error": "invalid_grant", "error_description": "Bad credentials"

java - 反汇编 Java 代码

java - 在 Spring Boot 中构建动态配置路径

jsp - 如何插入空格?

java - "The requested resource does not support http method ' GET '"- 但我 'm not using C# or asp.net, I' 是发出请求的人

java - Servlet-API 包不提供 JavaServlet 功能 - felix.http.jetty 无法运行