Session:保存对象;
获得session的两种方式:
1),通过actionContext.getSession() 方法;
2),实现SessionAware 接口;
---
action类:
public class LoginService extends ActionSupport implements SessionAware{
...Admin adm = adminDao.getByNamePwd(lg_admin,lg_pwd);
session.put(Struts2PortfolioConstants.USER, adm);//添加登录用户到session;
...
private Map session;
public void setSession(Map session) {
this.session = session;
}
Struts2PortfolioConstants中,声明一个变量:
public static final String USER = "user";
页面获取:
session:<s:property value="#session.user.name"/>。
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Cookie:保存字符串值;
---
连接字符串,保存cookie的value;
Cookie保存,获取类;
获取cookie后,分拆字符串,保存在javabean属性中,需要的页面显示cookie中的值。
---
1),Action:| 保存cookie;
//保存登录信息到cookie中; | 转码抛出异常;
try {
getHandleCookie().addCookie(Struts2PortfolioConstants.USER, adm.getName() + "," + adm.getPwd());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public HandleCookie getHandleCookie( ) {
return new HandleCookie();
}
2),cookie处理类:
public class HandleCookie{
//添加cookie; | UnsupportedEncodingException,转码异常
public void addCookie(String name,String value) throws UnsupportedEncodingException{
value = URLEncoder.encode(value,"UTF-8");
Cookie cookie = new Cookie(name, value);
cookie.setMaxAge(60*60*24*365);//保存365天;
cookie.setPath("/");
ServletActionContext.getResponse().addCookie(cookie);
}
// 得到指定的cookie;
public String getCookie(String cookieName) throws UnsupportedEncodingException {
HttpServletRequest request = ServletActionContext.getRequest();
Cookie[] cookies = request.getCookies();
String value = "";
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookieName.equals(cookie.getName())) { //获取具体的cookie;
value = URLDecoder.decode(cookie.getValue(),"UTF-8");
return value;
}
}
}
return null;
}
// 删除指定的cookie
public Cookie delCookie(String cookieName) {
HttpServletRequest request = ServletActionContext.getRequest();
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookieName.equals(cookie.getName())) {
cookie.setValue("");
cookie.setMaxAge(0);
cookie.setPath("/"); //添加,清除时需要加路径才能清除掉;
ServletActionContext.getResponse().addCookie(cookie);
}
}
}
return null;
}
}
3),Action:| 获取cookie; | 删除cookie;
public String getCook() throws UnsupportedEncodingException {
String cookieStr = getHandleCookie().getCookie(Struts2PortfolioConstants.USER);
if(cookieStr==null){
return "error";
}
if (cookieStr.length()>0) { //cookie值的长度>0;
String[] split = cookieStr.split(",");
setCookName(split[0]);
setCookPwd(split[1]);
}
return SUCCESS;
}
private String cookName;
public String getCookName() {
return cookName;
}
public void setCookName(String cookName) {
this.cookName = cookName;
}
private String cookPwd;
public String getCookPwd() {
return cookPwd;
}
public void setCookPwd(String cookPwd) {
this.cookPwd = cookPwd;
}
//类的引用;
public HandleCookie getHandleCookie( ) {
return new HandleCookie();
}
//删除cookie;
public String delCook() {
getHandleCookie().delCookie(Struts2PortfolioConstants.USER);
return SUCCESS;
}
4),页面显示:
用户名:<s:property value="cookName"/>
密码:<s:property value="cookPwd"/>