版权所有:(xiaodaoxiaodao)蓝小刀
				
				
						   
						
								xiaodaoxiaodao@gmail.com
						
						
						
						
				
		
		
				
						
								
										http://www.blogjava.net/xiaodaoxiaodao/archive/2007/06/10/123141.html
								
						     
				
						  
				
				
						
						
				
		
		
				
						转载请注明来源/作者
				
		
		
				
						
								 
						
				
		
		
				
						Struts
				
				
						学习笔记之Action
				
		
		
				
						 
				
		
		
				下面是Struts中的一些常用Action如DispatchAction/LookupDispatchAction/MappingDispatchAction/ForwardAction/IncludeAction的总结
		
		
				
						 
				
		
		
				1
				.DispatchAction extends BaseAction
		
		
				一般的Action如<action path="/createUser" type="examples.UserAction">,在这里UserAction只需要继承父类(extends Action类),然后重写父类的execute方法,在execute中实现具体的控制转向。
		
		
				对于同一个formbean上进行的新增、修改、删除等,我们需要分发不同的Action,这里有两种做法。
		
		
				
						①
				
				
				
				一种是通过在execute方法中if判断进行不同的转向:
		
		
				㈠
				
				
				UserAction
				类的execute方法中
		
		
				String method = request.getParameter("method");
				
						
						
				
		
		
				if (method.equals("create")) {
		
		
				
						 
						   
				
				…… 
		
		
				
						    return mapping.findForward("createUser");
		
		
				}
		
		
				if (method.equals("save")) { 
		
		
				
						    
				
				……
		
		
				
						    return mapping.findForward("saveUser");
		
		
				}
		
		
				
						 
				
		
		
				㈡ 
				struts-config.xml
				中:
		
		
				<action path="/createUser" type="examples.UserAction"
		
		
				
						    
						    name="userForm"
		
		
				
						        scope="request">
		
		
				
						    <forward name="createUser" path="/pages/listUser.jsp"/>
		
		
				</action>
		
		
				<action path="/saveUser" type="examples.UserAction"
		
		
				
						        name="userForm"
		
		
				
						        scope="request">
		
		
				
						    <forward name="saveUser" path="/pages/saveUser.jsp"/>
		
		
				</action>
		
		
				
						 
				
		
		
				㈢ 
				可以在页面中定义一个隐藏变量来指明相应的操作
		
		
				// 
				这里最好不要使用<html:hidden property="method"/>
		
		
				// 
				因为这种写法需要在formbean中定义相应的property,我们可以采用普通隐藏域
		
		
				<input type="hidden" name="method"/>
				
						
						
				
		
		
				然后定义一个javascript函数,我们可以在通过点击提交按钮的时候,在函数中修改它的值。
		
		
				<script>
		
		
				
						    function set(operation) {
		
		
				
						       
						
								 with (document.forms[0]) {
		
		
				
						            method.value = operation;
		
		
				
						        }
		
		
				
						    }
		
		
				</script>
				
						
						
				
		
		
				点击提交按钮时,通过set方法设置提交的method属性值:
		
		
				<html:submit onclick="set('create');">CREATE</html:submit>
		
		
				<html:submit onclick="set('save');">SAVE</html:submit>
				
						
						
				
		
		
				
						 
				
		
		
				
						 
				
		
		
				
						② 
				
				第二就是使UserAction继承DispatchAction,不需要重写execute方法:
		
		
				public ActionForward create(ActionMapping mapping,
		
		
				
						                           ActionForm form,
		
		
				
						                           HttpServletRequest request,
		
		
				
						                           HttpServletResponse response)
		
		
				
						        throws Exception {
		
		
				
						    
						// 
				
				進行一些create的逻辑
		
		
				
						    // ……
		
		
				
						    return mapping.findForward("createUser");
		
		
				}
		
		
				public ActionForward save(ActionMapping mapping,
		
		
				
						                           ActionForm form,
		
		
				
						                           HttpServletRequest request,
		
		
				
						                          
						 HttpServletResponse response)
		
		
				
						        throws Exception {
		
		
				
						    
						// 
				
				進行一些save的逻辑
		
		
				
						    // ……
		
		
				
						    return mapping.findForward("saveUser");
		
		
				}
				
						
						
				
		
		
				
						 
				
		
		
				㈡ 
				DispatchAction
				在配置上和一般Action稍有不同,就是要在Action配置中多一个parametr属性,这个属性可以指定执行DispatchAction中对应的方法。
		
		
				struts-config.xml
				中:
		
		
				<action path="/processUser" type="examples.UserAction"
		
		
				
						        name="userForm"
		
		
				
						        scope="request"
		
		
				
						        
						parameter="method">
		
		
				
						    <forward name="createUser" path="/pages/listUser.jsp"/>
		
		
				
						    <forward name="saveUser" path="/pages/saveUser.jsp"/>
		
		
				</action>
		
		
				
						 
				
		
		
				㈢ 
				我们在这里指定了parameter的值为method,则页面提交时我们必须指定提交时action的method参数来确定去我们想要调用哪个Action方法。
		
		
				<script>
		
		
				
						    function submitForm(operation) {
		
		
				
						       
						
								 with (document.forms[0]) {
		
		
				
						            action = action + '?method = '+ operation;
		
		
				
						            submit();
		
		
				
						        }
		
		
				
						    }
		
		
				</script>
				
						
						
				
		
		
				点击提交按钮时,通过submitForm方法设置提交时action的method参数:
		
		
				<html:form action="/processUser" method="get">
		
		
				<html:button onclick="submitForm('create');">CREATE</html:button>
		
		
				<html:button onclick="submitForm('save');">SAVE</html:button>
		
		
				</html:form>
				
						
						
				
		
		
				
						 
				
		
		
				2
				. LookupDispatchAction extends DispatchAction
		
		
				LookupDispatchAction
				继承DispatchAction, 在上面的
				
						② 
				
				㈢
				中对于同一个页面上的多个submit按钮,不需要那么多复杂的js函数来指定提交时action的method参数,即上面的submitForm(operation)方法可以省去,LookupDispatchAction其用法为:
		
		
				
						
								
										①
										用MessageResource将按钮的文本和ResKey相关联,例如button.save=保存;
										② ㈢
								
										中用LookupDispatchAction代替就是:
								
						
						
								
										<html:form action="/processUser" method="get">
								
						
						
								
										<html:submit property="
										
												method
										
										">
								
						
						
								
										
												    <bean:message key="
										button.create
										"/>
								
						
						
								
										</html:submit>
								
						
						
								
										<html:submit property="
										
												method
										
										">
								
						
						
								
										
												    <bean:message key="
										button.save
										"/>
								
						
						
								
										</html:submit>
								
						
						
								
										</html:form>
										
												
												
										
								
						
						
								
										
												 
										
								
						
						
								
										②
										在Action配置中多一个parametr属性,属性值与submit按钮的property属性值相同,这个属性可以指定执行LookupDispatchAction中对应的方法。
								
						
						
								
										struts-config.xml
										中:
								
						
						
								
										<action path="
										
												/processUser
										
										" type="examples.UserAction"
								
						
						
								
										
												        name="userForm"
								
						
						
								
										
												        scope="request"
								
						
						
								
										
												        
												parameter="
										
												method
										
										">
								
						
						
								
										
												    <forward name="createUser" path="/pages/listUser.jsp"/>
								
						
						
								
										
												    <forward name="saveUser" path="/pages/saveUser.jsp"/>
								
						
						
								
										</action>
								
						
						
								
										
												 
										
								
						
						
								
										③ 
								
						
				
		
		使UserAction继承LookupDispatchAction,重写getKeyMethodMap()方法, 将ResKey和MethodName对应起来, 如下:
		
				protected Map getKeyMethodMap() {
		
		
				
						    Map map = new HashMap();
		
		
				
						    map.put("button.create", "create");
		
		
				
						    map.put("button.save", "save");
		
		
				
						    return map;
		
		
				}
		
		
				
						 
				
		
		
				
						注:
				
				DispatchAction
				类使用请求参数的值确定调用哪种方法,而LookupDispatchAction类利用请求参数值,反向查询资源绑定,并将它与类中的一种方法匹配,实际上这两种方法有异曲同工之处。
		
		
				
						 
				
		
		
				3
				. MappingDispatchAction extends DispatchAction
		
		
				DispatchAction
				指定了parameter的值为method,则页面提交时我们必须指定提交时action的method参数来确定去我们想要调用哪个Action方法,而MappingDispatchAction直接通过struts-config.xml将多个action-mapping映射到同一个Action类的不同方法:
		
		
				<action path="/createUser" type="examples.UserAction"
		
		
				
						        name="userForm"
		
		
				
						        scope="request"
		
		
				
						        
						parameter="create">
		
		
				
						    <forward name="createUser" path="/pages/listUser.jsp"/>
		
		
				</action>
		
		
				<action path="/saveUser" type="examples.UserAction"
		
		
				
						        name="userForm"
		
		
				
						        scope="request"
		
		
				
						        
						parameter="save">
		
		
				
						    <forward name="saveUser" path="/pages/saveUser.jsp"/>
		
		
				</action>
				
						
						
				
		
		
				
						 
				
		
		
				然后UserAction继承MappingDispatchAction即可:
		
		
				public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
		
		
				public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
				
						
						
				
		
		
				
						 
				
		
		
				
						注:
				
				查看MappingDispatchAction的源码:
		
		
				protected String getMethodName(ActionMapping mapping, ActionForm form,
		
		
				
						    HttpServletRequest request, HttpServletResponse response,
		
		
				
						    String parameter) throws Exception {
		
		
				
						    
						// Return the unresolved mapping parameter.
				
		
		
				
						    return parameter;
		
		
				}
				
						
						
				
		
		
				可以看到它调用的方法是直接返回struts-config.xml中parameter的值。
		
		
				
						 
				
		
		
				4
				. ForwardAction extends BaseAction
		
		
				相当于<jsp:forward>功能,不需要配置formbean和action,可以直接进行跳转,只需要在struts-config.xml中配置:
		
		
				<action path="/listUser"
		
		
				
						        type="org.apache.struts.actions.ForwardAction"
		
		
				
						       
						 scope="request"
		
		
				
						        parameter="/pages/listUser.jsp">
		
		
				</action>
				
						
						
				
		
		
				parameter
				属性用于指定forward到哪个页面,path、type、parameter三个属性为必须,其他可省略。
		
		
				
						 
				
		
		
				5
				. IncludeAction extends BaseAction
		
		
				相当于<jsp:include>功能,需要在struts-config.xml中配置:
		
		
				
						<action path="/listUser" type="org.apache.struts.actions.IncludeAction"
				
				
						
								        name="userForm"
				
				
						
								        scope="request"
				
				
						
								        parameter="/pages/includepage.jsp">
				
				
						</action>
						
								
								
						
				
				
				
		
		
				 
		
		
				
						 
				
		
		
				
						版权所有:(xiaodaoxiaodao)蓝小刀
				
				
						   
						
								xiaodaoxiaodao@gmail.com