开源MVC框架Struts (二) Struts工作流程分析

  1. 介绍
    1. 我们的生活每天都是井井有条的渡过,生活很有规律例如几点睡觉几点起床都已经形成了一个“生物钟”。
    1. 在工厂里面工人在流水线上辛勤地工作,各个环节构成了整个工作流程,想做好一件事情就要有一个完整的流程,有始有终。
    2. Struts为我们的项目的Web层设计了一套标准工作流程供我们遵循,即如果你使用Struts框架,就得按照别人的规则来做,下面让我们来分析一下Struts的工作流。
  1. 流程图
    1. 首先,来看一张流程图

      1. 执行步骤如下
        1. Tomcat启动时,先从web.xmlStruts-config.xml配置文件中信息加载到内存中,Struts-config.xml对应上图文件。
        1. 登录页面以login.do方式请求服务器,ActionServlet会把这一请求转发到相应Action进行处理。
          1. ActionServlet是继承了HttpServlet,对HTTPServlet做了实现。
        1. Action调用Model层,将返回的结果根据<forward />标签配置返回到指定页面。
        2. 上图中没有画出ActionMapping/FormBean,他们分别是保存配置文件信息和页面数据的对象,也起着相当重要的作用。
  1. DispatchDynaActionForm
    1. Dispatch
      1. 通常需要为每一个处理页面写一个Action类进行控制,但如果页面非常多了Action类就会很多,带来很多麻烦。
      2. Dispatch将每个Action的处理过程移到了每个方法里面,只需要写一个Action就可以完成。
    1. DynaActionForm
      1. ActionForm对应于表单上的属性,且每个变量必须与表单上属性名字一样,当表单属性增加或删除时,就需要修改ActionForm的内容,程序还需要从新编译,引入动态ActionForm解决了这个问题,只需要简单的配置即可。
  1. Dispatch例子
    1. 下面是一个物料添加、删除页面实例,调用过程。
    2. JSP请求页面
      1. <body>
        	<form>
        		<a href="item.do?command=add">物料</a>  
        		<a href="item.do?command=del">物料</a>  
        	</form> 
        </body>


    1. Dispatch Action
      1. package com.bjpowernode.drp.web.actions;
        
        import java.io.FileOutputStream;
        import java.util.List;
        
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        
        import org.apache.commons.beanutils.BeanUtils;
        import org.apache.struts.action.ActionForm;
        import org.apache.struts.action.ActionForward;
        import org.apache.struts.action.ActionMapping;
        import org.apache.struts.actions.DispatchAction;
        import org.apache.struts.upload.FormFile;
        
        import com.bjpowernode.drp.BeanFactory;
        import com.bjpowernode.drp.PageModel;
        import com.bjpowernode.drp.domain.Item;
        import com.bjpowernode.drp.domain.ItemCategory;
        import com.bjpowernode.drp.domain.ItemUnit;
        import com.bjpowernode.drp.service.DataDictService;
        import com.bjpowernode.drp.service.ItemService;
        import com.bjpowernode.drp.web.forms.ItemActionForm;
        
        public class ItemAction extends DispatchAction {
        
        	/**
        	 * 添加
        	 * @param mapping
        	 * @param form
        	 * @param request
        	 * @param response
        	 * @return
        	 * @throws Exception
        	 */
        	public ActionForward add(ActionMapping mapping, ActionForm form,
        			HttpServletRequest request, HttpServletResponse response)
        			throws Exception {
        		Item item=new Item();
        		ItemActionForm iaForm=(ItemActionForm)form;
        		BeanUtils.copyProperties(item, iaForm);
        		
        		ItemCategory itemCategory=new ItemCategory();
        		itemCategory.setId(iaForm.getCategory());
        		item.setItemCategory(itemCategory);
        		
        		ItemUnit itemUnit=new ItemUnit();
        		itemUnit.setId(iaForm.getUnit());
        		item.setItemUnit(itemUnit);
        		
        		ItemService itemService=(ItemService)BeanFactory.getInstance().getBean(ItemService.class);
        		itemService.addItem(item);
        		
        		return mapping.findForward("item_index");
        	}
        
        	/**
        	 * 删除
        	 * @param mapping
        	 * @param form
        	 * @param request
        	 * @param response
        	 * @return
        	 * @throws Exception
        	 */
        	public ActionForward del(ActionMapping mapping, ActionForm form,
        			HttpServletRequest request, HttpServletResponse response)
        			throws Exception {
        		ItemActionForm itemActionForm=(ItemActionForm)form;
        		ItemService itemService=(ItemService)BeanFactory.getInstance().getBean(ItemService.class);
        		itemService.delItem(itemActionForm.getSelectFlag());
        		
        		return mapping.findForward("item_index");
        	}
        	
        }
        


    1. Struts-config.xml配置
      1. <?xml version="1.0" encoding="ISO-8859-1" ?>
        
        <!DOCTYPE struts-config PUBLIC
                  "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
                  "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
        <struts-config>
        	<form-beans>
        		<form-bean name="itemForm" type="com.bjpowernode.drp.web.forms.ItemActionForm"/>
        	</form-beans>
        	<!-- 这里面的parameter参数值必须与请求的command名字一样 -->
        	<action-mappings>   
        		<action path="/item"
        				type="com.bjpowernode.drp.web.actions.ItemAction"
        				name="itemForm"
        				scope="request"
        			
        				parameter="command"   
        		>  	
        			<forward name="item_index" path="/item.do" redirect="true"/>
        		</action>   
        	</action-mappings>
        	
            <message-resources parameter="MessageResources" />
        </struts-config>


    1. 注意:1.页面请求的command=add,后面的值需要与Dispatch Action中方法名字相同,这样才能对应到相应的方法上。2.Struts-config.xml中需要配置parameter=command属性。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 16
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李龙生的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值