下面介绍4种访问request,session,application的方法:
第一种:依赖于容器
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.Validations;
public class LoginAction1 extends ActionSupport{
private Map request;
private Map session;
private Map application;
public LoginAction1() {
request = (Map)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
}
@Override
public String execute() throws Exception {
request.put("r1", "r1_v");
session.put("s1", "s1_v");
session.put("s2", "s2_v");
application.put("a1", "a1_v");
return SUCCESS;
}
}
第二种:IOC (一般开发用这种较多)
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 LoginAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
private Map<String,Object> request;
private Map<String,Object> session;
private Map<String,Object> application;
@Override
public String execute() throws Exception {
request.put("r1", "r11");
session.put("s1", "s11");
application.put("a1", "a11");
return SUCCESS;
}
@Override
public void setRequest(Map<String, Object> arg0) {
request = arg0;
}
@Override
public void setSession(Map<String, Object> arg0) {
session = arg0;
}
@Override
public void setApplication(Map<String, Object> arg0) {
application = arg0;
}
}
第三种:依赖于容器(原始)
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction3 extends ActionSupport {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public LoginAction3(){
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
}
@Override
public String execute() throws Exception {
request.setAttribute("r1", "r111");
session.setAttribute("s1", "s111");
application.setAttribute("a1", "a111");
return SUCCESS;
}
}
第四种:也是IOC的
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction4 extends ActionSupport implements ServletRequestAware{
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
@Override
public String execute() throws Exception {
request.setAttribute("r1", "r1111");
session.setAttribute("s1", "s1111");
application.setAttribute("a1", "a1111");
return SUCCESS;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
this.application = session.getServletContext();
}
}
访问:
<s:property value="#request.r1"/> | <%=request.getAttribute("r1") %> <br/>
<s:property value="#session.s1"/> | <%=session.getAttribute("s1") %> <br/>
<s:property value="#application.a1"/> | <%=application.getAttribute("a1") %> <br/>
<s:property value="#attr.r1"/> 这种是自动搜索的,建议用上面那种精确的
<s:debug></s:debug>
对于为什么要用#来访问,你可以打开 <s:debug></s:debug>里面有说明:
Stack Context
These items are available using the #key notation