下面是如何利用反射机制将所有form类的方法都由一个action处理。
1.refTest.jsp的内容如下:
<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<script language="JavaScript1.2" type="text/javascript">
<!--
//查询
function selectList()
{
alert("调试弹出框");
<%-- retInfCtrl 必须和struts-config.xml中的name属性一样
Operate.value对应的方法名必须和form中对应,且方法类型为public。
因为mathod.invoke对应的方法必须是公共类型的!
--%>
retInfCtrl.operate.value="selectOne";
alert("隐藏域的值是 "+document.forms[0].operate.value) ;
retInfCtrl.submit();
}
</script>
<html:html>
<body>
<html:form method="post" action="/applyMng" onsubmit="return false" >
<%-- 对于form表单,都是通过input把对象输入进去的!所以对于想在其他类中得到该参数的话,必须将其加入到form中,又不想让它显示到页面上,那么只能用hidden属性--%>
<input type="hidden" name="operate">
文本框:
<tr>
<td>
<%--readonly为只读,即文本框中到内容不能更改!--%>
<input type="text" name="name" value="请输入姓名" size="20" readonly/>
</td>
</tr>
<tr>
<td>
<input type="button" class="button1" value="查询" onclick="selectList();">
<%--因为form表单中onsubmit属性设置为 return false,所以submit提交不起作用。 --%>
<input type="submit" class="button2" value="提交" >
</td>
</tr><P>
</html:form>
</body>
</html:html>
2.ReflectTestForm 内容如下:
package reflectTest;
import org.apache.struts.action.ActionForm;
publicclass ReflectTestForm extends ActionForm {
public ReflectTestForm(){
System.out.println("servlet已加载初始化");
}
public void selectInArriveList(){
System.out.println("程序进入Form中.............");
}
publicstaticvoid selectOne(){
System.out.println("程序也执行到selectOne方法中了!");
}
}
3.ReflectTestAction 测试内容如下:
package reflectTest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.TransactionManager;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
publicclass ReflectTestAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm ctrl, HttpServletRequest request, HttpServletResponse response)
throws Exception{
// 根据参数operate取得control中的对应方法。
Method method = null;
String methodName = "";
System.out.println("程序到Action中");
String operate = request.getParameter("operate");
// operate的参数中不能存在?号
String nodeId = "";
//如果不存在则返回-1!
int temp = operate.indexOf("?");
if (temp < 0)
{
methodName = operate;
}
else
{
methodName = operate.substring(0, temp);
String nodeIdPara = operate.substring(temp + 1, operate.length());
if (nodeIdPara != null && nodeIdPara.length() > 0)
{
int nIndex = nodeIdPara.indexOf("=");
if (nIndex > 0)
{
nodeId = nodeIdPara.substring(nIndex + 1, nodeIdPara.length());
}
}
}
if (methodName != null && !"".equals(methodName))
{
try
{
// 初始化事务状态
// TransactionManager.setState(TransactionState.TX_NEW);
method = ctrl.getClass().getMethod(methodName, null);
System.out.println(method);
//method输出的结果public static void reflectTest.ReflectTestForm.selectOne()
System.out.println("程序先执行到reflect中");
//invoke对带有指定参数的指定对象调用由此 Method 对象表示的基础方法。
method.invoke(ctrl, null);
System.out.println("程序执行完"+methodName+"中的方法了!");
}
catch (InvocationTargetException ite)
{
throw (Exception) ite.getTargetException();
}
return mapping.findForward("ok");
}
}
4.strust- config.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="retInfCtrl" type="reflectTest.ReflectTestForm"/>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/applyMng"
type="reflectTest.ReflectTestAction"
name="retInfCtrl"
scope="request"
parameter="operate"
validate="false">
<forward name="ok" path="/index.jsp"/>
</action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>