带formbean的Struts工作过程

来源:互联网 发布:知乎如何挣钱 编辑:程序博客网 时间:2024/06/06 08:57

 

struts的核心在于org.apache.struts.action.ActionServlet包,在web-Inf下面,web.xml配置了
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

这样我们就将所有的.do的请求都交给了主控制器,我们可以想象一下,

在struts-config.xml中有很多节点,最重要的就是<action-mapping>,<action-mapping>中会有很多个

<action>节点,若果我们将struts-config.xml解析成一个对象,可以想象,它肯定由一个action的集合,
class StrutsConfig{
   private List<Action> list;

private List<Bean> list_bean;
   public List<Action> getList(){
   }
   public void setList(List list){
     this.list = list;
   }
}
而每一个action节点,可以写成一个对象
class ActionConfig{
   private String path;
   private String type;

private String name;

......
   get,set方法;
}
ActionServlet的工作过程,首先,ActionServlet是一个servlet,它继承子httpservlet
Class ActionServlet extends HttpServlet{

public void doGet(HttpServletRequest request,HttpServletRespoonse response){
    doPost(request,response);
}

public void doPost(HttpServletRequest request,HttpServletRespoonse response){
    
        String url = request.getRequestURI();
        ServletContext ctx = getServletContext();
        StrutsConfig sc = (StrutsConfig)ctx.getAttribute("config");
        List<Action> list = sc.getList();

List<Bean> list_bean =sc.getListBean();
ActionForm form = null;

        for(int i=0;i<list.size();i++){
         ActionConfig temp = list.get(i);
         String path = temp.getPath();
         if(url.equals(path+".do")){//路径匹配

String name = temp.getName();
for(int i =0;i<list_bean.size();i++){
   if(list_bean.get(i).getName.equals(name)){//这个formBean与这个Action对应
       String bean_type = list.get(i).getType();//找到formBean对应的类
        Class clazz = Class.forName(bean_type);
       form = clazz.newInstance();//创建formbean对象
       Field[] field = clazz.getDeclaredFields();
       for(int i=0;i<field.length;i++){
          String str = request.getParameter(field[i].getName());
          field[i].setValue(str);
        }
    }
}


           String tpye = temp.getType();
           Action a = Class.forName(type).newInstance();//反射生成要执行的Action对象
           ActionForward af=a.execute(mapping,form,request,response);
           request.getRequestDispatcher(af.getString()).forward(request,response);
           //这里只能用主控制器进行控制跳转,因为request是容器负责控制的,容器只能读web.xml,因

为我们在web.xml中,只配置了主控制器,所以容器不知道跳转到哪里,只能通过主控制器
           break;
         }
         }

Public void init(){

         String filePath = "/WEB-INF/struts-config.xml";
//通过解析生成StrutsConfig对象
        
           ServletConfig sc = getServletConfig();
           String str = sc.getInitialParameter("config");//容器从web.xml中读初始化参数
           if(str!=null&&str.length()>0){
      filePath = str;
           }
            StrutsConfig s = StrutsConfigRead.parse(); //解析strut-config.xml文件
           ServletContext ctx = getServletContext();
     ctx.setAttribute("config",s);//将解析结果放在全局容器中
}
}

原创粉丝点击