import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport implements ServletRequestAware,
ServletResponseAware ,
ParameterAware,
SessionAware{
public Logger logger = Logger.getLogger(BaseAction.class);
public HttpServletRequest request;
public HttpServletResponse response;
public Map params;
public Map session;
//实现接口ServletRequestAware的方法
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request = request;
}
//实现接口ServletResponseAware的方法
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response = response;
}
//实现接口ParameterAware的方法
public void setParameters(Map params) {
// TODO Auto-generated method stub
this.params = convertForParam(params);
}
//实现接口SessionAware的方法
public void setSession(Map session) {
// TODO Auto-generated method stub
this.session = session;
}
public Map convertForParam(Map map){
HashMap hashmap = new HashMap(0);
//对接收到的参数map进行迭代,将数据取出来重新放到新定义的map中,对map进行转换
Entry entry;
Object[] value;
for(Iterator itr = map.entrySet().iterator();itr.hasNext();hashmap.put(entry.getKey(), value.length==1 ? value[0]:(Object)value)) {
entry = (Entry) itr.next();
value = (Object[]) entry.getValue();
}
return hashmap;
}
}
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class AuthorityInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 2329563960222482824L;
private static Logger logger = Logger.getLogger(AuthorityInterceptor.class);
@Override
public String intercept(ActionInvocation invocation) throws Exception {
try {
ActionContext ctx = invocation.getInvocationContext();
HttpServletRequest request =
(HttpServletRequest) ctx.get(StrutsStatics.HTTP_REQUEST);
String uri = request.getRequestURI();
if (uri.contains("Login") || uri.contains("AdminLogin")) {
return invocation.invoke();
}
if (request.getSession()!= null) {
HttpSession session = request.getSession();
if(session.getAttribute("xySysUser")!=null){
return invocation.invoke();
}
}
return Action.LOGIN;
} catch (Exception e) {
logger.error(null, e);
throw e;
}
}
}
SSH框架主要的类
最新推荐文章于 2016-09-08 10:11:00 发布