1.创建Request,Session,Application的Map对象
action:
Map<String,Object> request;
Map<String,Object> session;
Map<String,Object> application;
public UserAction1(){
System.out.println("UserAction1...");
request = (Map<String, Object>) ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
}
jsp:
Request:<s:property value="#request.msg"/>
Session:<s:property value="#session.msg"/>
Application:<s:property value="#application.msg"/>
Attribute:<s:property value="#attr.msg"/>
2.实现RequestAware,SessionAware,ApplicationAware接口(IOC)
public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
public UserAction2(){
System.out.println("UserAction2...");
}
private static final long serialVersionUID = 1L;
private Map<String,Object> request;
private Map<String,Object> session;
private Map<String,Object> application;
UsersDao uDao = new UsersDao();
Users users;
public String execute(){
request.put("msg", "北京欢迎你!");
session.put("msg", "北京欢迎你!");
application.put("msg", "北京欢迎你!");
String str = "failure";
this.addFieldError("loginErr", "用户名或密码不正确。");
if(uDao.queryUsers(users)){
str = "success";
this.addFieldError("loginErr", "");
}
return str;
}
3. 创建传统的HttpServletRequest,HttpSession,ServletContext对象
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public String execute(){
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
request.setAttribute("msg", "北京欢迎你!");
session.setAttribute("msg", "北京欢迎你!");
application.setAttribute("msg", "北京欢迎你!");
4.实现ServletRequestAware,ServletContextAware接口(IOC)
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public String execute(){
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
request.setAttribute("msg", "北京欢迎你!");
session.setAttribute("msg", "北京欢迎你!");
application.setAttribute("msg", "北京欢迎你!");