大家都知道,Struts控制器组件负责接受用户请求,更通模型,以及返回给用户合适的视图组件. 
控制器将模型层和视图层分开,这样分离,可以为同一个模型开发出不同的视图. 
        下面时Struts的三大主要组件 
ActionServlet组件:充当Struts框架的中央控制器 
RequestProcessor组件:充当每个子应用模块的请求处理器 
Action组件:真正来处理一项具体的业务. 
一. Struts的init()方法 
Struts应用中只存在
ActionServlet的一个实例,Servlet容器在启动时,或者用户首次请求
ActionServlet时加载
ActionServlet类.在这两种情况下,servlet容器都会在
ActionServlet容器被加载后立即执行它的init()方法,这可以保证
ActionServlet处理用户请求时已经被初始化. 
下面根据Init()讲述Struts的初始化过程 
public void init() throws ServletException {   
  
          
          
          
        try {   
  
1)initInternal();   
  
  
2)initOther();   
  
  
3) initServlet();   
  
  
getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);   
  
  
initModuleConfigFactory();   
  
  
4)ModuleConfig moduleConfig = initModuleConfig("", config);   
  
  
5)initModuleMessageResources(moduleConfig);   
  
  
6)initModuleDataSources(moduleConfig);   
  
  
7)initModulePlugIns(moduleConfig);   
  
  
moduleConfig.freeze();   
           
  
      Enumeration names = getServletConfig().getInitParameterNames();   
            while (names.hasMoreElements()) {   
                String name = (String) names.nextElement();   
                if (!name.startsWith("config/")) {   
                    continue;   
                }   
                String prefix = name.substring(6);   
                moduleConfig = initModuleConfig   
                    (prefix, getServletConfig().getInitParameter(name));   
                initModuleMessageResources(moduleConfig);   
                initModuleDataSources(moduleConfig);   
                initModulePlugIns(moduleConfig);   
                moduleConfig.freeze();   
     }   
  
  
this.initModulePrefixes(this.getServletContext());   
  
            this.destroyConfigDigester();   
        } catch (UnavailableException ex) {   
            throw ex;   
        } catch (Throwable t) {   
              
              
              
            log.error("Unable to initialize Struts ActionServlet due to an "  
                + "unexpected exception or error thrown, so marking the "  
                + "servlet as unavailable.  Most likely, this is due to an "  
                + "incorrect or missing library dependency.", t);   
            throw new UnavailableException(t.getMessage());   
        }       
}  
       将各个子模块应用(除了默认的)的前缀存到一个字符数组中,并放到servletcontext中,对于默认的子应用模块,在appclication范围内存放他的MoudleConfig实例的key为“org.apache.struts.action.MODULE”,其他模块如/account,存放的key为org.apache.struts.action.MODULE/account,消息,数据源和插件等部分存在servletcontext的key和上述方法类似,不在说明. 
二.ActionServlet的process方法 
当ActionServlet接受到HTTP请求后,在doget()或doPost()方法中都会调用process()方法来处理请求. 
 public void doGet(HttpServletRequest request,   
              HttpServletResponse response)   
        throws IOException, ServletException {   
  
        process(request, response);   
  
}   
  public void doPost(HttpServletRequest request,    
               HttpServletResponse response)   
        throws IOException, ServletException {   
  
        process(request, response);   
  
}   
下面是process方法,它看上去并不复杂,但他调用的其他方法比较复杂. 
 protected void process(HttpServletRequest request, HttpServletResponse response)    
        throws IOException, ServletException {   
          
        ModuleUtils.getInstance().selectModule(request, getServletContext());   
  
        ModuleConfig config = getModuleConfig(request);   
      
        RequestProcessor processor = getProcessorForModule(config);   
      
        if (processor == null) {   
           processor = getRequestProcessor(config);   
        }   
  
        processor.process(request, response);   
    }  
三. 扩展ActionServlet类 
从Struts1.1开始,为减轻ActionServlet的负担,多数功能已经移到RequestProcessor类中,所以基本不用扩展ActionServlet类 
如果需要创建自己的ActionServlet,则可以创建一个它的子类.覆盖init()方法(或其他方法),可以写一些自己的操作,但要先调用super.init(); 
定义如下的类: 
package sample;    
public class ExtendedActionServlet extends ActionServlet {    
        public void init() throws ServletException {    
               super.init();    
                 
               ……………    
        }    
}   
扩展完类后,还应该在web.xml文件中如下配置:
<servlet>    
        <servlet-name>sample</servlet-name>    
        <servlet-class>sample.ExtendedActionServlet</servlet-class>    
</servlet>    
    
<servlet-mapping>    
       <servlet-name>sample</servlet-name>    
       <url-pattern>/action/*<url-pattern> 
上面的/action/*表示负责处理所有以/action为前缀的URL,后面的/表示转义 
  
	
posted on 2009-04-05 11:10 
lanxin1020 阅读(207) 
评论(0)  编辑  收藏  所属分类: 
struts1