struts2 -- 在action中获取scope对象的4种方式
struts2中将action与servlet解耦合,故我们在action中的excute方法中无法直接使用request、session以及 application对象,为了操作这些对象,我们必须编写另外一些代码,现将在action中访问scope对象的方式总结如下,共四种。
方式一: 与Servlet解耦合的非IOC方式
?
特点:获取的scope对象与容器无关,通过ActionContext获取
?
import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; /** * @author waitingfortime * */ public class test2Action extends ActionSupport { private String username; ActionContext context; Map request; Map session; Map application; public String execute() throws Exception { context=ActionContext.getContext();//从ActionContext中获得上下文对象,并从该对象中一次获得scope对象 request=(Map) context.get("request"); session=context.getSession(); application=context.getApplication(); request.put("req", "requst属性"); session.put("ses", "sesion属性"); application.put("app", "application属性"); return SUCCESS; } }
?在jsp页面中有2种方式获得scope对象的值
public class test3Action extends ActionSupport implements RequestAware,SessionAware,ApplicationAware { private String username; Map request; Map session; Map application; public String execute() throws Exception { request.put("req", "requst属性"); session.put("ses", "sesion属性"); application.put("app", "application属性"); 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; } ...省略username的get/set方法}?
在方式二中它是通过实现接口,在重写接口中的方法中完成对Map requset的赋值,所以称之IOC方式。
方式三、与Servlet耦合的非IOC方式
import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /** * @author waitingfortime */public class test2Action extends ActionSupport { private String username; HttpServletRequest request; HttpSession session; ServletContext application; public String execute() throws Exception { request = ServletActionContext.getRequest(); session = request.getSession(); application = ServletActionContext.getServletContext(); request.setAttribute("req", "requst属性"); session.setAttribute("ses", "sesion属性"); application.setAttribute("app", "application属性"); return SUCCESS; }
?
此方法获取的纯粹的Scope对象,它与容器相关,这些Scope对象操作更强。
方式四、与Servlet耦合的IOC方式
public class Login4Action extends ActionSupport implements ServletRequestAware,ServletContextAware{ private String username; ActionContext context; HttpServletRequest request; HttpSession session; ServletContext application; public String execute() throws Exception { context=ActionContext.getContext(); session=request.getSession(); request.setAttribute("req", "requst属性"); session.setAttribute("ses", "sesion属性"); application.setAttribute("app", "application属性"); return SUCCESS; } public void setServletRequest(HttpServletRequest request) { System.out.println("测试:"+request); this.request=request; } public void setServletContext(ServletContext application) { System.out.println("测试:"+application); this.application=application; } ...省略username的get/set方法}
?方式四则是实现了接口,来通过依赖注入得到对象。
?