天天看點

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

Spring MVC @SessionAttribute注解
Spring MVC @SessionAttribute注解

繼續閱讀