/**PageBeginHtml Block Begin **/ /***自定义返回顶部小火箭***/ /*生成博客目录的JS 开始*/ /*生成博客目录的JS 结束*/

springboot 整合 thymeleaf

一:用idea 创建 springboot 项目:

详情请参考:《使用IDEA创建一个springboot项目


二:具体代码内容:


1:看项目代码结构

image


2:看项目启动访问效果

image


YHXJ{`[E_$~Y[HX@S2`_S7A

<--th:text 输出标签--> hello
<--th:text 输出标签--> Thymeleaf第一个案例!
<--th:value 可以将一个值放入到html的input标签value中-->
Thymeleaf第一个案例!
<--${#strings.isEmpty(msg)} 判断字符串是否为空--> false
<--${#strings.isEmpty(msg)} 判断字符串是否包含某个字符串内容为空--> false
<--${#strings.isEmpty(msg)} 判断字符串是否包含某个字符串内容为空:严格区分大小写--> true
<--${#strings.isEmpty(msg)} 判断字符串是否包含某个字符串内容为空:严格区分大小写--> false
<--${#strings.startsWith(msg)}判断字符串msg 是否以子字符串'a'开头。如果是返回 true反之返回 false.--> false
<--${#strings.startsWith(msg)} 判断字符串msg 是否以子字符串'a'开头。如果是返回 true反之返回 false.--> false
<--${#strings.endsWith(msg)} 判断字符串msg 是否以子字符串'a'结尾。如果是返回 true反之返回 false.--> false
<--${#strings.endsWith(msg)} 判断字符串msg的长度--> 15
<--${#strings.indexOf(msg,'a')} 判断字符串msg中某个指定字符串的下标位置 如果没有找到则返回-1--> 7
<--${#strings.substring(msg,13)} 截取子串,用户与 jdk String 类下 SubString 方法相同--> 例!
<--${#strings.substring(msg,13,15)} 截取子串,用户与 jdk String 类下 SubString 方法相同--> 例!
<--字符串转大小写--> THYMELEAF第一个案例!
<--${#strings.toLowerCase(msg)} 字符串转大小写--> thymeleaf第一个案例!
<--${#dates.format(key)} 格式化日期,默认的以浏览器默认语言为格式化标准--> 2021年2月20日 上午12时40分47秒
<--${#dates.format(key,'yyy/MM/dd')} 格式化日期,默认的以浏览器默认语言为格式化标准--> 2021/02/20
<--${#dates.year(key)} 格式化日期,year:取年--> 2021
<--${#dates.month(key)} 格式化日期,Month:取月--> 2
<--${#dates.day(key)} 格式化日期,Day:取日--> 20
<--条件判断 th:if--> 性别:男
<--条件判断 th:switch-->
ID 为 1
<--迭代遍历 th:each 迭代 list-->
ID	Name	Age
1	张三	20
2	李四	22
3	王五	24
<--迭代遍历 ht:each 状态变量
状态变量属性
1,index:当前迭代器的索引 从 0 开始
2,count:当前迭代对象的计数 从 1 开始
3,size:被迭代对象的长度
4,even/odd:布尔值,当前循环是否是偶数/奇数 从 0 开始
5,first:布尔值,当前循环的是否是第一条,如果是返回 true 否则返回 false
6,last:布尔值,当前循环的是否是最后一条,如果是则返回 true 否则返回 false

-->
ID	Name	Age	Index	Count	Size	Even	Odd	First	lase
1	张三	20	0	1	3	false	true	true	false
2	李四	22	1	2	3	true	false	false	false
3	王五	24	2	3	3	false	true	false	true
<--迭代遍历 th:each 迭代 Map-->
ID	Name	Age
u1=Users{userid=1, username='张三', userage=20}
u2=Users{userid=2, username='李四', userage=22}
u3=Users{userid=3, username='王五', userage=24}
ID	Name	Age
1	张三	20
2	李四	22
3	王五	24
<--域对象操作--> Request:HttpServletRequest
<--HttpSession--> Session:HttpSession
<--ServletContext--> Application:Application
<-- URL 表达式
th:href
th:src
url 表达式语法
基本语法:@{}
--> URL 类型
绝对路径
绝对路径
相对路径
1)相对于当前项目的根
相对于项目的上下文的相对路径
相对路径 2) 相对于服务器路径的根
相对于服务器的根 在 url 中实现参数传递
相对路径-传参 在 url 中通过 restful 风格进行参数传递
相 对 路 径 -传参-restful



DemoController

package com.alancode.springboot.controller;
/**
 * @author Alan_liu
 * @Create 2021/2/18 23:03
 */

import com.alancode.springboot.pojo.Users;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 *@program: demo
 *@description: thymeleaf 入门案例
 *@author: Alan_Liu
 *@create: 2021-02-18 23:03
 */
@Controller
public class DemoController {

	@RequestMapping("/show1")
	public  String ShowInfo(HttpServletRequest request, HttpServletResponse response, Model model){
		model.addAttribute("msg","Thymeleaf第一个案例!");
		model.addAttribute("key",new Date());
		model.addAttribute("sex","");
		model.addAttribute("id","1");
		List<Users> list = new ArrayList<>();
		list.add(new Users(1,"张三",20));
		list.add(new Users(2,"李四",22));
		list.add(new Users(3,"王五",24));
		model.addAttribute("list", list);
		List<Users> list1 = new ArrayList<>();
		list1.add(new Users(1,"张三",20));
		list1.add(new Users(2,"李四",22));
		list1.add(new Users(3,"王五",24));
		model.addAttribute("list1", list1);
		Map<String, Users> map = new HashMap<>();
		map.put("u1", new Users(1,"张三",20));
		map.put("u2", new Users(2,"李四",22));
		map.put("u3", new Users(3,"王五",24));
		model.addAttribute("map", map);

		request.setAttribute("req", "HttpServletRequest");
		request.getSession().setAttribute("sess", "HttpSession");
		request.getSession().getServletContext().setAttribute("app","Application");
		return "index";
	}





}



Users

package com.alancode.springboot.pojo;
/**
 * @author Alan_liu
 * @Create 2021/2/16 21:44
 */

/**
 *@program: SpringBootAddJsp
 *@description: 用户
 *@author: Alan_Liu
 *@create: 2021-02-16 21:44
 */
public class Users {

	private Integer userid;
	private String  username;
	private Integer userage;

	public Integer getUserid() {
		return userid;
	}

	public void setUserid(Integer userid) {
		this.userid = userid;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Integer getUserage() {
		return userage;
	}

	public void setUserage(Integer userage) {
		this.userage = userage;
	}

	@Override
	public String toString() {
		return "Users{" +
				"userid=" + userid +
				", username='" + username + '\'' +
				", userage=" + userage +
				'}';
	}

	public Users() {
	}

	public Users(Integer userid, String username, Integer userage) {
		this.userid = userid;
		this.username = username;
		this.userage = userage;
	}

	public Users(Integer userid) {
		this.userid = userid;
	}

	public Users(Integer userid, String username) {
		this.userid = userid;
		this.username = username;
	}

	public Users(String username) {
		this.username = username;
	}

	public Users(String username, Integer userage) {
		this.username = username;
		this.userage = userage;
	}
}


ThymeleafApplication

package com.alancode.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ThymeleafApplication {

	public static void main(String[] args) {
		SpringApplication.run(ThymeleafApplication.class, args);
	}

}

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>thymeleaf入门</title>

</head>
<body>
    <--th:text  输出标签-->
    <span  th:text="hello"></span>
    <hr/>
    <--th:text  输出标签-->
    <span  th:text="${msg}"></span>
    <hr/>
    <--th:value  可以将一个值放入到html的input标签value中-->
    <input  type="text" name="username" th:value="${msg}"/>
    <hr/>
    <--${#strings.isEmpty(msg)}  判断字符串是否为空-->
    <span  th:text="${#strings.isEmpty(msg)}"></span>

    <hr/>
    <--${#strings.isEmpty(msg)}  判断字符串是否包含某个字符串内容为空-->
    <span  th:text="${#strings.contains(msg,'9')}"></span>
    <br/>
    <--${#strings.isEmpty(msg)}  判断字符串是否包含某个字符串内容为空:严格区分大小写-->
    <span  th:text="${#strings.contains(msg,'T')}"></span>
    <br/>
    <--${#strings.isEmpty(msg)}  判断字符串是否包含某个字符串内容为空:严格区分大小写-->
    <span  th:text="${#strings.contains(msg,'t')}"></span>
    <br/>
    <--${#strings.startsWith(msg)}判断字符串msg 是否以子字符串'a'开头。如果是返回 true反之返回 false.-->
    <span  th:text="${#strings.startsWith(msg,'a')}"></span>
    <br/>
    <--${#strings.startsWith(msg)}  判断字符串msg 是否以子字符串'a'开头。如果是返回 true反之返回 false.-->
    <span  th:text="${#strings.startsWith(msg,'b')}"></span>
    <br/>
    <--${#strings.endsWith(msg)}  判断字符串msg 是否以子字符串'a'结尾。如果是返回 true反之返回 false.-->
    <span  th:text="${#strings.endsWith(msg,'b')}"></span>
    <br/>
    <--${#strings.endsWith(msg)}  判断字符串msg的长度-->
    <span  th:text="${#strings.length(msg)}"></span>
    <br/>
    <--${#strings.indexOf(msg,'a')}  判断字符串msg中某个指定字符串的下标位置  如果没有找到则返回-1-->
    <span  th:text="${#strings.indexOf(msg,'a')}"></span>
    <br/>
    <--${#strings.substring(msg,13)} 截取子串,用户与 jdk String 类下 SubString 方法相同-->
    <span  th:text="${#strings.substring(msg,13)}"></span>
    <br/>
    <--${#strings.substring(msg,13,15)} 截取子串,用户与 jdk String 类下 SubString 方法相同-->
    <span  th:text="${#strings.substring(msg,13,15)}"></span>
    <br/>
    <--字符串转大小写-->
    <span  th:text="${#strings.toUpperCase(msg)}"></span>
    <br/>
    <--${#strings.toLowerCase(msg)} 字符串转大小写-->
    <span  th:text="${#strings.toLowerCase(msg)}"></span>
    <br/>
    <hr/>
    <--${#dates.format(key)} 格式化日期,默认的以浏览器默认语言为格式化标准-->
    <span  th:text="${#dates.format(key)}"></span>
    <br/>
    <--${#dates.format(key,'yyy/MM/dd')} 格式化日期,默认的以浏览器默认语言为格式化标准-->
    <span  th:text="${#dates.format(key,'yyy/MM/dd')}"></span>
    <br/>
    <--${#dates.year(key)} 格式化日期,year:取年-->
    <span  th:text="${#dates.year(key)}"></span>
    <br/>
    <--${#dates.month(key)} 格式化日期,Month:取月-->
    <span  th:text="${#dates.month(key)}"></span>
    <br/>
    <--${#dates.day(key)} 格式化日期,Day:取日-->
    <span  th:text="${#dates.day(key)}"></span>
    <br/>
    <hr/>
    <--条件判断 th:if-->
    <span th:if="${sex} == '男'">
        性别:男
    </span>
    <span th:if="${sex} == '女'">
         性别:女
    </span>
    <hr/>
    <--条件判断 th:switch-->
    <div th:switch="${id}">
        <span th:case="1">ID 为 1</span>
        <span th:case="2">ID 为 2</span>
        <span th:case="3">ID 为 3</span>
    </div>
    <hr/>
    <--迭代遍历 th:each 迭代 list-->
    <table border="1">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Age</th>
        </tr>
        <tr th:each="u : ${list}">
            <td th:text="${u.userid}"></td>
            <td th:text="${u.username}"></td>
            <td th:text="${u.userage}"></td>
        </tr>
    </table>
    <hr/>
    <--迭代遍历 ht:each 状态变量<br/>
    状态变量属性<br/>
    1,index:当前迭代器的索引 从 0 开始<br/>
    2,count:当前迭代对象的计数 从 1 开始<br/>
    3,size:被迭代对象的长度<br/>
    4,even/odd:布尔值,当前循环是否是偶数/奇数 从 0 开始<br/>
    5,first:布尔值,当前循环的是否是第一条,如果是返回 true 否则返回 false<br/>
    6,last:布尔值,当前循环的是否是最后一条,如果是则返回 true 否则返回 false<br/>
    <br/>
    -->
    <table border="1">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Age</th>
            <th>Index</th>
            <th>Count</th>
            <th>Size</th>
            <th>Even</th>
            <th>Odd</th>
            <th>First</th>
            <th>lase</th>
        </tr>
        <tr th:each="u1,var : ${list1}">
            <td th:text="${u1.userid}"></td>
            <td th:text="${u1.username}"></td>
            <td th:text="${u1.userage}"></td>
            <td th:text="${var.index}"></td>
            <td th:text="${var.count}"></td>
            <td th:text="${var.size}"></td>
            <td th:text="${var.even}"></td>
            <td th:text="${var.odd}"></td>
            <td th:text="${var.first}"></td>
            <td th:text="${var.last}"></td>
        </tr>
    </table>
    <hr/>
    <--迭代遍历   th:each 迭代 Map-->
    <table border="1">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Age</th>
        </tr>
        <tr th:each="maps : ${map}">
            <td th:text="${maps}"></td>
        </tr>
    </table>
    <hr/>
    <table border="1">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Age</th>
        </tr>
        <tr th:each="maps : ${map}">
            <td th:each="entry:${maps}"
                th:text="${entry.value.userid}" ></td>
            <td th:each="entry:${maps}"
                th:text="${entry.value.username}"></td>
            <td th:each="entry:${maps}"
                th:text="${entry.value.userage}"></td>
        </tr>
    </table>
    <hr/>
    <--域对象操作-->
    Request:<span
            th:text="${#httpServletRequest.getAttribute('req')}"></span><br/>
    <hr/>
    <--HttpSession-->
    Session:<span th:text="${session.sess}"></span><br/>
    <hr/>
    <--ServletContext-->
    Application:<span th:text="${application.app}"></span>
    <hr/>
     <--
     URL 表达式<br/>
         th:href<br/>
         th:src<br/>
     url 表达式语法<br/>
         基本语法:@{}<br/>
     -->
    URL 类型<br/>
      绝对路径<br/>
    <a th:href="@{http://www.baidu.com}">绝对路径</a><br/>
       相对路径<br/>
    1)相对于当前项目的根<br/>
    相对于项目的上下文的相对路径<br/>
    <a th:href="@{/show}">相对路径</a>
    2) 相对于服务器路径的根<br/>
    <a th:href="@{~/project2/resourcename}">相对于服务器的根</a>
      在 url 中实现参数传递<br/>
    <a th:href="@{/show(id=1,name=zhagnsan)}">相对路径-传参</a>
      在 url 中通过 restful 风格进行参数传递<br/>
    <a th:href="@{/path/{id}/show(id=1,name=zhagnsan)}"> 相 对 路 径 -传参-restful</a>
    <hr/>
</body>
</html>


POM。xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.alanCode.springBoot</groupId>
    <artifactId>SpringbootAddViewThymeleaf</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringbootAddViewThymeleaf</name>
    <description>SpringbootAddViewThymeleaf</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--Springboot 启动器-->
        <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
posted @ 2021-02-20 00:46  一品堂.技术学习笔记  阅读(197)  评论(0编辑  收藏  举报