Spring Boot入门(三)视图层技术
一、SpringBoot 整合 jsp 技术
1.创建项目,这里就不创建类,可以参考入门一,翻看往期文章
2.修改 pom 文件,添加坐标
<!-- jstl坐标 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- jasper坐标 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
3 .创建全局配置文件,文件放在resources目录下
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
4.创建一个 Controller
/**
* 整合jsp
* @author xukang
*
*/
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/getUsetList")
public String getUsetList(Model model)throws Exception{
List<Users> list = new ArrayList<>();
list.add(new Users(1,"小米",20));
list.add(new Users(2,"华为",22));
list.add(new Users(3,"vivo",24));
model.addAttribute("list", list);
return "userList";//跳转视图
}
}
5.创建 jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title>
</head>
<body>
<table border="1" align="center" width="50%">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<c:forEach items="${list }" var="user">
<tr>
<td>${user.userid }</td>
<td>${user.username }</td>
<td>${user.userage }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
6、创建启动器
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
二,SpringBoot 整合 Freemarker
1、创建项目,步骤略
2、修改 pom 添加坐标
<!-- freemarker 启动器的坐标 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
3、编写视图
注意:springBoot 要求模板形式的视图层技术的文件必须要放到 src/main/resources 目录下必须要一个名称为 templates
<html>
<head>
<title>展示用户数据</title>
<meta charset="utf-9" />
</head>
<body>
<table border="1" align="center" width="50%">
<tbody>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr> <#list list as user >
<tr>
<td>${user.userid}</td>
<td>${user.username}</td>
<td>${user.userage}</td>
</tr>
<!--#list-->
</tbody>
</table>
</body>
</html>
4、创建 Controller,同上
5、创建启动器,同上
三,SpringBoot 整合 Thymeleaf
1. 创建 Thymeleaf 的入门项目
1.创建项目
2.修改 pom 文件添加坐标
<!-- springBoot 的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3 创建存放视图的目录
目录位置:src/main/resources/templates
templates:该目录是安全的。意味着该目录下的内容是不允许外界直接访问的。
2. Thymeleaf 的基本使用
1、Thymeleaf 特点:
Thymelaef 是通过他特定语法对 html 的标记做渲染
2、 编写 Controller
/**
* Thymeleaf 入门案例 *
*
*/
@Controller
public class DemoController {
@RequestMapping("/show")
public String showInfo(Model model) {
model.addAttribute("msg", "Thymeleaf 第一个案例");
return "index";
}
}
3、 创建视图 .html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Thymeleaf 入门</title>
</head>
<body>
<span th:text="Hello"></span>
<hr />
<span th:text="${msg}"></span>
</body>
</html>
4、 编写启动类
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
5、如果出现标记异常可以换跟高版本
<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>
也可以直接地位跟高的版本的坐标
3. Thymeleaf 语法详解
1.变量输出与字符串操作
th:text 在页面中输出值
th:value 可以将一个值放入到 input 标签的 value 中
2.判断字符串是否为空
Thymeleaf 内置对象
注意语法:
1,调用内置对象一定要用#
2,大部分的内置对象都以 s 结尾 strings、numbers、dates
${#strings.isEmpty(key)}:判断字符串是否为空,如果为空返回 true,否则返回 false
${#strings.contains(msg,'T')}:判断字符串是否包含指定的子串,如果包含返回 true,否则返回 false
${#strings.startsWith(msg,'a')}:判断当前字符串是否以子串开头,如果是返回 true,否则返回 false
${#strings.endsWith(msg,'a')}:判断当前字符串是否以子串结尾,如果是返回 true,否则返回 false
${#strings.length(msg)}:返回字符串的长度
${#strings.indexOf(msg,'h')}:查找子串的位置,并返回该子串的下标,如果没找到则返回-1
${#strings.substring(msg,13)}、${#strings.substring(msg,13,15)}:截取子串,用户与 jdk String 类下 SubString 方法相同
${#strings.toUpperCase(msg)}、${#strings.toLowerCase(msg)}:字符串转大小写。
3. 日期格式化处理
${#dates.format(key)}:格式化日期,默认的以浏览器默认语言为格式化标准
${#dates.format(key,'yyy/MM/dd')}:按照自定义的格式做日期转换
${#dates.year(key)}
${#dates.month(key)}
${#dates.day(key)}
year:取年 Month:取月 Day:取日
4.条件判断
th:if、th:switch
<span th:if="${sex} == '男'"> 性别:男
</span>
<span th:if="${sex} == '女'">
性别:女
</span>
<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>
5.迭代遍历
<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>
6.ht:each 状态变量
<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="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>
状态变量属性
1,index:当前迭代器的索引 从 0 开始
2,count:当前迭代对象的计数 从 1 开始
3,size:被迭代对象的长度
4,even/odd:布尔值,当前循环是否是偶数/奇数 从 0 开始 5,first:布尔值,当前循环的 是否是第一条,如果是返回 true 否则返回 false 6,last:布尔值,当前循环的是否是最后一条,如果是则返回 true 否则返回 false
7.域对象操作
1.httpServletRequest
request.setAttribute("req","HttpServletRequest");
Request:<span th:text ="${#httpServletRequest.getAttribute('req')}"></span><br/>
2.HttpSession
request.getSession().setAttribute("sess" ,"HttpSession");
Session:<spanth:text="${session.sess}"></span><br/>
3.ServletContext
request.getSession().getServletContext().setAttribute("app","Application");
Application:<spanth:text="${application.app}"></span>
8.URL 表达式
th:href
th:src
th:href="@{http://www.baidu.com}:绝对路径
th:href="@{/show}">:相对路径
th:href="@{~/project2/resourcename}
相对于服务器的根
h:href="@{/show(id=1,name=zhagnsan)}:相对路径传参
th:href=a>"@{/path/{id}/show(id=1,name=zhagnsan)}:相对路径传参、restful 风格
上一篇:Spring Boot入门(二)Servlet、Filter、Listener、访问静态资源、文件上传
下一篇:标题Spring Boot入门(四)整合 SpringMVC+MyBatis