简体   繁体   English

Thymeleaf和Spring4配置

[英]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. 我一直在尝试在正在玩的网络中利用Thymeleaf,但似乎不太了解配置/设置。

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. 以前,我曾使用web文件夹来存储资产和jsp / html等,但是现在由于views文件夹已移至资源而显得多余。 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. 我基本上是在尝试使用百里香叶而不是jsp文件来建立一个简单的Web应用程序。 Any pointers in the right direction are gratefully received. 任何朝着正确方向的指针都将受到感激。

Thus far I have a ConfigClass containing 到目前为止,我有一个ConfigClass包含

   @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. 我通常放在资源文件夹下的html文件是用于电子邮件模板的-这些不是您的Web应用程序的视图。 Views should stay under webapp/WEB-INF folder. 视图应保留在webapp/WEB-INF文件夹下。

In order to load the views from webapp/WEB-INF (from ServletContext , just like any usual SpringMVC applications), use ServletContextTemplateResolver . 为了从webapp/WEB-INF加载视图(从ServletContext加载,就像其他通常的SpringMVC应用程序一样),请使用ServletContextTemplateResolver ClassLoaderTemplateResolver is used if your templates are in classpath (eg email templates). 如果您的模板位于类路径中(例如,电子邮件模板),则使用ClassLoaderTemplateResolver

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 ,它可以处理所有默认配置和依赖项。

Spring Boot Get started here, to use Thymeleaf add below dependency in pom file Spring Boot从这里开始,使用Thymeleaf在pom文件中添加以下依赖项

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

Spring Boot configures rest everything. Spring Boot配置其余的一切。

Once you are familiar with it you can override default configurations 熟悉后即可覆盖默认配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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