spring boot 整合 thymeleaf

一:介绍

      如果Spring boot在类路径中对配置进行扫描,它将自动配置。我们可以使用application.properties更改默认的配置。thymeleaf是一个可以处理XML、HTML等的服务器端模板引擎,它可以访问一个类字段,从i18n消息文件中获取消息属性。我们可以使用thymymf将类字段与HTML表单元素绑定在一起。我们还将提供如何使用Maven,如果Spring启动启动器使用的是较低版本的thymeleaf,则使用Maven来使用更高版本。我们还将使用我们的Thymeleaf view 的CSS文件。

二: 新建项目 



三: 创建Maven文件 

        找到本例中使用的Maven文件。


<?xml version="1.0" encoding="UTF-8"?>
<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.xphsc</groupId>
    <artifactId>eglsc-thymeleaf</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <url>http://maven.apache.org</url>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.5.RELEASE</version>
    </parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.26</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.24</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.12</version>
    </dependency>
     <dependency>
            <groupId>com.github.xphsc</groupId>
            <artifactId>eglsc-helper</artifactId>
            <version>1.2.0</version>
        </dependency>
</dependencies></project>

找到spring boot启动器的描述。

spring-boot-starter-parent:父项目POM用于依赖关系管理。

spring-boot-starter-web::构建web、REST应用程序的启动器。它使用tomcat服务器作为默认的嵌入式服务器

spring-boot-starter-thymeleaf spring boot 配置thymeleaf模板引擎

2:创建application.properties,application-dev.yml

在Spring boot中,为了配置与数据库相关的属性、Hibernate和日志,我们需要使用application.properties。我们正在使用JPA规范的Hibernate实现。

application.properties
server.port=8082
spring.profiles.active=dev
logging.level.root= INFO
logging.level.org.springframework.web= ERROR
logging.level.com.xphsc= INFO
application-dev.yml
spring:
    thymeleaf:
            prefix: classpath:/WEB-INF/views/
            mode: HTML5
            encoding: UTF-8
            content-type: text/html
            cache: true

    datasource:
                   name: test_mysql
                   url: jdbc:mysql://localhost:3306/test_mysql?useUnicode=true&characterEncoding=UTF-8
                   username: root
                   password: root
                   type: com.alibaba.druid.pool.DruidDataSource
                   driver-class-name: com.mysql.jdbc.Driver
                   filters: stat
                   maxActive: 20
                   initialSize: 1
                   maxWait: 60000
                   minIdle: 1
                   timeBetweenEvictionRunsMillis: 60000
                   minEvictableIdleTimeMillis: 300000
                   validationQuery: select 'x'
                   testWhileIdle: true
                   testOnBorrow: false
                   testOnReturn: false
                   poolPreparedStatements: true
                   maxOpenPreparedStatements: 20

创建 thymeleaf模板页面(index.html)

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h4 th:text="${msg}"></h4>
</body>
</html>
创建访问页面控制器controller(ViewController)

@Controller
public class ViewController {

    @RequestMapping("/index")
    public String index(Model model){
        model.addAttribute("msg","欢迎进入index页面");
        return "index";
    }
}
创建 spring boot启动类
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder()
                .sources(Application.class)
                .bannerMode(Banner.Mode.CONSOLE)
                .run(args);
    }
3 :运行spring boot 的启动类 就可以访问自己定义好的页面(如图)

*********************************************

2



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值