AppFuse1.8中BaseAction的一些主要方法说明

功能:

通过请求的参数来决定该执行哪一个方法,而不像一般的Action,从Execute方法执行。在一个窗体中包括两个以上同名的按钮时,由Struts来决定具体执行那个按钮操作,减少了Action类,增加了请求处理的灵活性

AppFuse1.7与AppFuse1.8的BaseAction区别

AppFuse1.7是继承LookupDispatchAction, 需要复写 getKeyMethodMap()方法

具体可参考http://blog.csdn.net/GOALSTAR/archive/2005/12/05/543802.aspx

AppFuse1.8是继承DispatchAction

AppFuse1.8中BaseAction的主要方法

1.getBean()方法,得到Bean的对象

public Object getBean (String name) {

     if (ctx == null) {

            ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());

     }

      return ctx.getBean(name);

}


2.convert()
对象转换方法,用来实现POJOs à ActionForm & ActionForm à POJOs 的转换

// 调用ConvertUtil#convert(java.lang.Object)
protected Object convert (Object o) throws Exception {

              return ConvertUtil.convert(o);

}


3.execute()
的方法

public ActionForward execute (ActionMapping mapping, ActionForm form,

                          HttpServletRequest request, HttpServletResponse response)

    throws Exception {

       

        if (isCancelled(request)) {

            try {

                getMethod("cancel");

                return dispatchMethod(mapping, form, request, response, "cancel");

            } catch (NoSuchMethodException n) {

                log.warn("No 'cancel' method found, returning null");

                return cancelled(mapping, form, request, response);

            }

        }

 

        // Check to see if methodName indicated by request parameter

        String actionMethod = getActionMethodWithMapping(request, mapping);

        

        if (actionMethod != null) {

            return dispatchMethod(mapping, form, request, response, actionMethod);

        } else {

            String[] rules = {"edit", "save", "search", "view"};

            for (int i = 0; i < rules.length; i++) {

                // apply the rules for automatically appending the method name

                if (request.getServletPath().indexOf(rules[i]) > -1) {

                    return dispatchMethod(mapping, form, request, response, rules[i]);

                }

            }

        }

       

        return super.execute(mapping, form, request, response);

    }

AppFuse1.8中BaseAction是继承DispatchAction , 使用struts时我们经常会用到DispatchAction.有了这个类,我们不需要针对每一个Action都要写一个特定的类,而是可以把一些相关的方法放到一个类中.
DispatchActon
中使用了reflection来根据你传入的method参数的值来获取相应的参数来处理你的请求.正如他的方法 -- 他根据你传入的请求参数,用不同的方法来处理你的请求.

  
只要看看strutsDispatchAction(以下简写做DA)的源文件你就会发现,它有一个dispatchMethod方法,接受5个参数.其中4个就是我们通常的struts action里的(mapping,request,response,form),还有一个参数就是指定方法的参数的名字.

 

Ø Struts-config.xml中的配置

<action path="/editUser" ype="smarthings.webapp.action.UserAction"  name="userForm" scope="request"

input="list" roles="admin" parameter="method" unknown="false" validate="false" >

<forward name="viewUsers" path="/editUser.html?method=Search( edit / save / delete )"/>

</action>

 

然后在Action中具体的实现Action都是继承了BaseAction.比如

  public final class PeopleAction extends BaseAction {

    //具体的操作方法search

    public ActionForward search(ActionMapping mapping, ActionForm form,

                                HttpServletRequest request,

                                HttpServletResponse response)

         throws Exception {

        //具体的操作方法cancel

    public ActionForward cancel(ActionMapping mapping, ActionForm form,

                                HttpServletRequest request,

                                HttpServletResponse response)

    throws Exception {………}

 //具体的操作方法delete

    public ActionForward delete(ActionMapping mapping, ActionForm form,

                                HttpServletRequest request,

                                HttpServletResponse response)

         throws Exception {………}

 

//具体的操作方法Edit

   public ActionForward edit(ActionMapping mapping, ActionForm form,

                                HttpServletRequest request,

                                HttpServletResponse response)

         throws Exception {……}

………

………

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值