SpringBoot学习5.13-SpringMVC-操作session-@SessionAttribute

1.HttpSession存取参数

@SAessionAttribute

从会话HttpSession中获取指定名称参数,该注解只能作用在参数上。

语法:@SessionAttribute(value = "参数名")。

@SAessionAttributes

将指定的参数名和类型的参数存入会话HttpSession中,该注解只能作用在类上。

语法:@SessionAttributes(value = { "参数名" }, types = { 参数类型.class })

2.例子

前台请求ms2,执行控制器方法ms2,配合@SessionAttributes(value = { "user" }, types = { User.class }),可以将user参数存入数据到session中。此处value和types的值最终都是指定user,用任意一个就可以了。用其他方式将参数存入session也是可以的,主要在控制器上需要使用注解@SessionAttributes。

当前台在请求ms3,@SessionAttribute(value = "user")会从session中去user参数作为改方法的参数。

控制器:

package com.zyf.springMVC.mvcsession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/** 注解存取HttpSession中的参数 */
@Controller
@RequestMapping("/mvcsession")
// @SessionAttributes存入数据到session策略,指定参数名称或者参数类型,取二者并集。
@SessionAttributes(value = { "user" }, types = { User.class })
public class MvcSessionController {

	// 跳转到ms1页面
	// http://localhost:8080/mvcsession/ms1
	@RequestMapping("/ms1")
	public String ms1() {
		return "mvcsession/ms1";
	}

	// ajax请求,数据存入httpsession
	@RequestMapping("/ms2")
	public ModelAndView ms2(@RequestBody User user, ModelAndView mav) {
		user.setNote(user.getNote() + "_ms2");
		mav.addObject("user", user);
		mav.setViewName("mvcsession/ms1");
		return mav;// 配合@SessionAttributes,执行完改方法后,user存入httpsession中。
	}

	// @SessionAttribute从httpsession取出数据
	// http://localhost:8080/mvcsession/ms3
	@RequestMapping("/ms3")
	public ModelAndView ms3(@SessionAttribute(value = "user") User user, ModelAndView mav) {
		user.setNote(user.getNote() + "_ms3");
		mav.setViewName("mvcsession/ms1");
		return mav;
	}

	// ajax请求
	@RequestMapping("/ms2_1")
	@ResponseBody
	public User ms2_1(@RequestBody User user, ModelAndView mav) {
		user.setNote(user.getNote() + "_ms2");
		mav.addObject("user", user);
		mav.setViewName("mvcsession/ms1");
		return user;// 验证发现该方式未成功将user存入httpsession中。需要使用ms2的方式
	}
}

前台:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 加载Query文件-->
<script src="https://code.jquery.com/jquery-3.2.0.js">
</script>
<script type="text/javascript">
	$(function() {
		var params = {
			id:15,
			name:'zhangsan',
			note:'session操作_ajax',
			sex:2
		};
		$("#button").click(function() {
			$.post({
				url : "/mvcsession/ms2",
				// 此处需要告知传递参数类型为JSON,不能缺少
				contentType : "application/json",
				// 将JSON转化为字符串传递
				data : JSON.stringify(params),
				// 成功后的方法
				success : function(result) {
					if(result != null){
						console.log(result);
					}
				}
			});
		});
	});
</script>
</head>
<body>
	<span>用户名:</span><span th:text="${user}"></span><br/>
	<button id="button">按钮</button>
</body>
</html>

 

github:https://github.com/zhangyangfei/SpringBootLearn.git中的springMVC工程。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值