<action>元素属性parameter的作用


转发自:http://ocaicai.iteye.com/blog/1096275

1.简介:

没有struts之前,使用servlet,最常用的是 doGet,doPost,service方法,如果有些经验的程序员会合理的使用这三个方法:如在用户发出get的请求时,将用户请求在doGet方法中处理,用户发出post请求时,将用户的请求用doPost请求处理,必要时加上service方法去处理那些在一个servlet中必须执行的请求,用户的请求大体也就这三类,但是如果细分,一个“编辑”,“删除”,“查看”等操作都是doGet的范围,当然也可以都写到serice方法中或 doPost中处理,这样为了区分这些请求,我们通常都要在程序中加入一个判断的参数,如:operate,然后在程序中判断 if operate.equals("update")....,if operate.equals("del")....,if operate.equals("view")....等,实际上这只是个简单的逻辑,如果业务更加复杂,你可能写更多的类时operate的参数,这样就造成程序中有若干if..else if...else if ..,即便你有非常好的编码规范,整齐的缩进,这个程序也相当难维护;而用到struts时,你又可能把这些参数都写到execute方法中;那么最好的方法还是将这些逻辑分开处理,如果执行“编辑”操作的时候调用“编辑”对应的方法,执行“删除”的时候调用“删除”对应的方法...将是比较理想的结果,为了实现这个应用要求,struts引入许多类型的工具类,如:MappingDispathAction,LookDispachAction,DispatchAction,以满足不同要求的需要,这样你在struts-config.xml文件的action元素中增加 parameter属性即可实现这个功能。



2.下面是Struts中的一些常用Action如DispatchAction/LookupDispatchAction/MappingDispatchAction/ForwardAction/IncludeAction的总结


Java代码   收藏代码
  1. ② 第二就是使UserAction继承DispatchAction,不需要重写execute方法:  
  2. public ActionForward create(ActionMapping mapping,  
  3.                            ActionForm form,  
  4.                            HttpServletRequest request,  
  5.                            HttpServletResponse response)  
  6.         throws Exception {  
  7.     // 進行一些create的逻辑  
  8.     // ……  
  9.     return mapping.findForward("createUser");  
  10. }  
  11. public ActionForward save(ActionMapping mapping,  
  12.                            ActionForm form,  
  13.                            HttpServletRequest request,  
  14.                            HttpServletResponse response)  
  15.         throws Exception {  
  16.     // 進行一些save的逻辑  
  17.     // ……  
  18.     return mapping.findForward("saveUser");  
  19. }  
元素属性parameter的作用" style="display: none;">② 第二就是使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");
}

Xml代码   收藏代码
  1.  DispatchAction 在配置上和一般Action稍有不同,就是要在Action配置中多一个parametr属性,这个属性可以指定执行DispatchAction中对应的方法。  
  2. struts-config.xml 中:  
  3. <action path="/processUser" type="examples.UserAction"  
  4.         name="userForm"  
  5.         scope="request"  
  6.         parameter="method">  
  7.     <forward name="createUser" path="/pages/listUser.jsp"/>  
  8.     <forward name="saveUser" path="/pages/saveUser.jsp"/>  
  9. </action>  



4.详细请参考:
(1)
http://blog.csdn.net/newpiaoyun/archive/2008/09/10/2907703.aspx

(2)
http://huangliangbao.iteye.com/blog/816332


5.总结:
这样我们就可以不再在execute方法中写那么多的if(){}else(){}咯


一、

让Action方法继承至DispatchAction

Java代码   收藏代码
  1. package com.cdl.mail.struts.action;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5. import org.apache.struts.action.ActionForm;  
  6. import org.apache.struts.action.ActionForward;  
  7. import org.apache.struts.action.ActionMapping;  
  8. import org.apache.struts.actions.DispatchAction;  
  9.   
  10.   
  11. public class StudentAction extends DispatchAction  {  
  12.   
  13.     //不需要重写execute方法了  
  14. //  public ActionForward execute(ActionMapping mapping, ActionForm form,  
  15. //          HttpServletRequest request, HttpServletResponse response) {  
  16. //      String paraStr = request.getParameter("actionMethod");  
  17. //      System.out.println("execute方法获得参数:" + paraStr);  
  18. //      return mapping.findForward("success");  
  19. //  }  
  20.     public ActionForward isExist(ActionMapping mapping, ActionForm form,  
  21.             HttpServletRequest request, HttpServletResponse response) {  
  22.         String paraStr = request.getParameter("actionMethod");  
  23.         System.out.println("isExist方法获得参数:" + paraStr);  
  24.         return mapping.findForward("success");  
  25.     }  
  26.     public ActionForward save(ActionMapping mapping, ActionForm form,  
  27.             HttpServletRequest request, HttpServletResponse response) {  
  28.         String paraStr = request.getParameter("actionMethod");  
  29.         System.out.println("save方法获得参数:" + paraStr);  
  30.         return mapping.findForward("success");  
  31.     }  
  32. }  
元素属性parameter的作用" style="display: none;">package com.cdl.mail.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;


public class StudentAction extends DispatchAction  {

	//不需要重写execute方法了
//	public ActionForward execute(ActionMapping mapping, ActionForm form,
//			HttpServletRequest request, HttpServletResponse response) {
//		String paraStr = request.getParameter("actionMethod");
//		System.out.println("execute方法获得参数:" + paraStr);
//		return mapping.findForward("success");
//	}
	public ActionForward isExist(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String paraStr = request.getParameter("actionMethod");
		System.out.println("isExist方法获得参数:" + paraStr);
		return mapping.findForward("success");
	}
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String paraStr = request.getParameter("actionMethod");
		System.out.println("save方法获得参数:" + paraStr);
		return mapping.findForward("success");
	}
}


二、在struts-config.xml中配置<action>的parameter属性,相当于map的key,一个key可以对应多个value哦!

Xml代码   收藏代码
  1. <action parameter="actionMethod" path="/student"   
  2.     type="com.cdl.mail.struts.action.StudentAction">  
  3.     <set-property property="cancellable" value="true" />  
  4.     <forward name="success" path="/index.jsp" />  
  5. </action>  


三、在视图层testPara.jsp中,在url中配置参数key-value,相当于一个key可以对应多个value哦!实际上就是一个类对应到多个方法!

Html代码   收藏代码
  1. <body>  
  2.         <a href="student.do?actionMethod=isExist">执行isExist方法</a> <br>  
  3.         <a href="student.do?actionMethod=save">执行save方法</a> <br>  
  4. </body>  
元素属性parameter的作用" style="display: none;">  <body>
				<a href="student.do?actionMethod=isExist">执行isExist方法</a> <br>
				<a href="student.do?actionMethod=save">执行save方法</a> <br>
  </body>



最后,有图有真相:













.



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值