struts2框架中的IoC

12年前

学习三大框架,对spring中的IoC和DI并不陌生,但是对struts2中的IoC你又知道多少呢!

现将学习过程的一点小结拿来与大家分享!

在struts2的Action中如何取得request ,session,application呢!

(一)取得Map类型的request,session,application

    实现RequestAware,SessionAware,Application接口,通过IoC取得Map类型。示例如下:

package com.bjsxt.action;    import java.util.Map;    import javax.servlet.ServletContext;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpSession;    import org.apache.struts2.interceptor.ApplicationAware;  import org.apache.struts2.interceptor.RequestAware;  import org.apache.struts2.interceptor.SessionAware;    import com.opensymphony.xwork2.ActionSupport;    public class LoginAction1 extends ActionSupport implements RequestAware,    SessionAware, ApplicationAware {   // 声明username属性   private String username;   // 声明request属性   private Map<String, Object> request;   // 声明session属性   private Map<String, Object> session;   // 声明 application属性   private Map<String, Object> application;     // 重写execute方法,这里并没有使用ServletActionContext.getRequest()来获得request对象,这是因为实现了ServletRequestAware   public String execute() throws Exception {    request.put("req", "i am request arrt!!");    session.put("session", "i am session attr");    application.put("application", "i am application");    return SUCCESS;   }     // Username的get方法   public String getUsername() {    return username;   }     // Username的set方法   public void setUsername(String username) {    this.username = username;   }     // 重写 RequestAware中的方法   @Override   public void setRequest(Map<String, Object> request) {    // TODO Auto-generated method stub    this.request = request;     }     // 重写 SessionAware中的方法   @Override   public void setSession(Map<String, Object> session) {    // TODO Auto-generated method stub    this.session = session;     }     // 重写 ApplicationAware中的方法   @Override   public void setApplication(Map<String, Object> application) {    // TODO Auto-generated method stub    this.application = application;   }    }

   

(二)取得真实类型的HttpServletRequest,HttpSession,ServletContext

    实现ServletRequestAware,ServletContextAware接口,通过IoC取得真实类型。示例如下:

package com.bjsxt.action;    import javax.servlet.ServletContext;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpSession;  import org.apache.struts2.ServletActionContext;  import org.apache.struts2.interceptor.ServletRequestAware;  import org.apache.struts2.util.ServletContextAware;  import com.opensymphony.xwork2.ActionSupport;  public class LoginAction1 extends ActionSupport implements ServletRequestAware,ServletContextAware {   //声明username属性   private String username;   //声明request属性   HttpServletRequest request;   //声明session属性   HttpSession session;   //声明 application属性   ServletContext application;     // 重写execute方法,这里并没有使用ServletActionContext.getRequest()来获得request对象,这是因为实现了ServletRequestAware   public String execute() throws Exception {    request.setAttribute("req", "i am request arrt!!");    request.getSession().setAttribute("session", "i am session attr");    application.setAttribute("application", "i am application");    return SUCCESS;   }   //Username的get方法   public String getUsername() {    return username;   }   //Username的set方法   public void setUsername(String username) {    this.username = username;   }   //重写ServletContext中的方法   @Override   public void setServletContext(ServletContext application) {    // TODO Auto-generated method stub    this.application = application;   }   //重写ServletRequest中的方法   @Override   public void setServletRequest(HttpServletRequest request) {    // TODO Auto-generated method stub    this.request = request;   }    }