LookupDispatchAction类

 

LookupDispatchAction
 
       它是DispatchAction类的子类,也可以定义多种业务方法,它是利用请求参数值,反向查询资源并将它与类中的一种方法匹配。通常用于有多个同名提交按钮的表单的场合。它的处理更加简单。
 
该 Action也可包含多个处理方法,它可让处理方法与按钮直接关联,无须使用任何的脚本。使用LookupDispatchAction时,提交按钮必须使用Struts 的html 标签.
 
LookupDispatchAction 类使用:
1 必须用struts的html标签
2 必须实现LookupDispatchAction类的 protected Map getKeyMethodMap() 方法,即将这个类中的业务处理方法绑定到相关按钮上!
3 struts.config.xml parameter = "method" 要与 < html:submit property = "method" > 中的属性值相同。
 
 
    < action
      attribute = "calcForm"
      input = "/input.jsp"
      name = "calcForm"
       path = "/calc"
      scope = "request"
      parameter = "method"
      type = "cn.action.CalcAction" >
      < forward name = "input" path = "/input.jsp" ></ forward >
      < forward name = "result" path = "/result.jsp" ></ forward >
      </ action >
 
 
 
这段配置代码表明 :该action根据method 参数来区分请求分别调用哪个方法,此时无须使用method 的隐藏域,而是将按钮的property 设为method。通过这种方式可以避免书写javascript脚本。
 
例子:用LookupDispatchAction实现四则运算
Input.jsp 代码:
<%@ page language = "java" pageEncoding = "gb18030" %>
<%@ taglib uri = "http://jakarta.apache.org/struts/tags-bean" prefix = "bean" %>
<%@ taglib uri = "http://jakarta.apache.org/struts/tags-html" prefix = "html" %>
 
< html >
    < head >
        < title > LookupDispatchAction </ title >
    </ head >
    < body >
        < html:form action = "calc" method = "post" >
            第一个数 : < html:text property = "num1" />< html:errors property = "error1" />< br />
            第二个数 : < html:text property = "num2" />< html:errors property = "error2" />< br />
            < html:submit property = "method" >< bean:message key = "operate.add" /></ html:submit >
            < html:submit property = "method" >< bean:message key = "operate.subtract" /></ html:submit >
            < html:submit property = "method" >< bean:message key = "operate.multiply" /></ html:submit >
            < html:submit property = "method" >< bean:message key = "operate.divide" /></ html:submit >
            < html:reset property = "reset" >< bean:message key = "operate.clear" /></ html:reset >
        </ html:form >
    </ body >
</ html >
 
CalcAction.java 代码:
package cn.action;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.LookupDispatchAction;
import cn.form.CalcForm;
import java.util.HashMap;
import java.util.Map;
/**
  * MyEclipse Struts
  * Creation date: 03 - 23 - 2008
  *
  * XDoclet definition:
  * @struts .action path="/calc" name="calcForm" input="/input.jsp" scope="request" validate="true"
  */
public class CalcAction extends LookupDispatchAction {
    // 实现getKeyMethodMap()方法
    // 从map对象中查找与按钮名称对应的方法
    // 将方法绑定到按钮上
    protected Map getKeyMethodMap(){
        Map map= new HashMap();
        map.put( "operate.add" , "doAdd" );
        map.put( "operate.subtract" , "doSubtract" );
        map.put( "operate.multiply" , "doMultiply" );
        map.put( "operate.divide" , "doDivide" );
        return map;
    }
   
    private int num1 =0;
    private int num2 =0;
   
    //
    public ActionForward doAdd(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        CalcForm calcForm = (CalcForm) form; // TODO Auto-generated method stub
        //ActionMessages errors=new ActionMessages();
        HttpSession session=request.getSession();
        session=request.getSession();
        num1 =Integer.parseInt(calcForm.getNum1());
        num2 =Integer.parseInt(calcForm.getNum2());
        session.setAttribute( "result" , Integer.valueOf(( num1 + num2 )));
        return mapping.findForward( "result" );
    }
   
    //
    public ActionForward doSubtract(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        CalcForm calcForm = (CalcForm) form; // TODO Auto-generated method stub
        HttpSession session=request.getSession();
        session=request.getSession();
        num1 =Integer.parseInt(calcForm.getNum1());
        num2 =Integer.parseInt(calcForm.getNum2());
        session.setAttribute( "result" , Integer.valueOf(( num1 - num2 )));
        return mapping.findForward( "result" );
    }
   
    //
    public ActionForward doMultiply(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        CalcForm calcForm = (CalcForm) form; // TODO Auto-generated method stub
        HttpSession session=request.getSession();
        session=request.getSession();
        num1 =Integer.parseInt(calcForm.getNum1());
        num2 =Integer.parseInt(calcForm.getNum2());
        session.setAttribute( "result" , Integer.valueOf(( num1 * num2 )));
        return mapping.findForward( "result" );
    }
   
    //
    public ActionForward doDivide(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        CalcForm calcForm = (CalcForm) form; // TODO Auto-generated method stub
        ActionMessages errors=calcForm.validate(mapping, request);
        if (Integer.parseInt(calcForm.getNum2())==0){
            errors.add( "error2" , new ActionMessage( "errors.validate.zero" ));
            super .saveErrors(request, errors); // 调用ActionForm方法保存这个错误信息
            return mapping.findForward( "input" );
        }
        HttpSession session=request.getSession();
        session=request.getSession();
        num1 =Integer.parseInt(calcForm.getNum1());
        num2 =Integer.parseInt(calcForm.getNum2());
        session.setAttribute( "result" , Integer.valueOf(( num1 / num2 )));
        return mapping.findForward( "result" );
    }
}
 
 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值