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 >


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


相关文章
|
Java 网络架构
SpringMVC@PathVariable
SpringMVC@PathVariable
80 0
|
设计模式 开发框架 前端开发
SpringMVC源码解析DispatcherServlet#doDispatch方法流程(上)
SpringMVC源码解析DispatcherServlet#doDispatch方法流程(上)
266 0
SpringMVC源码解析DispatcherServlet#doDispatch方法流程(上)
SpringMVC源码解析DispatcherServlet#doDispatch方法流程(下)
SpringMVC源码解析DispatcherServlet#doDispatch方法流程(下)
319 0
SpringMVC源码解析DispatcherServlet#doDispatch方法流程(下)