首页 > 代码库 > springMVC 获取request 处理全局session业务
springMVC 获取request 处理全局session业务
<a target=_blank href=http://www.mamicode.com/"http://blog.csdn.net/is_zhoufeng/article/details/7559756" style="color: rgb(0, 0, 0); text-decoration: none; font-family: 'Microsoft YaHei'; font-size: 20px; line-height: 30px; ">springMVC中得到request对象,session对象>RequestAttributes ra = RequestContextHolder.getRequestAttributes(); HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest(); request.getSession().setAttribute(name, webuser);
(1)web.xml
<session-config>
<session-timeout>30</session-timeout>
</session-config>注:30指分钟
也可以让所有的Control继承一个BaseControl类 ,BaseControl类的定义为:
package com.jrgy.web.controller; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.jrgy.pojo.domain.FriendlyLink; import com.jrgy.pojo.domain.ItemInfo; import com.jrgy.pojo.domain.WebUser; import com.jrgy.util.CacheManager; import com.jrgy.util.Constants; import com.jrgy.web.service.IAssociationManager; import com.jrgy.web.service.IWebIndexManager; import com.jrgy.web.service.IWebItemInfoManager; public class BaseController implements Serializable{ private static final long serialVersionUID = 8695834026800952113L; /** * ???? * */ // // HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder // .getRequestAttributes()).getRequest(); CacheManager cacheManager = CacheManager.getInstance(); @Autowired IWebIndexManager webIndexManager; @Autowired IWebItemInfoManager webItemInfoManager; @Autowired IAssociationManager webAssociationManager; /** * 导航栏 * */ List<ItemInfo> listitemInfo = new ArrayList<ItemInfo>(); /** * 友情链接 * */ List<FriendlyLink> friendLyLinkLs = new ArrayList<FriendlyLink>(); List itmeCode = new ArrayList(); Map map = new HashMap(); /** * ?????????? * @param Object ???????? * @param String KEY ? * @return Object * @throws Exception */ public Object getCache(Object object, String key) { Object cacheDate = cacheManager.get(key); if (cacheDate != null) { object = cacheDate; } return object; } /** * * @param key * @param Object * @return void * @throws Exception */ public void addCache(String key, Object object) { cacheManager.delCacheByKey(key); cacheManager.add(key, object); } public void addSessionWebUser(String name,WebUser webuser) { RequestAttributes ra = RequestContextHolder.getRequestAttributes(); HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest(); request.getSession().setAttribute(name, webuser); } public Object getWebUserAttribute(String name) { // TODO Auto-generated method stub RequestAttributes ra = RequestContextHolder.getRequestAttributes(); HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest(); Object obj=null; if (!name.trim().equals("")) { obj= request.getSession().getAttribute(name); } return obj; } }这样 ,每个Control就能够方便的得到Servlet API 中的对象了。
登陆保存用户到session
@RequestMapping("/islogin.do") public void islogin(HttpServletRequest request, HttpServletResponse response){ String sessionId = request.getSession().getId(); response.setContentType("text/html;charset=utf-8"); String userName = request.getParameter("userName"); String passWord = request.getParameter("userPassWord"); WebUser webuser=new WebUser(); String Msg = null; try { if(!StrUtil.checkStr(userName)){ Msg="2"; }else if(!StrUtil.checkStr(passWord)){ Msg="3"; } else{ webuser=mainWebUserManager.userLogin(userName,passWord); if (webuser!=null) { if(Constants.webUserStruts.equals(webuser.getWebUserType())){ Msg = "5"; }else{ Msg = "1"; addSessionWebUser(Constants.LOGIN_WebUSER,webuser); log.debug(userName+""); } } else{ Msg = "4"; } } PrintWriter out = response.getWriter(); String outMsg = "{Msg:"+Msg+"}"; out.print(outMsg); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
在需要使用session以下代码:String name=Constants.LOGIN_WebUSER; WebUser web=(WebUser) getWebUserAttribute(name); if (web!=null) { currentUser=web.getWebUserName(); }springMVC 获取request 处理全局session业务
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。