LookupDispatchAction
public abstract class LookupDispatchAction extends DispatchAction
通过这个Action抽象类继承DispatchAction,它的相应方法的执行由 ActionMapping中parameter属性决定。它适合在一个form中有很多按钮,按不同的按钮则执行不同的操作。
1. strurts-config.xml 中相关的信息
......
<action path= "/lookup-submit"
type= "org.apache.struts.webapp.dispatch.LookupDispatchExampleAction"
parameter= "dispatchParam"
name= "testForm"
scope= "request">
<exception key= "dispatch.ServletException"
type= "javax.servlet.ServletException"
path= "/lookup.jsp"/>
<forward name= "success" path= "/lookup.jsp"/>
</action>
.....
<action path= "/lookup-submit"
type= "org.apache.struts.webapp.dispatch.LookupDispatchExampleAction"
parameter= "dispatchParam"
name= "testForm"
scope= "request">
<exception key= "dispatch.ServletException"
type= "javax.servlet.ServletException"
path= "/lookup.jsp"/>
<forward name= "success" path= "/lookup.jsp"/>
</action>
.....
这里要注意
parameter="dispatchParam"
参数。这需要和JSP页面中提交按钮的value值相对应。
2.JSP页面
<html:form action=
"lookup-submit" style=
"display:inline">
<html:submit property= "dispatchParam"><bean:message key= "button.foo.label" /></html:submit>
<html:submit property= "dispatchParam"><bean:message key= "button.bar.label" /></html:submit>
<html:submit property= "dispatchParam"><bean:message key= "button.invalid.label" /></html:submit>
<html:submit property= "wrongParam"><bean:message key= "parameter.wrong.label" /></html:submit>
</html:form>
<html:form action= "lookup-noparam" style= "display:inline">
<html:submit property= "dispatchParam"><bean:message key= "parameter.missing.label"/></html:submit>
</html:form>
<html:submit property= "dispatchParam"><bean:message key= "button.foo.label" /></html:submit>
<html:submit property= "dispatchParam"><bean:message key= "button.bar.label" /></html:submit>
<html:submit property= "dispatchParam"><bean:message key= "button.invalid.label" /></html:submit>
<html:submit property= "wrongParam"><bean:message key= "parameter.wrong.label" /></html:submit>
</html:form>
<html:form action= "lookup-noparam" style= "display:inline">
<html:submit property= "dispatchParam"><bean:message key= "parameter.missing.label"/></html:submit>
</html:form>
<html:submit property="dispatchParam"><bean:message key="button.foo.label" /></html:submit>。
这里的property="dispatchParam" 和前面配置文件中的参数名称一致。
注意在这里的<bean:message/>key中的取值,和Action 中map的值一致。也就是,根据这里的message 标签中key的值,能在Action的map中找到相关的记录。
3.Action 的代码。
Action 继承了LookupDispatchAction,所以要实现LookupDispatchAction中的抽象方法getKeyMethodMap()方法。代码如下,由于逻辑代码和DispatcherAction 完全一样,故省略。
private Map keyMethodMap =
new HashMap();
/**
* Constructor - populate the key method map.
*/
public LookupDispatchExampleAction() {
keyMethodMap.put( "button.foo.label", "doFoo");
keyMethodMap.put( "button.bar.label", "doBar");
}
protected Map getKeyMethodMap() {
return keyMethodMap;
}
/**
* Constructor - populate the key method map.
*/
public LookupDispatchExampleAction() {
keyMethodMap.put( "button.foo.label", "doFoo");
keyMethodMap.put( "button.bar.label", "doBar");
}
protected Map getKeyMethodMap() {
return keyMethodMap;
}
注意: keyMethodMap.put("button.foo.label", "doFoo");
在往填充map时,button.foo.label.和在jsp中message标签的 key值相对应。
在往填充map时,button.foo.label.和在jsp中message标签的 key值相对应。
运行结果分析:
1.如果在jsp 按钮中,按钮包含的message标签的key值,在Action的map中找不到相应的记录,会报
ServletException: Action[/lookup-submit] missing resource in key method map
2.如果在<html:submit> 标签的property 是错误的取值(和struts-config.xml中parameter的值不同),报
ServletException: Request[/lookup-submit] does not contain handler parameter named 'dispatchParam'. This may be caused by whitespace in the label text
3.如果在struts-config.xml 中,忘记配置action的parameter参数,报
ServletException: DispatchMapping[/lookup-noparam] does not define a handler property
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/240826,如需转载请自行联系原作者