<a target=_blank href="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对象</a>
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;
}
}
</pre><p><span style="color:rgb(54,46,43); font-family:Arial; font-size:14px; line-height:26px">这样 ,每个Control就能够方便的得到Servlet API 中的对象了。</span></p><p><span style="color:rgb(54,46,43); font-family:Arial; font-size:14px; line-height:26px"></span></p><p> 登陆保存用户到session</p><p></p><pre name="code" class="java">@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();
}
鲁承星/Luke
互联网产品负责人/Leader
-----------------------------------------------------
微信 : 465608188
电话:13717825876
邮箱:lcxingperfect@163.com