取得Map类型的request、session、application、真实类型的HttpServletRequest、HttpSession、HttpServletApplication
第一种方式:在Action中取得Map类型的request、session、application 依赖于容器的方式
public class LoginAction1 extends ActionSupport{
private Map request;
private Map session;
private Map application;
//构造函数
public LoginAction1(){
request = (Map)ActionContext.getContext().get("request");
//为什么直接在get里面填写request就可以取得Map类型的request了呢?这是为什么呢
//我们可以在jsp的页面上使用<s:debug>,就可以发现在debug中有两部分内容
//第一部分:Value stack(值栈) 其中包含的是一些Action中的属性、errors、fieldErrors、actionErrors等内容
//这些值可以在页面上用<s:property value="errors.name[0]"/>取出错误信息 或 <s:property value="action中的属性"/>
//第二部分:Stack Context 也就是ActionContext (action的上下文环境),其中就包括了许多的key和对应的value
//在key的取值中就有request,所以上面用get("request")的方式就会取到Map类型的request值
//还有一点需要注意,在页面上访问ActionContext中的值时需要使用 #key 的方式。
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
}
public String execute(){
request.put("r1","r1");
session.put("s1","s1");
application.put("a1","a1");
request SUCCESS;
}
}
前台jsp页面访问时,可以这样来访问:
<s:property value="#request.r1"/> | <%=request.getAttribute("r1")%>
<s:property value="#session.s1"/> | <%=session.getAttribute("s1")%>
<s:property value="#application.a1"/> | <%=application.getAttribute("a1")%>
<s:property value="#attr.a1"/> //attr会自动搜索request,session,application范围搜索
第二种方式: IOC方式
public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware {
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
//DI dependency injection
//IoC inverse of control
public String execute() {
request.put("r1", "r1");
session.put("s1", "s1");
application.put("a1", "a1");
return SUCCESS;
}@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
}
其中的aware(得知,知道的意思),需要重写对应的方法
第三种方法:获得HttpServletRequest、HttpSession、HttpServletApplication 依赖于容器的方式
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();
}
public String execute() {
request.setAttribute("r1", "r1");
session.setAttribute("s1", "s1");
application.setAttribute("a1", "a1");
return SUCCESS;
}
}
第四种方式:获得HttpServletRequest、HttpSession、HttpServletApplication IOC方式
public class LoginAction4 extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public String execute() {
request.setAttribute("r1", "r1");
session.setAttribute("s1", "s1");
application.setAttribute("a1", "a1");
return SUCCESS;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
this.application = session.getServletContext();
}
}