【问题标题】:Thymeleaf: Error resolving template "scripts/.."Thymeleaf:解析模板“脚本/..”时出错
【发布时间】: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


【解决方案1】:

我在以下请求映射之一中有错字

@RequestMapping(name = "/url", method = RequestMethod.GET)

应该是

@RequestMapping(value = "/url", method = RequestMethod.GET)

我不知道幕后发生的更多细节,但错误消息也没有提供关于错误根本原因的信息。

【讨论】:

    猜你喜欢
    • 2016-12-21
    • 2021-11-05
    • 2014-08-26
    • 1970-01-01
    • 2021-09-25
    • 2020-10-07
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    相关资源
    最近更新 更多