Allin's blog

先做正确的事,然后才是正确的做事

leeshaoqunID:leeshaoqun
135162次访问,排名549好友0人,关注者2
leeshaoqun的文章
原创 90 篇
翻译 1 篇
转载 129 篇
评论 35 篇
allin的公告
  • leeshaoqun的博客
  • 电子科技大学中山学院
  • 计算机工程系
老照片
最近评论
mldstk:wow gold,
mldstk:wow gold,
alex.xu:漂亮 ,说实话 比官方文档写的好
lkuzhi:是啊,能不能找个能用的过来啊
泽雄:已经不能用了
文章分类
收藏
    相册
    ♡韩国可爱的小童星 ♡
    korea
    韩佳人
    金泰熙
    宋惠乔
    宋慧乔车太贤《我和我的女友》
    微笑美女——韩孝珠
    Hibernate
    Hibernate中文网
    Hibernate官方网站
    php
    Haohappy的专栏--PHP5研究中心
    Spring
    Raible's Wiki -- AppFuse
    Spring 开发参考手册
    SpringFramework中文论坛
    SpringFramework官方站点
    个人博客
    greengnn's space
    开源
    java开源大全(RSS)
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 action-mappings之attribute 和 parameter属性收藏

    新一篇: 把一个java web应用包装成桌面应用的简单做法 | 旧一篇: Jakarta Commons-DbUtils

     

    action-mappings的attribute 属性

    在《Programming Jakarta Struts》这本书中的第四章“Configuring the Struts Application”中这样一段说明来分别阐述这两
    个属性:(102页)
    ++++++++
    atribute:
    ++++++++
    The name of the request or session scope attribute under which the form bean for this action can be accessed.
    A value is only allowed here if there is a form bean specified in the name attribute. This attribute is
    optional and has no default value.

    ++++++++
    name:
    ++++++++
    The name of the form bean, if any, that is associated with this action. This value must be the name attribute
    from one of the form-bean elements defined earlier. This attribute is optional and has no default value.

    1)应用前提,attribute只有在设置了name后才有意义。
    2)attribute可以实现对象的重用,即如果设置了attribute属性,在创建actionform是,会先去查找相应的scope中是否有此对象,如果有,则重用,否则创建新的对象。
    3)当你将创建的acitonForm保存到相应的scope中时,你想用一个更有意义的名字来访问它时,它就有意义了。例如:
    配置form.
    <form-bean name="employee" type="Employee"/>
    配置action:
    <action
    attribute="validEmployee"
    name="employee"
    type="EmployeeAction"
    scope="request"
    path="/employee">
    .....
    这样就可以用validEmployee在JSP页面中访问了,而不是用employee.
    这在同一个form 在不同情况下有不同的意义时,意义才很明显。



    action-mappings的parameter 属性


    没有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引入DispatchAction,这样你在struts-config.xml文件的action元素中增加 parameter属性即可实现这个功能:

    例如appfuse的配置:
    <action
           path="/saveUser"
           type="org.appfuse.webapp.action.UserAction"
           name="userForm"
           scope="request"
           input="edit"
           parameter="method"
           unknown="false"
           validate="false"
         >
           <forward
             name="list"
             path="/WEB-INF/pages/userList.jsp"
             redirect="false"
           />
           <forward
             name="edit"
             path="/WEB-INF/pages/userForm.jsp"
             redirect="false"
           />
         </action>

    parameter ="method"这个参数就是说,在用户提交请求时取得method参数,根据method参数调用相应的方法,如/editUser.html? method=Delete就是调用对应action中的Delete方法,这样你就可以写一个Action类处理很多的逻辑,而不是象从前那样在一个方 法里面加上若干参数,或者直接建若干个action来处理。

    例如appfuse的UserAction

    package org.appfuse.webapp.action;

    import ...


    public final class UserAction extends BaseAction {
        
         public ActionForward add(ActionMapping mapping, ActionForm. form,
                                  HttpServletRequest request,
                                  HttpServletResponse response)
         throws Exception {
             ...
         }

         public ActionForward cancel(ActionMapping mapping, ActionForm. form,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
         throws Exception {
             ...
         }

         public ActionForward delete(ActionMapping mapping, ActionForm. form,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
         throws Exception {
             ...
         }

         public ActionForward edit(ActionMapping mapping, ActionForm. form,
                                   HttpServletRequest request,
                                   HttpServletResponse response)
         throws Exception {
             ...
         }

         public ActionForward save(ActionMapping mapping, ActionForm. form,
                                   HttpServletRequest request,
                                   HttpServletResponse response)
         throws Exception {
             ...
         }

         public ActionForward search(ActionMapping mapping, ActionForm. form,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
         throws Exception {
             ...
         }
        
         public ActionForward unspecified(ActionMapping mapping, ActionForm. form,
                                          HttpServletRequest request,
                                          HttpServletResponse response)
         throws Exception {
            
             ...
         }

         private void sendNewUserEmail(HttpServletRequest request, UserForm. userForm)
         throws Exception {
             ...
         }

    }

    当你没有传入method参数,或者没有符合参数的方法时,程序将执行unspecified方法;当然method只是一个逻辑名字而已,你也可以使用其他名字,如:method1,method2,go2,asdad等

    发表于 @ 2008年04月14日 14:33:00|评论(loading...)|编辑

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © allin