Struts中LookupDispatchAction的使用

LookupDispatchAction是用来解决一个表单多个提交的情况。下面通过一个简单的运算器实例,来介绍下它的使用。
1.配置文件(这是为了实现国际化的功能,本例只实现了中文和英文国际化):
   AppResources.properties :
btn.add=add
btn.subtract=subtract

  AppResources_zh.properties:(要使用Native2ASCII转化,本例为了方便就使用汉语拼音代替了)
btn.add=jia
btn.subtract=jian
2.输入页面: submit.jsp
     < bean:define id ="add" >    
< bean:message key ="btn.add" />
     </bean:define>                                    
     < bean:define id ="sub" >
< bean:message key ="btn.subtract" />
     </bean:define>
     < form action ="/strutsapp/math.do" method ="GET" >
        Number A : < input type ="text" name ="a" /> < br />
        Number B : < input type ="text" name ="b" /> < br />
< input type ="submit" name ="btn" value ="${add}" />
< input type ="submit" name ="btn" value ="${sub}" />
     </form>

3.配置文件: struts-config.xml
     < form-beans >    
< form-bean name ="allForm" type ="org.apache.struts.action.DynaActionForm" >
                 < form-property name ="a" type ="java.lang.Integer" />
                 < form-property name ="b" type ="java.lang.Integer" />
                                 </ form-bean >
     </ form-beans >
     < action-mappings >
                                 < action parameter ="btn" path ="/math" type ="com.kettas.struts.MathAction" name ="allForm" >
                 < forward name ="ok" path ="/ret.jsp" />
                                 </ action >
     </ action-mappings >

4.Action类: MathAction.java
 
public class MathAction extends LookupDispatchAction{
@Override
public Map getKeyMethodMap(){    
     //按钮的值应来自于资源文件.
     //在map中保存是资源文件中的key
    Map m = new HashMap();
    m.put( "btn.add" , "addOperate" );
    m.put( "btn.subtract" , "subOperate" );    
     return m ;
}
public ActionForward addOperate( ActionMapping mapping,
                     ActionForm form ,
                                                    HttpServletRequest request ,
                     HttpServletResponse response){                                        
     DynaActionForm daf = (DynaActionForm)form ;
        
     Integer a = (Integer)daf.get( "a" ) ;
     Integer b = (Integer)daf.get( "b" ) ;
        
     int ret = a.intValue() + b.intValue();
     request.setAttribute( "ret" , ret ) ;
     return mapping.findForward( "ok" ) ;
        
}
public ActionForward subOperate( ActionMapping mapping,
                    ActionForm form ,
                 HttpServletRequest request ,
                HttpServletResponse response){
    DynaActionForm daf = (DynaActionForm)form ;
        
     Integer a = (Integer)daf.get( "a" ) ;
     Integer b = (Integer)daf.get( "b" ) ;
        
     int ret = a.intValue() - b.intValue();
     request.setAttribute( "ret" , ret ) ;
     return mapping.findForward( "ok" ) ;
}
     }
5.结果页面: ret.jsp
< c:if test ="${!empty ret}" >
             < h2 >ret= ${ret} </h2>
< c:if >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值