Spring MVC @SessionAttribute注解

@SessionAttribute注解

在HTTP的会话(session)对象属性值中,用来传递给控制器的参数。

创建Hello控制器

package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
@Controller
public class Hello {
	@RequestMapping("/show")
	public String show(@SessionAttribute("name")String userName) {
		System.out.println("userName="+userName);
		return "success";
	}
}

创建index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>index</title>
</head>
<body>
	<%
		session.setAttribute("name", "baixue");
		response.sendRedirect("./show");
	%>
</body>
</html>

启动Tomcat并访问index.jsp

猜你喜欢

转载自blog.csdn.net/dwenxue/article/details/81383097