全部博文(247)
分类: Java
2013-12-19 06:46:54
转载:http://miseryboy.javaeye.com/blog/311363
1、struts2的Action可以通过ActionContext来访问ServletAPI。下面是ActionContext类中包含的几个常用方法:
Object get(Object key):该方法类似于调用HttpServletRequest的getAttribute(String name)方法;
Map getApplication():返回一个Map对象,该对象模拟了该应用的ServletContext实例;
static ActionContext getContext():静态方法,获取系统的ActionContext实例;
Map getParameters():获取所有的请求参数。类似于调用HttpServletRequest对象的getParameterMap方法;
Map getSession():返回一个Map对象,该Map对象模拟了HttpSession实例;
void setApplication(Map application):直接传入一个Map实例,将该Map实例里的key-value对转换成application的属性名,属性值;
void setSession(Map session):直接传入一个Map实例,将该Map实例里的key-value对转换成session的属性名,属性值
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();
}
public String execute() {
request.put("r1", "r1");
session.put("s1", "s1");
application.put("a1", "a1");
return SUCCESS;
}
}
2、Action直接访问ServletAPI,Struts2的Action可以实现下列接口,直接来访问ServletAPI:
ServletContextAware:实现该接口的Action可以直接访问Web应用的ServletContext实例
ServletRequestAware:实现该接口的Action可以直接访问用户请求的HttpServletRequest实例
ServletResponseAware:实现该接口的Action可以直接访问服务器响应的HttpServletResponse实例
例子:
public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware {
private Map
private Map
private Map
//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
this.request = request;
}
@Override
public void setSession(Map
this.session = session;
}
@Override
public void setApplication(Map
this.application = application;
}
}
3、struts2还提供了一个ServletActionContext,这个类包含了如下一些静态方法:
static PageContext getPageContext():取得Web应用的PageContext对象;
static HttpServletRequest getRequest():取得web应用的HttpServletRequest对象;
static HttpServletResponse getResponse():取得web应用的HttpServletResponse对象;
static ServletContext getServletContext():取得web应用的ServletContext对象;
例子:
public class LoginAction extends ActionSupport {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public LoginAction() {
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;
}
}
4.实现ServletRequestAware
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();
}
}