学习Session 基类

package com.ruangao.framework.web.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.ruangao.framework.constants.Constants;

public class BaseAction extends ActionSupport implements SessionAware,
ServletRequestAware, ServletResponseAware {
private static final long serialVersionUID = 7241480283222137733L;

private Map session;
private HttpServletRequest servletRequest;

private HttpServletResponse servletResponse;

protected final Log logger = LogFactory.getLog(getClass());

public void setSession(Map session) {
this.session = session;
}

public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;
}

public void setServletResponse(HttpServletResponse servletResponse) {
this.servletResponse = servletResponse;
}

/**
*
* <p>
* Discription:[查询IP地址]
* </p>
*
* @return
*/
public String findIp() {
return this.servletRequest.getRemoteAddr();
}

/**
*
* <p>
* Discription:[方法功能中文描述]
* </p>
*
* @param key
* @param value
*/
public void insertSession(String key, Object value) {
ActionContext actionContext = ActionContext.getContext();
Map session = actionContext.getSession();
session.put(key, value);
}

/**
*
* <p>
* Discription:[插入session]
* </p>
*
* @param value
*/
public void insertSysUserSession(Object value) {
ActionContext actionContext = ActionContext.getContext();
Map session = actionContext.getSession();
session.put(Constants.SESSION_KEY_SYS_USER, value);
}

/**
*
* <p>
* Discription:[移除session]
* </p>
*/
public void removeSysUserSession() {
ActionContext actionContext = ActionContext.getContext();
Map session = actionContext.getSession();
session.remove(Constants.SESSION_KEY_SYS_USER);
}

/**
*
* <p>
* Discription:[插入session]
* </p>
*
* @param value
*/
public void insertRegUserSession(Object value) {
ActionContext actionContext = ActionContext.getContext();
Map session = actionContext.getSession();
session.put(Constants.SESSION_KEY_REG_USER, value);
}

/**
*
* <p>
* Discription:[移除session]
* </p>
*/
public void removeRegUserSession() {
ActionContext actionContext = ActionContext.getContext();
Map session = actionContext.getSession();
session.remove(Constants.SESSION_KEY_REG_USER);
}

/**
*
* <p>
* Discription:[根据key查找]
* </p>
*
* @param key
* @return
*/
public Object findSession(String key) {
ActionContext actionContext = ActionContext.getContext();
Map session = actionContext.getSession();
return session.get(key);
}


// 定义方法
private String method;

@JSON(serialize = false)
public String getMethod() {
return method;
}

public void setMethod(String method) {
this.method = method;
}

public HttpServletRequest getServletRequest() {
ActionContext ctx = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) ctx
.get(ServletActionContext.HTTP_REQUEST);
return request;
}

public HttpServletResponse getServletResponse() {
ActionContext ctx = ActionContext.getContext();
HttpServletResponse response = (HttpServletResponse) ctx
.get(ServletActionContext.HTTP_RESPONSE);
return response;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值