一.JSP九大内置对象和四大作用域
1.九大内置对象
名称 | 类型 | 含义 | 获取方式 |
---|---|---|---|
request | HttpServletRequest | 封装所有请求信息 | 方法参数 |
response | HttpServletResponse | 封装所有响应信息 | 方法参数 |
session | HttpSession | 封装所有会话信息 | req.getSession() |
application | ServletContext | 封装所有信息 | req.getServletContext() |
out | PrintWriter | 输出对象 | resp.getWriter() |
exception | Exception | 异常对象 | |
page | Object | 当前页面对象 | |
pageContext | pageContext | 获取其他对象 | |
config | ServletConfig | 配置信息 |
2.四大作用域
- page
在当前页面不会重新实例化 - request
在一次请求中是同一个对象,下一次请求重新实例化 - session
(1) 一次会话内有效
(2) 只要客户端Cookie中传递的Jsessionid不变,Session不会重新实例化(不超过默认时间)
(3) 实际有效时间
浏览器关闭,Cookie失效。
默认时间,在时间范围内无任何交互。
在tomcat的web.xml中配置
<session-config>
<session-timeout>30</session-timeout>
</session-config>
- application
只有在tomcat启动项目时才实例化,关闭tomcat时销毁application
二.SpringMVC作用域传值的几种方式
在开发中,大部分是使用ajax技术,因此这四种传递参数的方法,并不太常用。(作为了解)
2.1 使用原生Servlet
在控制器的响应的方法中添加Servlet中的一些作用域:HttpRequestServlet或者HttpSession。
【注意】在方法中,ServletContext的对象是不能作为函数参数传递的
Controller层:
@RequestMapping("demo")
public String demo(HttpServletRequest req,HttpServletResponse resp,HttpSession hs){
//request作用域
req.setAttribute("req", "req的值");
//session作用域(通过Request获取session)
HttpSession session = req.getSession();
session.setAttribute("session", "session的值");
//通过方法中传递session对象
hs.setAttribute("hs", "hs的值");
//application作用域
ServletContext sc = req.getServletContext();
sc.setAttribute("sc", "application的值");
return "/main.jsp";
}
JSP测试:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
request:${requestScope.req}<br/>
Session(request获取):${sessionScope.session}<br/>
Session(方法中传session对象):${sessionScope.hs}<br/>
application:${applicationScope.sc}
</body>
</html>
显示结果:
2.2 使用Map集合
- 把map中的内容放在request作用域中
- spring会对map集合通过BindingAwareModelMap进行实例化
Controller层:
@RequestMapping("demo1")
public String demo1(Map<String, Object> map){
System.out.println(map.getClass());
map.put("map","map的值");
return "/main.jsp";
}
JSP测试:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
map:${map}<br/>
map:${requestScope.map}
</body>
</html>
控制台:
浏览器:
2.3 使用SpringMVC中Model接口
本质也是通过request域,把内容最终放入到 request 作用域中.
Controller层:
@RequestMapping("demo2")
public String demo2(Model model){
model.addAttribute("model","model的值");
return "/main.jsp";
}
JSP测试:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
model:${model}<br/>
</body>
</html>
浏览器:
2.4 使用SpringMVC中ModelAndView类
Controller层:
@RequestMapping("/demo3")
public ModelAndView demo3(){
//参数,跳转视图
ModelAndView mav = new ModelAndView("/main.jsp");
mav.addObject("mav", "mav的值");
return mav;
}
JSP测试:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
mav:${mav}<br/>
mav:${requestScope.mav}<br/>
</body>
</html>
浏览器: