Annotated a class with @Configuration and @Controller. Need help in refactoring

前端 未结 6 613
一整个雨季
一整个雨季 2021-01-17 15:49

Below is my class in which i had to use both @Configuration and @Controller as there should be only one instance of Thymeleaf in the e

6条回答
  •  情话喂你
    2021-01-17 16:22

    Don't put request mappings inside configuration classes, it violates the principal of separation of concerns. You can go for a approach like below.

    All the application wide beans are setup in Application class which is present in the root of the classpath. Application class in the best place to have your thymeleaf and static resource configurations too, since the Application class have application-scope.

    @SpringBootApplication
    @EnableWebMvc
    public class Application{
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @Bean
        public ViewResolver viewResolver() {
           ThymeleafViewResolver resolver = new ThymeleafViewResolver();
           resolver.setTemplateEngine(templateEngine());
           resolver.setCharacterEncoding("UTF-8");
           resolver.setCache(false);
           return resolver;
        }
    
       @Bean
       public TemplateEngine templateEngine() {
            SpringTemplateEngine templateEngine = new SpringTemplateEngine();
            templateEngine.setEnableSpringELCompiler(true);
            templateEngine.addDialect(new LayoutDialect());
            templateEngine.addDialect(new Java8TimeDialect());
            templateEngine.setTemplateResolver(templateResolver());
            return templateEngine;
       }
    
       private ITemplateResolver templateResolver() {
           SpringResourceTemplateResolver resolver = new 
                SpringResourceTemplateResolver();
           resolver.setApplicationContext(applicationContext);
           resolver.setPrefix("classpath:/templates/");
           resolver.setTemplateMode(TemplateMode.HTML);
           return resolver;
       }
    }
    

    If you put the static resources inside a folder named static or public in the classpath, springboot identify that as the location for static resources. Then you don't need to override addResourceHandlers method. If you really want to do it, you can do it inside the Application class extending WebMvcConfigurerAdapter. You don't need separate class to configure just static resource paths.

    Don't put request mappings inside configuration classes, put them in separate controller classes like:

    @Controller
    public class MyController {
       @GetMapping("/view-template")
       @ResponseBody
       public void viewTemplates() {
             Context context = new Context();
             context.setVariable("mydata", "this is it");
    
             String html = templateEngine().process("templates/view-to-process.html", context);
             System.out.println(html);
        }
    }
    

    Of cause, springboot allows you to do it the way you like, but you'd better stick to a general approach.

提交回复
热议问题