Struts2学习----------Web资源获取

本文介绍了使用Struts2框架获取Web资源的四种方法:使用Struts2Aware拦截器、Struts2RequestAware拦截器、Struts2内置静态对象ActionContext及ServletActionContext。详细对比了各种方法的特点和适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要介绍获取Web资源的四种方式

一、使用Struts2Aware拦截器

使用Struts2 Aware拦截器来获取web资源,首先必须是在Action中进行,然后还得实现ServletRequestAware,ServletResponseAware,ServletContextAware接口来获取对应的ServletRequestServletResponseServletContext对象。

[java] view plain copy
  1. package cn.lovepi.chapter03.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4. import org.apache.struts2.interceptor.ServletRequestAware;  
  5. import org.apache.struts2.interceptor.ServletResponseAware;  
  6. import org.apache.struts2.util.ServletContextAware;  
  7.   
  8. import javax.servlet.ServletContext;  
  9. import javax.servlet.ServletRequest;  
  10. import javax.servlet.ServletResponse;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13.   
  14. /** 
  15.  * Created by icarus on 2016/7/9. 
  16.  * 使用Struts2 aware拦截器获取web资源 
  17.  * 首先这是一个action 
  18.  * 然后得实现对应的接口来获取对应的web类 
  19.  */  
  20. public class FirstAction extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{  
  21.     // 设置类中的对应的request和response对象和context对象  
  22.     private ServletRequest request;  
  23.     private ServletResponse response;  
  24.     private ServletContext context;  
  25.     /** 
  26.      * action执行方法 
  27.      * @return 
  28.      * @throws Exception 
  29.      */  
  30.     @Override  
  31.     public String execute() throws Exception {  
  32.         String username=request.getParameter("username");  
  33.         System.out.println("第一种拦截器获取资源:"+username);  
  34.         return "success";  
  35.     }  
  36.   
  37.     /** 
  38.      * 设置传递到action中的ServletRequest 
  39.      * @param httpServletRequest 
  40.      */  
  41.     @Override  
  42.     public void setServletRequest(HttpServletRequest httpServletRequest) {  
  43.         request=httpServletRequest;  
  44.     }  
  45.   
  46.     /** 
  47.      * 设置传递到action中的ServletResponse 
  48.      * @param httpServletResponse 
  49.      */  
  50.     @Override  
  51.     public void setServletResponse(HttpServletResponse httpServletResponse) {  
  52.         response=httpServletResponse;  
  53.     }  
  54.   
  55.     /** 
  56.      * 设置传递到action中的ServletContext,即application 
  57.      * @param servletContext 
  58.      */  
  59.     @Override  
  60.     public void setServletContext(ServletContext servletContext) {  
  61.         context=servletContext;  
  62.     }  
  63. }  


二、使用Struts2RequestAware拦截器

接下来使用RequestAware拦截器来获取web资源,只要实现RequestAware接口就可以了,然后实现其中的setRequest方法。不同的是,这里将ServletRequestServletResponseServletContext对象放置在了一个集合当中,需要使用Struts2当中的key值来获取对应的对象。


[java] view plain copy
  1. package cn.lovepi.chapter03.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4. import org.apache.struts2.StrutsStatics;  
  5. import org.apache.struts2.interceptor.RequestAware;  
  6.   
  7. import javax.servlet.ServletContext;  
  8. import javax.servlet.ServletRequest;  
  9. import javax.servlet.ServletResponse;  
  10. import java.util.Map;  
  11.   
  12. /** 
  13.  * Created by icarus on 2016/7/9. 
  14.  * 使用RequestAware拦截器获取web资源 
  15.  * 需要实现RequestAware接口 
  16.  */  
  17. public class SecondAction extends ActionSupport implements RequestAware{  
  18.     private ServletResponse response;  
  19.     private ServletRequest request;  
  20.     private ServletContext context;  
  21.   
  22.     /** 
  23.      * 对应action方法 
  24.      * @return 
  25.      * @throws Exception 
  26.      */  
  27.     @Override  
  28.     public String execute() throws Exception {  
  29.         String username=request.getParameter("username");  
  30.         System.out.println("第二种方法获取web资源:"+username);  
  31.         return "success";  
  32.     }  
  33.   
  34.     /** 
  35.      * 获取对应的web资源,使用map集合方式 
  36.      * 即键值对获取 
  37.      * @param map 
  38.      */  
  39.     @Override  
  40.     public void setRequest(Map<String, Object> map) {  
  41.         request= (ServletRequest) map.get(StrutsStatics.HTTP_REQUEST);  
  42.         response= (ServletResponse) map.get(StrutsStatics.HTTP_RESPONSE);  
  43.         context= (ServletContext) map.get(StrutsStatics.SERVLET_CONTEXT);  
  44.     }  
  45. }  


