第一种方式:获取map类型的request,session,application
package com.maya.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class RSAAction extends ActionSupport {
//获取map类型的request,session,application
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
private ActionContext context;
//为了方便在构造函数中获取ActionContext对象
public RSAAction(){
context=ActionContext.getContext();
}
//以下获取map类型的request,session,application
@Override
public String execute() throws Exception {
//获取map类型的request
request=(Map<String, Object>) context.get("reqeust");
request.put("key", "value");
//获取map类型的session
session=context.getSession();
session.put("key", "value");
//获取map类型的application
application=context.getApplication();
application.put("key", "value");
return super.execute();
}
}
第二种方式:采用接口方式获得map类型的request、session、application
package com.maya.action;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class RSAAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware {
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
//获取reqeust
@Override
public void setRequest(Map<String, Object> reqeust) {
this.request=reqeust;
}
//获取session
@Override
public void setSession(Map<String, Object> session) {
this.session=session;
}
//获取application
@Override
public void setApplication(Map<String, Object> application) {
this.application=application;
}
@Override
public String execute() throws Exception {
//赋值方式
request.put("key", "value");
session.put("key", "value");
application.put("key", "value");
return super.execute();
}
}
第三种方式:获取Http类型的request、session、application
package com.maya.action;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.opensymphony.xwork2.ActionSupport;
public class RSAAction extends ActionSupport {
//获取Http类型的request、session、application
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public RSAAction(){
request=ServletActionContext.getRequest();
session=request.getSession();
application= session.getServletContext();
}
@Override
public String execute() throws Exception {
request.setAttribute("key", "value");
session.setAttribute("key", "value");
application.setAttribute("key", "value");
return super.execute();
}
}