Spring_配置Thymeleaf

世界上最快乐的事,莫过于为理想而奋斗。——苏格拉底

JSP和Thymeleaf

  大多数的JSP模板都是采用HTML的形式,但是又掺杂上了各种JSP标签库的标签,在Web浏览器或HTML编辑器展现的结果很难在审美上接近模板最终所渲染出来的效果。

  同时JSP规范是与Servlet规范紧密耦合,它只能作用于基于Servlet的Web应用中,JSP模板不能作为通用模板,也不能作用于非Servlet的Web应用。

  Thymeleaf模板是原生的,不依赖于标签库,它能在接受原始HTML的地方进行编辑和渲染,且因为它没有与Servlet规范耦合,因此Thymeleaf模板能够进入JSP模板 非法涉足的领域(如格式化Email)。

  下面介绍如何在SpringMVC中使用Thymeleaf。

配置Thymeleaf

  为了要在Spring中使用Thymeleaf,我们需要配置三个启用Thymeleaf与Spring集成的bean:

    1. ThymeleafViewResolver:将视图名称解析为Thymeleaf模板视图。

    2. SpringTemplateEngine:处理模板并渲染结果。

    3. TemplateResolver:加载Thymeleaf模板。

  pom.xml配置文件如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation
="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cn.spring.study</groupId> <artifactId>spring-thymeleaf</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.14.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.14.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>3.0.2.RELEASE</version> </dependency> </dependencies> </project>

  使用Java配置DispatcherServlet代码如下:

package com.spring.thymeleaf.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
 * 配置DispatcherServlet
 * @author 
 *
 */
public class ThymeleafWebAppInitialzer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return new Class<?>[] { RootConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return new Class<?>[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[] { "/" };
    }

}

  使用Java配置Spring代码如下:

package com.spring.thymeleaf.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

@Configuration
@EnableWebMvc
@ComponentScan("com.spring")
public class WebConfig extends WebMvcConfigurerAdapter {
    
    /**
     * 配置模板解析器
     */
    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver
            = new SpringResourceTemplateResolver();
        templateResolver.setPrefix("/WEB-INF/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        return templateResolver;
    }
    
    /**
     * 配置模板引擎
     */
    @Bean
    public TemplateEngine templateEngine(SpringResourceTemplateResolver templateResolver) {
        SpringTemplateEngine templateEngine =
                new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
        return templateEngine;
    }
    
    /**
     * 配置Thymeleaf模板视图解析器
     */
    @Bean
    public ViewResolver viewResolver(SpringTemplateEngine templateEngine) {
        System.err.println("=========================ViewResolver");
        ThymeleafViewResolver viewResolver = 
                new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine);
        return viewResolver;
    }
    
    
    
    /**
     * 配置静态资源处理
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        // TODO Auto-generated method stub
        configurer.enable();;
    }
}

  如此Thymeleaf配置好了,接下来配置Thymeleaf模板文件。

定义Thymeleaf模板

<html xmlns="http://www.w3.org/1999/xhmtl"
      xmlns:th="http://www.thymeleaf.org"
>
<head>
<title>thymeleaf</title>
</head>
<body>
    <h1>Welcome to thymeleaf!</h1>
    <a th:href="">Spittles</a>
</body>
</html>

  如此一个简单的Thymeleaf模板就定义好了,接下来是借助Thymeleaf实现表单绑定,未完待续...

 

猜你喜欢

转载自www.cnblogs.com/dandelZH/p/9080413.html