設(shè)置request僵刮,session, application 的作用域
@RequestMapping(value = "/inner", method = RequestMethod.GET)
public String inner(HttpServletRequest request,Model model) {
request.setAttribute("request","request");
request.getSession().setAttribute("session","session");
request.getServletContext().setAttribute("application","application");
model.addAttribute("url", "www.mldn.com"); // request屬性傳遞包裝
model.addAttribute("url2", "<span style='color:red'>www.mldn.cn</span>");
return "message_show_inner"; // 此處只返回一個路徑蓉坎, 該路徑?jīng)]有設(shè)置后綴割疾,后綴默認(rèn)是*.html
}
<p th:text="${#httpServletRequest.getAttribute('request')}"/>
<p th:text="${#httpSession.getAttribute('session')}"/>
<p th:text="${#httpServletRequest.getServletContext().getRealPath('application')}"/>
<hr/>
<p th:text="'官方網(wǎng)站:' + ${url2}"/>
<p th:text="'request:' + ${request}"/>
<p th:text="'session:' + ${session.session}"/>
實體對象傳遞却盘,進(jìn)行前臺的邏輯判斷
@GetMapping(value = "/memberShow")
public String memberShoe(Model model){
Member vo =new Member();
vo.setMid(101L);
vo.setName("張三");
vo.setAge(9);
vo.setSalaryl(999.99);
vo.setBrithday(new Date());
model.addAttribute("member",vo);
return "member_show";
}
<span th:unless="${member.age > 18}">不滿</span> <hr/>
<!--取反值拴疤,如: if(9>18)取9<18-->
<span th:if="${member.age < 18}">未成年人</span><hr/>
<span th:if="${member.name eq '張三'}">歡迎張三訪問</span><hr/>
<span th:switch="${member.mid}">
<p th:case="101">101</p>
<p th:case="102">102</p>
<p th:case="*">沒有匹配的數(shù)據(jù)</p>
</span>
<!--<div th:object="${member}">
<p th:text="'用戶編號:'+*{mid}"/>
<p th:text="'用戶姓名:'+*{name}"/>
<p th:text="'用戶年齡:'+*{age}"/>
<p th:text="'用戶工資:'+*{salaryl}"/>
<p th:text="'用戶工資:'+*{brithday}"/>
<p th:text="'用戶生日:'+*{#dates.format(brithday,'yyyy-MM-dd')}"/>
</div>-->
set 集合傳遞前臺進(jìn)行方法 各種方法進(jìn)行數(shù)據(jù)處理
@GetMapping(value = "/member/set")
public String set(Model model){
Set<String> allNames =new HashSet<String>();
/*Set<Integer> allIds =new HashSet<Integer>();*/
List<Integer> allIds =new ArrayList<Integer>();
for (int i = 0; i < 5; i++) {
allNames.add("mldn-"+i);
allIds.add(i);
}
model.addAttribute("names",allNames);
model.addAttribute("allIds",allIds);
model.addAttribute("myDate",new Date());
return "member/member_set";
}
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模版渲染</title>
<script type="text/javascript" th:src="@{js/main.js}"></script>
<link rel="icon" type="image/x-icon" href="/images/mldn.ico"/>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<p th:text="${#dates.format(myDate,'yyyy-MM-dd')}"/>
<p th:text="${#dates.format(myDate,'yyyy-MM-dd HH:mm:ss.SSS')}"/>
<hr/>
<p th:text="${#strings.replace('www.mldn.cn','.','$')}"/>
<p th:text="${#strings.toUpperCase('www.mldn.cn')}"/>
<p th:text="${#strings.trim('www.ml dn.cn')}"/>
<hr/>
<p th:text="${#sets.contains(names,'mldn-0')}"/>
<p th:text="${#sets.contains(names,'mldn-9')}"/>
<p th:text="${#sets.size(names)}"/>
<p th:text="${#sets.contains(allIds,0)}"/>
<p th:text="${allIds[1]}"/>
<p th:text="${names[1]}"/>
<!--list 和 set 可以不改變方法進(jìn)行獲取-->
<!--同時可以進(jìn)行下標(biāo)進(jìn)行獲取-->
</body>
</html>
list 集合傳遞前臺進(jìn)行方法 各種方法進(jìn)行數(shù)據(jù)處理
@GetMapping(value = "/member/list")
public String list(Model model){
List<Member> allMembers =new ArrayList<Member>();
for (int i = 0; i < 10; i++) {
Member vo =new Member();
vo.setMid(101L+i);
vo.setName("張三");
vo.setAge(9);
vo.setSalaryl(999.99);
vo.setBrithday(new Date());
allMembers.add(vo);
}
model.addAttribute("member",allMembers);
return "member/member_list";
}
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模版渲染</title>
<script type="text/javascript" th:src="@{js/main.js}"></script>
<link rel="icon" type="image/x-icon" href="/images/mldn.ico"/>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<table>
<tr>
<td>No.</td>
<td>MID</td>
<td>姓名</td>
<td>年林</td>
<td>偶數(shù)</td>
<td>奇數(shù)</td>
</tr>
<tr th:each="member,memberStat:${member}">
<td th:text="${memberStat.index+1}"/>
<td th:text="${member.mid}"/>
<td th:text="${member.name}"/>
<td th:text="${member.age}"/>
<td th:text="${memberStat.even}"/>
<td th:text="${memberStat.odd}"/>
</tr>
</table>
</body>
</html>
map 集合傳遞前臺進(jìn)行方法 各種方法進(jìn)行數(shù)據(jù)處理
@GetMapping(value = "/member/map")
public String map(Model model){
Map<String,Member> allMembers =new HashMap<String, Member>();
for (int i = 0; i < 10; i++) {
Member vo =new Member();
vo.setMid(101L+i);
vo.setName("張三");
vo.setAge(9);
vo.setSalaryl(999.99);
vo.setBrithday(new Date());
allMembers.put("mldn-"+i,vo);
}
model.addAttribute("member",allMembers);
return "member/member_map";
}
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模版渲染</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<!-- 引入頁面后進(jìn)行處理 />
<!--<div th:replace="@{/commons/footer} :: compayInfo"/>-->
<!--<div th:include="@{/commons/footer} :: compayInfo" th:with="itemid=2,subid=20"/>-->
<p th:text="${#maps.containsKey(member,'mldn-7')}"/>
<p th:text="${member['mldn-7'].name}"/>
<hr/>
<table>
<tr>
<td>No.</td>
<td>KEY</td>
<td>MID</td>
<td>姓名</td>
<td>年林</td>
<td>偶數(shù)</td>
<td>奇數(shù)</td>
</tr>
<tr th:each="memberEntry,memberStat:${member}">
<td th:text="${memberStat.index+1}"/>
<td th:text="${memberEntry.key}"/>
<td th:text="${memberEntry.value.mid}"/>
<td th:text="${memberEntry.value.name}"/>
<td th:text="${memberEntry.value.age}"/>
<td th:text="${memberStat.even}"/>
<td th:text="${memberStat.odd}"/>
</tr>
</table>
</body>
</html>
map 引入頁面進(jìn)行使用
<meta http-equiv="Content-Type" charset="text/html;charset=UTF-8">
<footer th:fragment="compayInfo">
<p>魔樂科技(www.mldn.co)</p>
<p th:text="${itemid}"/>
<p th:text="${subid}"/>
</footer>