javascript
在Spring Controller中将数据缓存到session
Servlet方案
在Controller的方法的參數(shù)列表中,添加一個(gè)javax.servlet.http.HttpSession類型的形參。spring?mvc會(huì) 自動(dòng)把當(dāng)前session對(duì)象注入這個(gè)參數(shù),此后可以使用setAttribute(String key, Object value)將數(shù)據(jù)緩存到session,使用removeAttribute(?String?key)將指定的數(shù)據(jù)從session緩存中移除。
1 package cn.sinobest.jzpt.demo.login.web; 2 import javax.servlet.http.HttpSession; 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.Model; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 /** 7 * 登陸相關(guān)Controller.<br> 8 * H - HttpSession. 9 * @author lijinlong 10 * 11 */ 12 @Controller 13 @RequestMapping("demo/h_login") 14 public class HLoginController { 15 16 @RequestMapping("/login") 17 public String login(Model model, String username, HttpSession session) { 18 Logger.logger.debug("in HLoginController.login..."); 19 String currUsername = session.getAttribute("username") == null ? null 20 : session.getAttribute("username").toString(); 21 // 嘗試從session中獲取username數(shù)據(jù)。 22 23 boolean usernameIsNull = username == null || username.isEmpty(); 24 boolean currunIsNull = currUsername == null || currUsername.isEmpty(); 25 if (usernameIsNull && currunIsNull) { 26 return View.VIEW_LOGIN; 27 } 28 29 if (!usernameIsNull) { 30 session.setAttribute("username", username); 31 // 將username緩存到session中。 32 } 33 return View.VIEW_LOGOUT; 34 } 35 36 /** 37 * 注銷. 38 * @param model 39 * @param session 40 * @return 41 */ 42 @RequestMapping("/logout") 43 public String logout(Model model, HttpSession session) { 44 Logger.logger.debug("in HLoginController.logout..."); 45 session.removeAttribute("username"); 46 // 將username從session中移除。 47 return View.VIEW_LOGIN; 48 } 49 } LoginController.javaSpring方案
spring mvc提供了內(nèi)嵌的支持方案:
對(duì)Controller使用org.springframework.web.bind.annotation.SessionAttributes注解,可以將指定名稱?或者?類型的數(shù)據(jù),在model.addAttribute(?String key, ?Object value)時(shí),緩存到session中。
調(diào)用org.springframework.web.bind.support.SessionStatus實(shí)例的setComplete(),在方法的參數(shù)列表中聲明SessionStatus類型的參數(shù),會(huì)被自動(dòng)注入。
SessionAttributes的使用方法
- 匹配單一的key
?@SessionAttributes("username") // 匹配key=username的數(shù)據(jù)? - 匹配key數(shù)組
?@SessionAttributes({"username", "password"}) // 匹配key=username或者password的數(shù)據(jù)? -
匹配單一類
?@SessionAttributes(types=String.class) // 匹配String類型的數(shù)據(jù)? - 匹配類數(shù)組
?@SessionAttributes(types={String.class, List.class}) // 匹配String類型或List類型的數(shù)據(jù)? - 混合匹配
?@SessionAttributes(value={"username", "password"}, types={String.class, List.class})?
ModelAttribute
使用ModelAttribute,可以自動(dòng)將session中指定的參數(shù)注入到方法的形參;但是如果session中沒有指定的參數(shù),會(huì)拋出異常:
org.springframework.web.HttpSessionRequiredException: Session attribute 'username' required - not found in session
Model中的數(shù)據(jù)
Spring?會(huì)把Session中的數(shù)據(jù)裝載到Model中,所以使用model.asMap().get("username")可以獲取 session中的數(shù)據(jù)。返回頁面前,spring會(huì)把Model中的數(shù)據(jù)放入requestScope,所以在頁面可以使 用${requestScope.username}來獲取數(shù)據(jù)。
轉(zhuǎn)載于:https://www.cnblogs.com/ywjy/p/5016034.html
總結(jié)
以上是生活随笔為你收集整理的在Spring Controller中将数据缓存到session的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JNDI 在 J2EE 中的角色
- 下一篇: MIN()与MAX()函数 的注意事项