Spring Boot 第三天

既然沒有後路,那就昂首挺胸,勇往直前,加油!(任一一)

一、 SpringBoot整合jsp

1.創建項目
2.修改pom文件

<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
http://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>1.5.10.RELEASE</version>
</parent>
	<groupId>com.bjsxt</groupId>
	<artifactId>08-spring-boot-view-jsp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
<!-- jdk1.7 -->
<properties>
	<java.version>1.7</java.version>
</properties>
<dependencies>
<!-- springBoot 的啓動器 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 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>
</dependencies>
</project>

3.創建 springBoot 的全局配置文件,application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

4.創建Controller

/**
* SpringBoot 整合 jsp
*
*
*/
@Controller
public class UserController {
/*
* 處理請求,產生數據
*/
@RequestMapping("/showUser")
public String showUser(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 對象
	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>I nsert 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.創建啓動類

/**
* SpringBoot 啓動類

*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
	SpringApplication.run(App.class, args);
	}
}

二、SpringBoot 整合 Freemarker

1,創建項目
2,修改 pom 添加座標

<dependencies>
		<!-- spring-boot啓動器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- freemarker啓動器座標 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>
	</dependencies>

3,編寫視圖

<html>
	<head>
		<title>展示用戶數據</title>
		<meta charset="utf-9"></meta>
	</head>

	<body>
		<table border="1" align="center" width="50%">
			<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>
		</table>
	</body>

</html>

4,創建 Controller

@RequestMapping("/showUser")
	public String showUser(Model model){
			List<Users> list=new ArrayList<>();
			list.add(new Users(1,"佔山",20));
			list.add(new Users(2,"黑鐵",25));
			list.add(new Users(3,"三兒",45));
			//將list數據傳遞給jsp對象,所以需要一個model對象
			model.addAttribute("list",list);
			//跳轉視圖
			return "userList";
	}

5,創建啓動器

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

參考目錄:
參考目錄

SpringBoot 整合 Thymeleaf0(重點理解記憶)

  1. 創建項目
  2. 修改pom文件
<!-- springBoot 的啓動器 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf 的啓動器 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3.創建存放視圖的目錄
目錄位置:src/main/resources/templates
templates:該目錄是安全的。意味着該目錄下的內容是不允許外界直接訪問的。

4.Thymeleaf 的基本使用

4.1 Thymeleaf 特點:
Thymelaef 是通過他特定語法對 html 的標記做渲染。
4.2 編寫 Controller

/**
* Thymeleaf 入門案例

*/
@Controller
public class DemoController {
@RequestMapping("/show")
	public String showInfo(Model model){
		model.addAttribute("msg", "Thymeleaf 第一個案例");
		return "index";
	}
}

4.3 創建視圖 .html

<span th:text="Hello"></span>
<hr/>
<span th:text="${msg}"></span>

4.4 編寫啓動類

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

在這裏插入圖片描述
解決方案:
1.讓 html 的標記按照嚴禁的語法去編寫。
在這裏插入圖片描述
2.Thymeleaf.jar:更新爲 3.0 以上的版本
thymeleaf-layout-dialect.jar:更新爲 2.0 以上的版本

更換 thymeleaf 的 jar 包的版本

<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>

5.Thymeleaf 語法詳解

5.1變量輸出與字符串操作
th:text 在頁面中輸出值
th:value 可以將一個值放入到 input 標籤的 value 中
5.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.length(msg)} 返回字符串的長度
${#strings.endsWith(msg,‘a’)} 判斷當前字符串是否以子串結尾,如果是返回 true,否則返回 false
${#strings.indexOf(msg,‘h’)} 查找子串的位置,並返回該子串的下標,如果沒找到則返回-1
${#strings.substring(msg,13)} 截取子串,用戶與 jdk String 類下 SubString 方法相同
${#strings.substring(msg,13,15)} 截取子串,用戶與 jdk String 類下 SubString 方法相同
${#strings.toUpperCase(msg)} 字符串轉大小寫。
${#strings.toLowerCase(msg)} 字符串轉大小寫。
5.3日期格式化處理
${#dates.format(key)} 格式化日期,默認的以瀏覽器默認語言爲格式化標準
${#dates.format(key,‘yyy/MM/dd’)} 按照自定義的格式做日期轉換
${#dates.year(key)} 取年
${#dates.month(key)} 取月
${#dates.day(key)} 取天
5.4條件判斷

5.4.1 th:if

<span th:if="${sex} == '男'">
	性別:男
</span>
<span th:if="${sex} == '女'">
	性別:女
</span>

5.4.2th:switch

<div th:switch="${id}">
	<span th:case="1">ID 爲 1</span>
	<span th:case="2">ID 爲 2</span>
</div>
5.5 迭代遍歷

5.5.1 th:each

@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";
}
//index3.html
<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>
5.6 ht:each 狀態變量
@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";
}
//
<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
5.7 th:each 迭代 Map
@RequestMapping("/show5")
	public String showInfo5(Model model) {
		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, "王五", 25));
		model.addAttribute("map", map);
		return "index5";
	}
	//index5.html
	<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>
	<th />
	<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>
5.8 域對象的操作

5.8.1 HttpServletRequest

@RequestMapping("/show6")
	public String showInfo6(HttpServletRequest request, Model model) {
		request.setAttribute("req", "HttpServletRequest");
		request.getSession().setAttribute("sess", "HttpSession");
		request.getSession().getServletContext().setAttribute("app", "Application");
		return "index6";
	}
	//index6.html
	Request:
		<span th:text="${#httpServletRequest.getAttribute('req')}"></span>
	<br /> 
	Session:
		<span th:text="${session.sess}"></span>
	<br />
	Application:
		<span th:text="${application.app}"></span>
5.9 URL表達式

th:href

th:src

5.9.1 url 表達式語法
基本語法:@{}

5.9.2 URL類型

<!-- 絕對路徑 -->
	<a th:href="@{http://www.baidu.com}">絕對路徑</a>
	<hr />
	<!-- 相對路徑  相對於項目的根路徑,相對於項目的上下文的相對路徑-->
		<a th:href="@{/show}">相對路徑</a>
	<hr/>
	<!-- 相對於服務器的根  -->
		<a th:href="@{~/project2/resourcename}">相對於服務器的根</a>
	<hr/>
	<!--在 url 中實現參數傳遞-->
		<a th:href="@{/show(id=1,name=zhagnsan)}">相對路徑-傳參</a>
	<hr/>
	<!--在 url 中通過 restful 風格進行參數傳遞-->
		<a th:href="@{/path/{id}/show(id=1,name=zhagnsan)}"> 相 對 路 徑 - 傳 參-restful</a>

[相關項目代碼]

@RequestMapping("/show")
	public String showInfo(Model model) {
		model.addAttribute("msg", "Thymeleafjdsaidhwqhduwqwaexc ");
		model.addAttribute("key", new Date());
		return "index";
	}
//index.html
<span th:text="Hello"></span>
	<hr />
		<span th:text="${msg}"></span>
	<hr/>
		<input type="text" name="username" th:value="${msg}"/>
	<hr/>
		<span th:text="${#strings.isEmpty(msg)}"></span>
	<hr/>
		<span th:text="${#strings.contains(msg,'T')}"></span>
	<hr/>
		<span th:text="${#strings.startsWith(msg,'a')}"></span>
	<hr/>
		<span th:text="${#strings.endsWith(msg,'a')}"></span>
	<hr/>
		<span th:text="${#strings.length(msg)}"></span>
	<hr/>
		<span th:text="${#strings.indexOf(msg,'h')}"></span>
	<hr/>
		<span th:text="${#strings.substring(msg,13)}"></span>
	<hr/>
		<span th:text="${#strings.substring(msg,13,15)}"></span>
	<hr/>
		<span th:text="${#strings.toUpperCase(msg)}"></span>
	<hr/>
	
		<span th:text="${#dates.format(key)}"></span>
	<hr/>
		<span th:text="${#dates.format(key,'yyy/MM/dd')}"></span>
	<hr/>
		<span th:text="${#dates.year(key)}"></span>
	<hr/>
		<span th:text="${#dates.month(key)}"></span>
	<hr/>
		<span th:text="${#dates.day(key)}"></span>
	<hr/>
-------------------------------------------------------------------------------
	@RequestMapping("/show2")
	public String showInfo2(Model model) {
		model.addAttribute("sex", "男");
		return "index2";
	}
	//index2.html
	<span th:if="${sex} == '男'"> 性別:男 </span>
	<span th:if="${sex} == '女'"> 性別:女 </span>
-------------------------------------------------------------------------------
	@RequestMapping("/show3")
	public String showInfo3(Model model) {
		model.addAttribute("id", "1");
		return "index3";
	}
	//index3.html
	<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>
-------------------------------------------------------------------------------
	@RequestMapping("/show4")
	public String showInfo4(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 "index4";
	}	
-------------------------------------------------------------------------------
	@RequestMapping("/show5")
	public String showInfo5(Model model) {
		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, "王五", 25));
		model.addAttribute("map", map);
		return "index5";
	}
	
-------------------------------------------------------------------------------
	@RequestMapping("/show6")
	public String showInfo6(HttpServletRequest request, Model model) {
		request.setAttribute("req", "HttpServletRequest");
		request.getSession().setAttribute("sess", "HttpSession");
		request.getSession().getServletContext().setAttribute("app", "Application");
		return "index6";
	}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章