简体   繁体   中英

Thymeleaf and Spring4 configuration

I've been trying to utilise Thymeleaf in a web that I'm playing with and can't quite seem to get my head around the configuration / setup.

Previously I've used the web folder for storage of assets and jsp/html etc but this now seems to be redundant as the views folder has moved to resources. Is this accurate?

My structure looks something like this:

src-main-java-various controllers/models etc
src-main-resources-Meta-inf - persistence.xml
src-main-resources-spring-config.xml
src-main-resources-views-test.html etc etc
src-main-webapp-assets-css - now in the wrong place?
src-main-webapp-WEB-INF-html/jsp = now irrelevant?

Apologies if this seems like a dumb question, I can't seem to find a decent tutorial that doesn't have conflicting information in it.

I'm basically trying to set up a simple web application using thyme leaf instead of jsp files. Any pointers in the right direction are gratefully received.

Thus far I have a ConfigClass containing

   @Bean
    public ViewResolver viewResolver() {
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setTemplateMode("XHTML");
        templateResolver.setPrefix("views/");
        templateResolver.setSuffix(".html");
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver);

        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(engine);
        return viewResolver;
    }

which seems to be the key behind everything but as it's from a tutorial I'm not sure what is good/bad/incorrect/bad practice and so on.

The html files I usually put under resources folder are for email templates - these are not the views of your web application. Views should stay under webapp/WEB-INF folder.

In order to load the views from webapp/WEB-INF (from ServletContext , just like any usual SpringMVC applications), use ServletContextTemplateResolver . ClassLoaderTemplateResolver is used if your templates are in classpath (eg email templates).

Here is my production config

@Bean
public ViewResolver viewResolver() {
   ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
   templateResolver.setTemplateMode("HTML5");
   templateResolver.setPrefix("/WEB-INF/html/");
   templateResolver.setSuffix(".html");
   SpringTemplateEngine engine = new SpringTemplateEngine();
   engine.setTemplateResolver(templateResolver);

   ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
   viewResolver.setTemplateEngine(engine);
   return viewResolver;
}

My directory

src-main-java --> Java classes (Spring controllers, etc.)
src-main-resources --> xml configs
src-main-webapp-css --> CSS files
src-main-webapp-js --> Javascript files
src-main-webapp-WEB-INF-views --> HTML5 files

Try Spring Boot which takes care of all default configuration and dependencies as well.

Spring Boot Get started here, to use Thymeleaf add below dependency in pom file

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Spring Boot configures rest everything.

Once you are familiar with it you can override default configurations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM