LookupDispatchAction的作用:
将多个响应用户请求的Action方法放置在同一个Action中,也就是 一个表单具有多个提交按钮,每个提交按钮要提交给不同的Action处理方法的情况。
一。配置系统的资源文件 ApplicationResources.properties
button.add= add Button
button.delete = delete Button
‘ ’表示在 jsp页面中 控件上显示的名字。
二。创建jsp
<body>
This a struts page. <br>
<html:form action="/mapping">
<html:submit property="method">
<bean:message key="button.add"></bean:message>
</html:submit>
<html:submit property="method">
<bean:message key="button.delete"></bean:message>
</html:submit>
</html:form>
</body>
此处有两个submint按钮,他们分别叫“add Button”,“delete Button”(资源文件中配置的原因)。
三。创建MyAction,一定要继承LookupDispatchAction,并且不要重写execute方法(此类里不要出现execute方法)
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out.println("add");
return null;
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out.println("delete");
return null;
}
@Override
//定义资源关键字与Action方法之间的映射关系,这里的映射是通过资源文件中的关键字来进行的
protected Map getKeyMethodMap() {
Map map = new HashMap<String, String>();
map.put("button.add", "add");
map.put("button.delete", "delete");
return map;
}
四。配置struts-config.xml
<action path="/mapping"
parameter="method"
type="com.yourcompany.struts.action.MyAction"
scope="request"
name="aaForm"
>
</action>
//一定要有name参数,对应一Form,否则将报
Cannot retrieve definition for form bean null on action 错误