|
1.取得Map类型request,session,application
/**
*依赖于struts2容器
*/
public class LoginAction extends ActionSupport{
private Map request;
private Map session;
private Map application;
public LoginAction() {
request=(Map)ActionContext.getContext().get("request");
session=ActionContext.getContext().getSession();
application=ActionContext.getContext().getApplication();
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
2.IoC(控制反转) 常用
实现RequestAware,SessionAware,ApplicationAware接口
public class LoginAction 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{
return SUCCESS;
}
public void setRequest(Map<String, Object> request) {
this.request = request;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
public void setApplication(Map<String, Object> application) {
this.application = application;
}
}
3.真实类型 HttpServletRequest, HttpSession, ServletContext的引用
(1)
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();
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
(2)IoC (实现ServletRequestAware接口)
public class LoginAction4 extends ActionSupport implements ServletRequestAware{
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
@Override
public String execute() throws Exception {
return SUCCESS;
}
public void setServletRequest(HttpServletRequest request) {
this.request=request;
session=request.getSession();
application=session.getServletContext();
}
} |
|