Jsp Sprinboot webapp没有';不加载webjar和css
我有带springboot和jsp的maven webapp。我正在尝试导入WebJAR以在jsp上使用引导,我在pom(包括定位器)上添加依赖项,并将资源处理程序放在我的webconfig上:Jsp Sprinboot webapp没有';不加载webjar和css,jsp,spring-boot,web-applications,webjars,Jsp,Spring Boot,Web Applications,Webjars,我有带springboot和jsp的maven webapp。我正在尝试导入WebJAR以在jsp上使用引导,我在pom(包括定位器)上添加依赖项,并将资源处理程序放在我的webconfig上: <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>3.3.7-1</version> <
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7-1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator</artifactId>
<version>0.30</version>
</dependency>
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/webjars/**")
.addResourceLocations("/webjars/");
}
}
然后我尝试在jsp中导入JAR:
<script src="/webjars/jquery/3.1.1/jquery.min.js"></script>
但是不起作用。。我将css放在webapp文件夹中,但无法将其链接到jsp中
我怎样才能解决这个问题??怎么了??谢谢大家
Ps:我在web inf/jsp/pages中有我的jsp,并且我已经向application.properties添加了一个上下文路径(但我认为与WebConfig类存在一些冲突)通过使用
@EnableWebMvc
,您已经向Spring Boot表明您想要完全控制Spring MVC的配置。这意味着它所有的静态资源自动配置、WebJAR支持等都已关闭
您可以完全删除WebConfig
类,因为它复制了Spring Boot将自动为您执行的配置。如果您需要自定义默认配置,您应该像以前一样实现webmvcconfiguer
,但省略@EnableWebMvc
注释。添加以下类
@Configuration
@EnableWebSecurity//(debug=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().requestMatchers(CorsUtils::isPreFlightRequest)
.antMatchers("/webjars/**");
}
}
添加
资源链
,并将其设置为false,如下所示:
registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/").resourceChain(false);
这显然不是webjar定位器问题。配置webjar定位器时,必须调用
resourceChain()
方法。尝试将${pageContext.request.contextPath}
添加到JSP文件中。这个给我修好了
<link href="${pageContext.request.contextPath}/webjars/bootstrap/4.6.0/css/bootstrap.min.css"
rel="stylesheet">
<script src="${pageContext.request.contextPath}/webjars/jquery/3.5.1/jquery.min.js" defer></script>
<script src="${pageContext.request.contextPath}/webjars/bootstrap/4.6.0/js/bootstrap.min.js" defer></script>
这个问题没有提到Spring安全性,因此这段代码很有可能无法编译。谢谢,它可以工作!!为了加载css,我在参考资料中创建了静态文件夹。我把css文件夹放在那里,然后通过
导入它。PS:正如你所说,我也删除了这个类,并将我添加到属性文件中的上下文添加到jars路径。这对我很有帮助,但不幸的是,链接断了,我找不到相应的issue@evandor,谢谢你的评论,我删除了链接。