设置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"; // 此处只返回一个路径, 该路径没有设置后缀,后缀默认是*.html
}
<p th:text="${#httpServletRequest.getAttribute('request')}"/>
<p th:text="${#httpSession.getAttribute('session')}"/>
<p th:text="${#httpServletRequest.getServletContext().getRealPath('application')}"/>
<hr/>
<p th:text="'官方网站:' + ${url2}"/>
<p th:text="'request:' + ${request}"/>
<p th:text="'session:' + ${session.session}"/>
实体对象传递,进行前台的逻辑判断
@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="*">没有匹配的数据</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 集合传递前台进行方法 各种方法进行数据处理
@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 可以不改变方法进行获取-->
<!--同时可以进行下标进行获取-->
</body>
</html>
list 集合传递前台进行方法 各种方法进行数据处理
@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>偶数</td>
<td>奇数</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 集合传递前台进行方法 各种方法进行数据处理
@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>
<!-- 引入页面后进行处理 />
<!--<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>偶数</td>
<td>奇数</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 引入页面进行使用
<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>