【发布时间】:2016-01-31 00:43:27
【问题描述】:
我有一个带有 thymeleaf 的 spring-boot 应用程序,这个问题已经在这里提出了 http://forum.thymeleaf.org/CSS-and-JS-Not-Resolving-td4028878.html。该应用程序运行良好,今天由于某种原因停止了。我正在使用开箱即用的 spring-boot 配置。
对于我所有的脚本和样式,thymyleaf 试图将其解析为一个奇怪的模板。例如,当我尝试获取 jquery 时出现错误
{"timestamp":1454200386383,"status":500,"error":"Internal Server Error","exception":"org.thymeleaf.exceptions.TemplateInputException","message":"Error resolving template \"scripts/jquery.min\", template might not exist or might not be accessible by any of the configured Template Resolvers","path":"/scripts/jquery.min.js"}
这是我的配置
@Configuration
@EnableWebMvc
@ComponentScan("com.")
public class MvcWebConfig extends WebMvcConfigurerAdapter {
@Autowired
MessageRepository repository;
static final Logger logger = LoggerFactory.getLogger(MvcWebConfig.class);
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/static/", "classpath:/static/images/", "classpath:/static/css/"};
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
@Bean(name = "localeResolver")
public CookieLocaleResolver localeResolver() {
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
Locale defaultLocale = new Locale("en");
localeResolver.setDefaultLocale(defaultLocale);
return localeResolver;
}
@Bean
public DatabaseDrivenMessageSource messageSource() {
DatabaseDrivenMessageSource messageSource = new DatabaseDrivenMessageSource();
messageSource.setRepository(repository);
messageSource.reload();
return messageSource;
}
这是我的安全配置
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
CustomUserDetailsService userService;
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/scripts/**", "/css/**", "/images/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/index", "/login","/career", "/contact", "/error").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.logoutSuccessUrl("/")
.permitAll();
http.csrf()
.csrfTokenRepository(csrfTokenRepository());
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userService);
}
private CsrfTokenRepository csrfTokenRepository() {
HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
repository.setSessionAttributeName("_csrf");
return repository;
}
}
提前致谢
【问题讨论】:
-
请发布您的配置
-
您实际上并没有使用开箱即用的配置,因为
@EnableWebMvc表明您正在自己配置东西。删除它以及addResources,因为它们随后由 Spring Boot 配置。 -
你说得对,我不需要@EnableWebMvc。此外,我还删除了使代码更清晰的资源。但问题不是由这种错误配置引起的。
标签: spring-boot thymeleaf