SpringBoot
变量输出与字符串操作
th:text
th:text | |
---|---|
在页面中输出值 |
th:value
th:value | |
---|---|
可以将一个值放入到input标签的value中 |
判断字符串是否为空
thymeleaf内置对象
注意语法:
1,调用内置对象一定要用#
2,大部分的内置对象都以s结尾strings、numbers、dates’
${#strings.isEmpty(msg)} | |
---|---|
判断字符串是否为空,如果为空返回true,否则返回false |
${#strings.contain(msg,‘T’)} | |
---|---|
判断字符串是否包含指定的子串,如果包含返回true,否则返回false |
${#strings.startsWith(msg,‘T’)} | |
---|---|
判断当前字符串是否以子串开头,如果是返回true,否则返回false |
${#strings.endsWith(msg,‘案例’)} | |
---|---|
判断当前字符串是否以子串结尾,如果是返回true,否则返回false |
${#strings.length(msg)} | |
---|---|
返回字符串的长度 |
${#strings.indexOf(msg,‘h’)} | |
---|---|
查找子串的位置,并返回该子串的下标,如果没找到,返回-1 |
${#strings.substring(msg,13)}, ${#strings.substring(msg,13,14)} | |
---|---|
截取子串,用法与jdk String类下的SubString方法相同 |
${#strings.toUpperCase(msg)} ${#strings.toLowerCase(msg)} | |
---|---|
字符串转大小写 |
日期格式化处理
${#dates.format(key)} | ${#dates.format(key,‘yyyy/MM/dd’)} |
---|---|
格式化日期,默认的以浏览器默认语言为格式化标准 | 按照自定义的格式做日期转换 |
${#dates.year(key)} | ${#dates.month(key)} | ${#dates.year(key)} |
---|---|---|
按照年取日期 | 按照月取日期 | 按照日取日期 |
条件判断
th:if
th:switch
迭代遍历
th:each 迭代list
th:each的遍历
DemoController.java
package com.bjsxt.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bjsxt.pojo.Users;
/**
* Thymeleaf入门案例
* @author ouyang
*
*/
@Controller
public class DemoController {
@RequestMapping("/show")
public String showInfo(Model model) {
model.addAttribute("msg","Thymeleaf 第一个案例");
model.addAttribute("key",new Date());
return "index";
}
@RequestMapping("/show2")
public String showInfo2(Model model) {
model.addAttribute("sex","男");
model.addAttribute("id","2");
return "index2";
}
@RequestMapping("/show3")
public String showInfo3(Model model) {
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);
return "index3";
}
}
Users.java
package com.bjsxt.pojo;
public class Users {
private Integer userid;
private String username;
private Integer userage;
public Users() {
super();
}
public Users(Integer userid, String username, Integer userage) {
super();
this.userid = userid;
this.username = username;
this.userage = 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;
}
}
index3.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<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>
</body>
</html>
th:each="u,var : ${list}
u是取出的对象;
var是取出对象的各种状态;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<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>Last</th>
</tr>
<tr th:each="u,var : ${list}">
<td th:text="${u.userid}"></td>
<td th:text="${u.username}"></td>
<td th:text="${u.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>
</body>
</html>
index: 当前迭代器的索引(从0开始的);
count: 当前迭代的计数器(从1开始的);
size: 被迭代的大小;
even: 当前循环的次数是偶数,则返回true;
odd: 当前循环的次数是奇数,则返回true;
first: 当前循环为第一个,则返回true;
last: 当前循环为最后一个,则返回true;
th:each 迭代map
index4.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<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>
</body>
</html>
第一个表格取到的map对象,所有需要再进行一次迭代。
域对象操作
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Request:<span th:text="${#httpServletRequest.getAttribute('req')}"></span><br/>
Session:<span th:text="${session.sess}"></span><br/>
Application:<span th:text="${application.app}"></span>
</body>
</html>
HttpServletRequest
request.setAttribute(“req”, “HttpServletRequest”);
th:text="${#httpServletRequest.getAttribute(‘req’)}"
HttpSession
request.getSession().setAttribute(“sess”, “HttpSession”);
th:text="${session.sess}"
ServletContext
request.getSession().getServletContext().setAttribute(“app”, “Application”);
th:text="${application.app}"
URL表达式
th:href
th:src
url 表达式语法
基本语法: @{}
url类型
绝对路径
<a th:href="@{http://www.baidu.com}">绝对路径</a><br/>
相对路径
1)相对于当前项目的根
相对于项目的上下文的相对路径
<a th:href="@{/show}">相对路径</a>
2)相对于服务器路径的根(位于同一tomcat)
<a th:href="@{~/project2/resourcename}">相对于服务器的根</a>
在url中实现参数传递
<a th:href="@{/show(id=1,name=zhangsan)}">相对路径-传参</a>
在url中通过restful风格进行参数传递
<a th:href="@{/path/{id}/show(id=1,name=zhangsan)}">相对路径-传参-restful</a>
代码
DemoController.java
package com.bjsxt.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Thymeleaf
* @author ouyang
*
*/
@Controller
public class DemoController {
@RequestMapping("/{page}")
public String showInfo(@PathVariable String page,Integer id,String name) {
System.out.println(id+"--"+name);
return page;
}
}
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Thymeleaf-Url</title>
</head>
<body>
<a th:href="@{http://www.baidu.com}">绝对路径</a><br/>
<hr/>
<a th:href="@{/show}">相对路径</a>
<hr/>
<a th:href="@{~/project2/resourcename}">相对于服务器的根</a>
<hr/>
<a th:href="@{/show(id=1,name=zhangsan)}">相对路径-传参</a>
<hr/>
<a th:href="@{/path/{id}/show(id=1,name=zhangsan)}">相对路径-传参-restful</a>
</body>
</html>
show.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Page show</title>
</head>
<body>
show Page
</body>
</html>