一 获取作用域中对象的数据
- 创建congtroller
@RequestMapping("/show5")
public String showInfo5(HttpServletRequest request ,Model model) {
request.setAttribute("req", "HttpServletRequest");
request.getSession().setAttribute("sess", "HttpSession");
request.getSession().getServletContext().setAttribute("con", "contenxt");
return "index5";
}
2.编写index5
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
request:<span th:text="${#httpServletRequest.getAttribute('req')}"></span>
session:<span th:text="${session.sess}"></span>
Context:<span th:text="${application.con}"></span>
</body>
</html>
二 URL表达式
- 创建controller
package com.synda.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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-URL
* @author admin
*
*/
@Controller
public class DemoController {
@RequestMapping("/{page}")
public String showInfo(@PathVariable String page ,String id) {
System.out.println(id);
return page;
}
}
2 . 页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>thymeleaf-URL</title>
</head>
<body>
<a th:href="@{http://www.baidu.com}">绝对路径</a>
<a href="http://www.baidu.com">绝对路径</a>
<!-- 相对于上下文的相对路径 -->
<a th:href="@{/show}">相对路径-相对于当前项目的根</a>
<a th:href="@{~/project2/resourcename}">相对于服务器路径的根</a>
<!-- 两个参数则在中间加逗号 -->
<a th:href="@{/show(id=1,name='chsn')}">参数传递</a>
<!-- -->
</body>
</html>