Spring MVC Файл конфигурации Thymleaf - PullRequest
0 голосов
/ 11 апреля 2020

Я пытаюсь реализовать https://mkyong.com/spring-boot/spring-boot-hello-world-example-thymeleaf/ с помощью файла конфигурации Spring MVC для большей гибкости, которая фактически отсутствует в руководстве.

Для получения файла конфигурации Spring MVC я воспользовался документацией https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#views -and-view-resolvers-in-spring- mvc. Вот код

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

@Configuration
@EnableWebMvc
@ComponentScan
public class SpringWebConfig  extends WebMvcConfigurerAdapter implements ApplicationContextAware {


    private ApplicationContext applicationContext;


    public SpringWebConfig() {
        super();
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // TODO Auto-generated method stub
        this.applicationContext = applicationContext;


    }

    @Bean
    @Description("Thymeleaf View Resolver")
    public ThymeleafViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setOrder(1);
        return viewResolver;
    }

    @Bean
    @Description("Spring Message Resolver")
    public ResourceBundleMessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }


    @Bean
    @Description("Thymeleaf Template Engine")
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.setTemplateEngineMessageSource(messageSource());
        return templateEngine;
    }

    @Bean
    @Description("Thymeleaf Template Resolver")
    public SpringResourceTemplateResolver templateResolver() {
         SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
            templateResolver.setApplicationContext(this.applicationContext);
        templateResolver.setPrefix("/WEB-INF/views/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");

        return templateResolver;
    }

      @Override
        public void addResourceHandlers(final ResourceHandlerRegistry registry) {
            super.addResourceHandlers(registry);
            registry.addResourceHandler("/images/**").addResourceLocations("/images/");
            registry.addResourceHandler("/css/**").addResourceLocations("/css/");
            registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        }

}

Я получаю два предупреждения, и представление не отображается должным образом

2020-04-11 19:37:12.239  WARN 27247 --- [nio-8080-exec-2] o.s.web.servlet.PageNotFound             : No mapping for GET /webjars/bootstrap/4.2.1/css/bootstrap.min.css
2020-04-11 19:37:12.241  WARN 27247 --- [nio-8080-exec-4] o.s.web.servlet.PageNotFound             : No mapping for GET /webjars/bootstrap/4.2.1/js/bootstrap.min.js

как обеспечить сопоставление для веб-файлов / bootstrap ...

Может кто-нибудь помочь мне?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...