三、使用Struts2内置静态对象ActionContext

这种方式来获取web资源并不需要实现任何的接口


[java] view plain copy
  1. package cn.lovepi.chapter03.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionContext;  
  4. import com.opensymphony.xwork2.ActionSupport;  
  5. import org.apache.struts2.ServletActionContext;  
  6.   
  7. import javax.servlet.ServletContext;  
  8. import javax.servlet.ServletRequest;  
  9. import javax.servlet.ServletResponse;  
  10.   
  11. /** 
  12.  * Created by icarus on 2016/7/9. 
  13.  * Struts2获取web资源对象第三种方法 
  14.  * 使用Struts2内置静态对象ActionContext 
  15.  * 使用这种方式不需要实现任何的接口 
  16.  * ----------------------------------- 
  17.  * 注:HttpServlet只是Servlet的子类 
  18.  */  
  19. public class ThirdAction extends ActionSupport{  
  20.     @Override  
  21.     public String execute() throws Exception {  
  22.         ActionContext ac=ActionContext.getContext();  
  23.         ServletRequest request= (ServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);  
  24.         ServletContext context= (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);  
  25.         ServletResponse response= (ServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE);  
  26.         String username=request.getParameter("username");  
  27.         System.out.println("第三种方式:"+username);  
  28.         return "success";  
  29.     }  
  30. }  

四、使用Struts2内置静态对象ServletActionContext

这是最简单最多使用的方法


[java] view plain copy
  1. package cn.lovepi.chapter03.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4. import org.apache.struts2.ServletActionContext;  
  5.   
  6. import javax.servlet.ServletContext;  
  7. import javax.servlet.ServletRequest;  
  8. import javax.servlet.ServletResponse;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. /** 
  12.  * Created by icarus on 2016/7/9. 
  13.  * Struts2获取web资源的第四种方式 
  14.  * 使用Struts2内置对象ServletActionContext 
  15.  * 这是四种方法中最好的方式 
  16.  * 不需要实现任何接口,也可以按照需求获取所需的对象 
  17.  */  
  18. public class FourthAction extends ActionSupport{  
  19.     @Override  
  20.     public String execute() throws Exception {  
  21.         ServletRequest request= ServletActionContext.getRequest();  
  22.         ServletResponse response=ServletActionContext.getResponse();  
  23.         ServletContext context=ServletActionContext.getServletContext();  
  24.         String username=request.getParameter("username");  
  25.         System.out.println("第四种方式:"+username);  
  26.         return "success";  
  27.     }  
  28. }  

总结:

       通过四种方法都可以获取到对应的web资源,但是使用Aware拦截器需要实现对应的接口才可以获取到对应的资源,而使用RequestAware则只需实现一个接口便可以获取到所有的web资源对象。对于使用Struts2内置的静态对象来获取Web资源来说并不需要实现任何的接口,相比较与前两种使用拦截器的方式来说,使用内置静态对象的方式更值得推荐。而在后两种方式中:使用内置对象ActionContext返回的是一个map集合,得需要使用对应的key来获取所需的资源对象。而ServletActionContext则可以根据不同的方法直接获取对应的web资源对象。

       虽然使用内置对象ServletActionContext的方式来获取Web资源是最好的方式,值得推荐。但是,我们也得了解前三种获取web资源的方式,因为在某些项目中可能会遇到别人使用前三种方式来获取web资源。但在日常的开发中我们必须得熟练掌握第四种获取web资源的方式,即ServletActionContext方式。


补充说明:

        ServletContext,也就是Application,服务器对象,只要服务器不关闭,这个对象就永远存在。该信息是存储与服务器中的,一般数据我们是严禁存储到application对象中去的,因为这样很容易导致服务器内存溢出,程序崩溃。

       他的实际应用场景是:在驾校考试系统中,用户只需要注册就可以免费答题,用户量庞大。而在页面中每次只出现一道题,答完之后显示下一道。假如我们每次的数据处理如下图所示:


如上图所示,我们在这里使用到了Application对象来存储对应的题目信息,这样省去了我们每次都去数据库中获取下一道题目的步骤。在这种情况下,系统一启动就从数据库中获取全部的题目信息保存到Application对象中去,然后用户只需访问ServletContext就好,然后直接在界面中显示对应的Application中的数据就行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值