springMVC使用注解操作HttpSession中的对象

java


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@SessionAttributes("user")
//@SessionAttributes({"a","b"})
public class PutGetSession {
	
	@RequestMapping("putSession.do")
	public String putSession(Model model){
		model.addAttribute("user","用户对象");
		
		return "index3";
	}
	
	@RequestMapping("getSession.do")
	public String getSession(@ModelAttribute("user") String getUser){
		
		System.out.println("getUser= "+getUser);
		return "index3";
	}
}

@ModelAttribute("user") 将httpSession 中的user 注入到getUser中

index3.jsp


 <body>
  ${sessionScope.user }
  </body>

你可能感兴趣的:(spring,mvc,session)