LookupDispatchAction是用来解决一个表单多个提交的情况。下面通过一个简单的运算器实例,来介绍下它的使用。
1.配置文件(这是为了实现国际化的功能,本例只实现了中文和英文国际化):
AppResources.properties :
1.配置文件(这是为了实现国际化的功能,本例只实现了中文和英文国际化):
AppResources.properties :
btn.add=add
btn.subtract=subtract
btn.subtract=subtract
AppResources_zh.properties:(要使用Native2ASCII转化,本例为了方便就使用汉语拼音代替了)
btn.add=jia
btn.subtract=jian
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>
< 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 >
< 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" ) ;
}
}
@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 >
< h2 >ret= ${ret} </h2>
< c:if >