日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

在Spring Controller中将数据缓存到session

發(fā)布時間:2025/3/14 javascript 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Spring Controller中将数据缓存到session 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Servlet方案

在Controller的方法的參數(shù)列表中,添加一個javax.servlet.http.HttpSession類型的形參。spring?mvc會 自動把當前session對象注入這個參數(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.java

Spring方案

spring mvc提供了內(nèi)嵌的支持方案:

  • 將數(shù)據(jù)緩存到session
    對Controller使用org.springframework.web.bind.annotation.SessionAttributes注解,可以將指定名稱?或者?類型的數(shù)據(jù),在model.addAttribute(?String key, ?Object value)時,緩存到session中。
  • 清除session中的數(shù)據(jù)
    調(diào)用org.springframework.web.bind.support.SessionStatus實例的setComplete(),在方法的參數(shù)列表中聲明SessionStatus類型的參數(shù),會被自動注入。
  • 1 package cn.sinobest.jzpt.demo.login.web; 2 import org.springframework.stereotype.Controller; 3 import org.springframework.ui.Model; 4 import org.springframework.web.bind.annotation.ModelAttribute; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.SessionAttributes; 7 import org.springframework.web.bind.support.SessionStatus; 8 /** 9 * 登陸相關(guān)Controller.<br> 10 * S - Spring Session. 11 * @author lijinlong 12 * 13 */ 14 @Controller 15 @RequestMapping("demo/s_login") 16 @SessionAttributes("username") // 指定了key為username的數(shù)據(jù),會被放入session中。 17 public class SLoginController { 18 @RequestMapping("/login") 19 public String login(Model model, String username) { 20 Logger.logger.debug("in SLoginController.login..."); 21 String currUsername = model.asMap().get("username") == null ? null 22 : model.asMap().get("username").toString(); 23 // 嘗試從session中獲取username(spring mvc會自動把session中的數(shù)據(jù)裝載到model中)。 24 25 boolean usernameIsNull = username == null || username.isEmpty(); 26 boolean currunIsNull = currUsername == null || currUsername.isEmpty(); 27 if (usernameIsNull && currunIsNull) { 28 return View.VIEW_LOGIN; 29 } 30 31 if (!usernameIsNull) { 32 model.addAttribute("username", username); 33 // username會被放入session中(key和@SessionAttributes的參數(shù)匹配)。 34 } 35 return View.VIEW_LOGOUT; 36 } 37 38 @RequestMapping("/logout") 39 public String logout(SessionStatus status, 40 @ModelAttribute("username") String currUsername) { 41 Logger.logger.debug("in SLoginController.logout..."); 42 Logger.logger.debug("current user is:" + currUsername); 43 status.setComplete(); 44 // 清除session中的attribute 45 return View.VIEW_LOGIN; 46 } 47 } LoginController

    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,可以自動將session中指定的參數(shù)注入到方法的形參;但是如果session中沒有指定的參數(shù),會拋出異常:
    org.springframework.web.HttpSessionRequiredException: Session attribute 'username' required - not found in session

    Model中的數(shù)據(jù)

    Spring?會把Session中的數(shù)據(jù)裝載到Model中,所以使用model.asMap().get("username")可以獲取 session中的數(shù)據(jù)。返回頁面前,spring會把Model中的數(shù)據(jù)放入requestScope,所以在頁面可以使 用${requestScope.username}來獲取數(shù)據(jù)。

    轉(zhuǎn)載于:https://www.cnblogs.com/ywjy/p/5016034.html

    總結(jié)

    以上是生活随笔為你收集整理的在Spring Controller中将数据缓存到session的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。