说明:
1.常用的是第二中方法取到,Map类型的request,session,application的引用
2.request,session,application是jsp的内置对象,HttpServletRequest,HttpServletSession, HttpServletContext是servlet中与之对应的元素。
一.取得Map类型的request,session,application的引用
1. 直接取得
通过ActionContext中取出相关的web元素。值栈中Stack Context实际就是ActionContext,事实上值栈也放在request里面。
package com.xie.action.validate;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class ActionCheck1 extends ActionSupport {
private Map
private Map
private Map
public ActionCheck1(){
req=(Map
ses=ActionContext.getContext().getSession();
app=ActionContext.getContext().getApplication();
}
public String execute(){
req.put("xie", "UC num is 1596144767");
ses.put("wang", "don't know");
app.put("liu", "try to don't know");
return SUCCESS;
}
}
页面访问:
2. 通过IoC方式取得
自己定义的Action应该实现RequestAware,SessionAware,ApplicationAware接口。
取得web元素的代码如下:
package com.xie.action.validate;
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;
@SuppressWarnings("serial")
public class ActionCheck2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware {
private Map
private Map
private Map
public void setRequest(Map
req=request;
}
public void setSession(Map
ses=session;
}
public void setApplication(Map
app=application;
}
public String execute(){
req.put("xie", "UC num is 1596144767");
ses.put("wang", "don't know");
app.put("liu", "try to don't know");
return SUCCESS;
}
}
三.取得真实类型的HttpServletRequest,HttpServletSession, HttpServletContext的引用
3.直接取得
代码如下:
package com.xie.action.validate;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class ActionCheck3 extends ActionSupport {
private HttpServletRequest req;
private HttpSession ses;
private ServletContext app;
public ActionCheck3(){
req=ServletActionContext.getRequest();
ses=req.getSession();
app=ses.getServletContext();
}
public String execute(){
req.setAttribute("xie", "UC num is 1596144767");
ses.setAttribute("wang", "don't know");
app.setAttribute("liu", "try to don't know");
return SUCCESS;
}
}
4.通过IoC取得,需实现ServletRequestAware
取得代码如下:
package com.xie.action.validate;
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;
@SuppressWarnings("serial")
public class ActionCheck4 extends ActionSupport implements ServletRequestAware {
private HttpServletRequest req;
private HttpSession ses;
private ServletContext app;
public void setServletRequest(HttpServletRequest request) {
req=request;
ses=req.getSession();
app=ses.getServletContext();
}
public String execute(){
req.setAttribute("xie", "UC num is 1596144767");
ses.setAttribute("wang", "don't know");
app.setAttribute("liu", "try to don't know");
return SUCCESS;
}
}