Spring Boot整合thymeleaf

来源:互联网 发布:centos7 网络配置 编辑:程序博客网 时间:2024/04/28 02:14
今天继续学习了Spring boot的一些东西,然后我就整合了一个Thymeleaf 这个模板中间经历了非常多的困难 现在记录下来我的惨痛经历

First Blood
刚开始我直接建了一个普通的工程,我闲建Maven工程太费劲了,然后我就把我的jar包直接导入进去了,这就导致了我的第一个问题,无法找到*.HTML的文件。

把我的pom.xml放了上来 仅供参考

<parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.2.RELEASE</version>        <relativePath /> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <!-- mysql连接 -->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <scope>runtime</scope>        </dependency>        <!-- mybatis -->        <dependency>            <groupId>org.mybatis.spring.boot</groupId>            <artifactId>mybatis-spring-boot-starter</artifactId>            <version>1.1.1</version>        </dependency>        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>fastjson</artifactId>            <version>1.2.7</version>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-thymeleaf</artifactId>        </dependency>        <dependency>            <groupId>net.sourceforge.nekohtml</groupId>            <artifactId>nekohtml</artifactId>            <version>1.9.22</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build>

Double Kill

Spring Boot的配置文件报错 ,天呐
//* 就是你自己的东西了

server.port=8011spring.datasource.url=jdbc:mysql://****?characterEncoding=utf8&useSSL=truespring.datasource.username=****spring.datasource.password=****spring.datasource.driver-class-name=com.mysql.jdbc.Drivermybatis.mapperLocations=classpath:com/****/MapperXML/*.xmlspring.thymeleaf.content-type=text/html spring.thymeleaf.cache=false spring.thymeleaf.mode =LEGACYHTML5

其实很简单的东西 然后在 resource的文件夹里建一个Hello World.html,
里面放上

<!DOCTYPE HTML><html xmlns:th="http://www.thymeleaf.org"><head>    <title>hello</title>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><!--/*@thymesVar id="name" type="java.lang.String"*/--><p th:text="'Hello!, ' + ${name} + '!'" >3333</p></body></html></html>

Controller 为
.

@Controllerpublic class HelloController {    @RequestMapping(value = "hello", method = RequestMethod.GET)    public String hello(Model model) {        model.addAttribute("name", "Dear");        return "hello";    }}

加油