我有一个带有Spring和JSP的Maven JSP应用程序。我试图导入webjars以便在JSP上使用引导程序,我在pom上添加了依赖项(包括定位器),并将资源处理程序放在webconfig上:
<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>现在,我在webconfig上添加引用:
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/webjars/**")
.addResourceLocations("/webjars/");
}
}然后我尝试导入JSP中的jars:
<script src="/webjars/jquery/3.1.1/jquery.min.js"></script>但不起作用。我将css放在webapp文件夹中,但不能将它链接到JSP中。
我怎么才能解决这个问题?怎么了?
PS:我的JSP在web-inf/jsp/pages中,我为application.properties添加了一个上下文路径(但我认为与WebConfig类有一些冲突)。
发布于 2018-11-23 17:06:48
通过使用@EnableWebMvc,您已经向Spring表示您希望完全控制Spring的配置。这意味着它对静态资源、web支持等的所有自动配置都被关闭了。
您可以完全删除WebConfig类,因为它重复Spring自动为您所做的配置。如果您需要自定义默认配置,您应该像以前那样实现WebMvcConfigurer,但忽略@EnableWebMvc注释。
发布于 2019-02-07 15:43:45
添加resourceChain并将其设置为false,如下所示:
registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/").resourceChain(false);这显然不是webjar定位器problem.call to resourceChain()方法对于webjar定位器的配置所必需的。
发布于 2018-11-23 17:22:01
添加下面的类。
@Configuration
@EnableWebSecurity//(debug=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().requestMatchers(CorsUtils::isPreFlightRequest)
.antMatchers("/webjars/**");
}
}https://stackoverflow.com/questions/53450145
复制相似问题