gpt4 book ai didi

java - WebJars 无法在 JSP 中访问

转载 作者:太空宇宙 更新时间:2023-11-04 13:33:44 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com