SpringMVC(七)——SpringMVC作用域传值

一.JSP九大内置对象和四大作用域

1.九大内置对象

名称类型含义获取方式
requestHttpServletRequest封装所有请求信息方法参数
responseHttpServletResponse封装所有响应信息方法参数
sessionHttpSession封装所有会话信息req.getSession()
applicationServletContext封装所有信息req.getServletContext()
outPrintWriter输出对象resp.getWriter()
exceptionException异常对象
pageObject当前页面对象
pageContextpageContext获取其他对象
configServletConfig配置信息

2.四大作用域

  1. page
    在当前页面不会重新实例化
  2. request
    在一次请求中是同一个对象,下一次请求重新实例化
  3. session
    (1) 一次会话内有效
    (2) 只要客户端Cookie中传递的Jsessionid不变,Session不会重新实例化(不超过默认时间)
    (3) 实际有效时间
          浏览器关闭,Cookie失效。
          默认时间,在时间范围内无任何交互。
    在tomcat的web.xml中配置
<session-config>
	<session-timeout>30</session-timeout>
</session-config>
  1. 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集合

  1. 把map中的内容放在request作用域中
  2. 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>

浏览器:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值