springbooy整合thymeleaf

木易 2345 0 0

    在springboot整合视图层,官方推荐使用thymeleaf。

    thymeleaf只是渲染html的一种方式,是一种模板。


第一步创建一个maven项目

    1.png


第二步:修改Jdk版本,添加thymeleaf启动器

 <properties>
  	<java.version>1.7</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-thymeleaf</artifactId>
    </dependency>
  </dependencies>

    

第三步:在resources下创建templates目录

2.png


第四步:创建controller层

package com.spring;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MessageController {
	
	@RequestMapping("/showMessage")
	public String showMessage(Model model) {
		
		model.addAttribute("msg", "thymeleaf");
		return "/index";
		
	}

}


第五步:在templates目录下创建html页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<span th:text="hello,我是thymeleaf"></span>
	<hr/>
	<span th:text="${msg}"></span>

</body>
</html>

第六步:创建启动类进行启动,访问controller

    如果我们没有指定thymeleaf的版本就会出现异常,因为在thymeleaf3.0以前,对html有严格的规定,所有的标签都必须闭合,否则就会出现异常

image.png


解决方案:1添加闭合标记

                    2.修改thymeleaf和thymeleaf-layout-dialect的版本

<properties>
  	<java.version>1.7</java.version>
  	<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
  	<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
  </properties>




评论区
请写下您的评论...
暂无评论...
猜你喜欢
框架 2563 springbootmybatis1.创建maven项目2.sql文件SETNAMESutf8mb4;SETFOREIGN_KEY_CHECKS=0
框架 2668 1.配置springboot支持websocketpackagecom.example.demo.websocket;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.so
spring/springmvc 1538 springmvc+mybatisshiro权限1.需要的jar包propertiesshiro.version1.3.2/shiro.version/propertiesdependency
weblog 1027 pomparent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version2.1.3.RELEASE/version /parentdependencies dependency groupIdorg.springframework.boot/group
框架 2409 安装redis数据库参考:http://www.jiajiajia.club/blog/artical/166redis配置详解参考:http://www.jiajiajia.club/blog/artical/210安装完数据库以后如果不是本地连接记得修改密码requirepass。默认是没有密码需要后台运行修改daemonizeyes默认是noyml配置文件spring:redis:host:
框架 1483 1.pom文件dependencygroupIdorg.apache.shiro/groupIdartifactIdshiro-spring/artifactIdversion1.4.0/version/dependencydependency groupIdorg.apache.shiro/groupId artifactIdshiro-ehcache/artifactId vers
框架 2599 1.项目结构2.pom文件依赖?xmlversion="1.0"encoding="UTF-8"?projectxmlns="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.
java框架 1378 springbootelasticsearch框架实现全文索引demo配置说明参考:http://www.jiajiajia.club/blog/artical/Ja4t7X/378
归档
2018-12  5 2019-01  2 2019-04  1 2019-05  2 2019-06  1 2019-11  1 2019-12  3
标签
博客 java linux springboot 笔记 问题 cloud
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。