Struts中LookupDispatchAction的使用

本文通过一个简单的计算器实例介绍了 Struts2 中 LookupDispatchAction 的使用方法,该组件可以处理表单中的多个提交按钮,并针对每个按钮执行不同的业务逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>


     本文转自NightWolves 51CTO博客,原文链接:http://blog.51cto.com/yangfei520/230886,如需转载请自行联系原作者


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值