Struts2中访问web元素的四种方式(访问requst、session、application)
?
四种方式:
1. 通过ActionContext来访问request,session,application对象
2. 通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象
3. 通过ServletActionContext来访问request,session,application对象
4. 通过实现ServletRequestAware接口来访问request,session,application对象
?
演示代码:
方式一:
view plaincopy to clipboardprint?
public class UserAction1 extends ActionSupport{
private static final long serialVersionUID = 1L;
private Map <String,Object>request;
private Map <String,Object>session;
private Map <String,Object>application;
@SuppressWarnings("unchecked")
@Override?
public String execute(){
System.out.println("通过ActionContext来访问request,session,application对象");
// 初始化
request = (Map<String,Object>)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
// 赋值
request.put("requestKey", "requestValue");
session.put("sessionKey", "sessionValue");
application.put("applicationKey", "applicationValue");
return "success";
}
}?
public class UserAction1 extends ActionSupport{
private static final long serialVersionUID = 1L;
private Map <String,Object>request;
private Map <String,Object>session;
private Map <String,Object>application;
@SuppressWarnings("unchecked")
@Override
public String execute(){
System.out.println("通过ActionContext来访问request,session,application对象");
// 初始化
request = (Map<String,Object>)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
// 赋值
request.put("requestKey", "requestValue");
session.put("sessionKey", "sessionValue");
application.put("applicationKey", "applicationValue");
return "success";
}
}
方式二:
view plaincopy to clipboardprint?
public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
private static final long serialVersionUID = 1L;
private Map <String,Object>request;
private Map <String,Object>session;
private Map <String,Object>application;
@SuppressWarnings("unchecked")
@Override?
public String execute(){
System.out.println("通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象");
// 赋值
request.put("requestKey", "requestValue");
session.put("sessionKey", "sessionValue");
application.put("applicationKey", "applicationValue");
return "success";
}
@Override?
public void setRequest(Map<String, Object> request) {
this.request = request;
}
@Override?
public void setApplication(Map<String, Object> application) {
this.application = application;
}
@Override?
public void setSession(Map<String, Object> session) {
this.session = session;
}
}?
public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
private static final long serialVersionUID = 1L;
private Map <String,Object>request;
private Map <String,Object>session;
private Map <String,Object>application;
@SuppressWarnings("unchecked")
@Override
public String execute(){
System.out.println("通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象");
// 赋值
request.put("requestKey", "requestValue");
session.put("sessionKey", "sessionValue");
application.put("applicationKey", "applicationValue");
return "success";
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
}
方式三:
view plaincopy to clipboardprint?
public class UserAction3 extends ActionSupport{
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
@SuppressWarnings("unchecked")
@Override?
public String execute(){
System.out.println("通过ServletActionContext来访问request,session,application对象");
// 初始化
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
// 赋值
request.setAttribute("requestKey", "requestValue");
session.setAttribute("sessionKey", "sessionValue");
application.setAttribute("applicationKey", "applicationValue");
return "success";
}
}?
public class UserAction3 extends ActionSupport{
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
@SuppressWarnings("unchecked")
@Override
public String execute(){
System.out.println("通过ServletActionContext来访问request,session,application对象");
// 初始化
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
// 赋值
request.setAttribute("requestKey", "requestValue");
session.setAttribute("sessionKey", "sessionValue");
application.setAttribute("applicationKey", "applicationValue");
return "success";
}
}
方式四:
view plaincopy to clipboardprint?
public class UserAction4 extends ActionSupport implements ServletRequestAware{
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
@SuppressWarnings("unchecked")
@Override?
public String execute(){
Syste