在上节中用DispatchAction类实现Action类方法的动态分配时要求方法名必须与From表单的按钮值value取值相同,这样就导致展现给用户的按钮值被紧紧耦合。其实可以让Action类继承LookUpDispatchAction类实现按钮取值的多样化。
新建一个UserForm类并让其继承ActionFrom,代码如下所示:
package com.sinosoft.form; import org.apache.struts.action.ActionForm; public class UserForm extends ActionForm { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
新建一个Action类UserAction1并让其继承LookUpDispatchAction类,代码如下所示:
package com.sinosoft.action; import java.util.HashMap; import java.util.Map; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; importorg.apache.struts.action.ActionMapping; importorg.apache.struts.actions.LookupDispatchAction; import com.sinosoft.form.UserForm; public class UserAction1 extendsLookupDispatchAction { @Override protectedMap getKeyMethodMap() { //TODO Auto-generated method stub Mapmap=new HashMap(); map.put("userForm.add","add"); map.put("userForm.update","update"); map.put("userForm.delete","delete"); returnmap; } publicActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequestrequest,HttpServletResponse response){ UserFormuserForm =(UserForm) form; userForm.setMessage("你调用了add方法..."); returnmapping.findForward("success"); } publicActionForward update(ActionMapping mapping,ActionForm form,HttpServletRequestrequest,HttpServletResponse response){ UserFormuserForm =(UserForm) form; userForm.setMessage("你调用了update方法..."); returnmapping.findForward("success"); } publicActionForward delete(ActionMapping mapping,ActionForm form,HttpServletRequestrequest,HttpServletResponse response){ UserFormuserForm =(UserForm) form; userForm.setMessage("你调用了delete方法..."); returnmapping.findForward("success"); } }
在这个类中,可以看出add,update,delete方法与execute方法类似,有着一样的参数和返回类型。只是继承LookUpDispatchAction类不必要提供execute方法,但是必须实现getKeyMethodMap()方法,这个方法返回一个Map值,这个Map中的keys值将会映射到Struts的配置文件ApplicationResource.properties中(注意:这个配置文件放在工程的src目录下),这个配置文件这样配置:
userForm.add=增加 userForm.update=更新 userForm.delete=删除
其中“增加”,“更新”,“删除”为form表单中对应按钮的value值。
新建一个jsp页面LookUpDispatchActionDemo.jsp.代码如下所示:
<%@ page language="java"contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="html"uri="http://struts.apache.org/tags-html"%> <%@ taglib prefix="bean"uri="http://struts.apache.org/tags-bean"%> <!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <title>使用LookUpDispathAction类</title> <script type="text/javascript"> function submitForm(){ document.forms[0].action="userAction1.do?method=增加"; document.forms[0].submit(); } </script> </head> <body> <html:form action="/5/userAction1"> <table> <tr> <td><bean:write name="userForm"property="message"></bean:write></td> </tr> <tr> <html:submit value="增加" onclick="submitForm()"></html:submit> </tr> <tr> <html:submit value="更新" property="method" ></html:submit> </tr> <tr> <html:submit property="method">删除</html:submit> </tr> </table> </html:form> </body> </html>
接着在配置文件中配置,如下所示:
<form-beans> <form-bean name="userForm"type="com.sinosoft.form.UserForm"></form-bean> </form-beans> <action-mappings> <action path="/5/userAction1"scope="session" parameter="method"input="/5/LookUpDispatchActionDemo.jsp" name="userForm" type="com.sinosoft.action.UserAction1"> <forward name="success"path="/5/LookUpDispatchActionDemo.jsp"></forward> </action> </action-mappings>
好了,打开服务器并部署项目,运行LookUpDispatchAction.jsp页面,如下所示:
当我们点击“增加”按钮时,页面如下所示:
当我们点击“更新”、“删除”按钮时页面如下所示:
可以看出,此时页面按钮的value可以不与Action中的方法名相同。
这是本人学习的结果,欢迎转载,欢迎交流,但转载务必给出本文章的链接地址:http://blog.csdn.net/youqishini/article/details/7341094,谢谢~