DispatchAction的使用

1,先写一个CommonAction类
    public class CommonAction extends DispatchAction {

    private static ApplicationContext context = null;

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

        // Identify the request parameter containing the method name
        String parameter = mapping.getParameter();
        if (parameter == null) {
            String message = messages.getMessage("dispatch.handler", mapping
                                                 .getPath());
            log.error(message);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                               message);
            return (null);
        }

        // Identify the method name to be dispatched to.
        // dispatchMethod() will call unspecified() if name is null
        String name = request.getParameter(parameter);

       
        // to do : check the authorization...
       

        // Invoke the named method, and return the result
        return dispatchMethod(mapping, form, request, response, name);
    }
}
该类的主要方法是dispatchMethod负责调用Action类的name方法

2.现在具体写一个类,该类包含save和list方法

public class ListAllLeavesAction extends CommonAction {
   
    public ActionForward list(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
       
        LybanForm listForm = (LybanForm)form;
       
        Session session = HibernateUtil.currentSession();
        Criteria c = session.createCriteria(Ly.class);
       
        listForm.setFields(c.list());
        return mapping.findForward("list");
    }
   
    public ActionForward save(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        LybanForm addLybanForm = (LybanForm) form;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();

        Session session = HibernateUtil.currentSession();
        Transaction t = session.beginTransaction();
        Ly ly = new Ly();
        ly.setMail(addLybanForm.getMail());
        ly.setMessages(addLybanForm.getMessages());
        ly.setName(addLybanForm.getName());
        ly.setTitle(addLybanForm.getTitle());
        ly.setTime(sdf.format(now));
        session.save(ly);
        t.commit();

        HibernateUtil.closeSession();

        return mapping.findForward("success");
    }
}

3 .现在看一下页面是如何调用ListAllLeavesAction类的save方法
<body>
    <html:form action="/jsp/lyb/manageLyban" method="post">
        <table width="90%" border="1" align="center" bordercolor="#CCCCCC">
                <tr>
                <input type="hidden" name="method" value="save"/>//该隐藏域的method属性的值就是ListAllLeavesAction类对应的save方法的名字
                    <td width="100" align="right" class="tdright">
                            &nbsp;留言标题<span class="red">*</span>:                    </td>
                    <td width="185" class="TDTEXT2">
                        <input name="title" type="text">
                  &nbsp;</td>
                    <td width="98" align="right" class="tdright">
                    <div align="right">留言人:</div>
                    </td>
                    <td width="279" class="TDTEXT2"><input name="name" type="text">&nbsp;</td>
                </tr>

                <tr>
                    <td align="right" class="tdright">
                        <div align="right">
                            留言内容<span class="red">*</span>:                        </div>                    </td>
                    <td width="185" class="TDTEXT2"><input name="messages" type="text"></td>
                    <td align="right" class="tdright">
                        <div align="right">
                            留言人Email:</div>    </td>
                    <td width="279" class="TDTEXT2"><input name="mail" type="text"></TD>
                </tr>
                <tr align="center">
                    <td height="33" colspan="5">
                        <input type="button" value="确定" onClick="submit();" />
                </td>
                </tr>
        </table>
    </html:form>   
    </body>
</html>
这样就会调用ListAllLeavesAction类里面的save方法

4.Struts配置文件的配置
<struts-config>
    <form-beans>
        <form-bean name="addLybForm" type="com.dragonsoft.lyb.struts.form.LybanForm" />
    </form-beans>

    <action-mappings>
    <action attribute="addLybForm" parameter="method" input="/jsp/addLyban.jsp"  name="addLybForm" path="/jsp/lyb/manageLyban" scope="request" type="com.dragonsoft.lyb.struts.action.ListAllLeavesAction">
        <forward name="success" path="/jsp/lyb/manageLyban.do?method=list"/>
        <forward name="list" path="/jsp/listAllLeaves.jsp" />
    </action>
    </action-mappings>
</struts-config>
注意的是parameter的值要跟页面隐藏域method的名字一样

这是一个简单的例子,想必大家也看的懂,我就不需要再罗嗦了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值