当表单类的属性个数和类型不能确定的时候, 就可以使用动态表单来解决
普通表单和动态表单的区别:
1. 普通表单ActionForm, 首先要定义一个这样的类, 然后配置到struts-config.xml中
2. 动态表单完全依赖于反射机制, 所以不需要创建表单类, 直接在struts-config.xml中配置即可.
项目结构:
index.jsp:
web.xml:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
action
org.apache.struts.action.ActionServlet
config
/WEB-INF/struts-config.xml
action
*.do
struts-config.xml:
/p>
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
parameter="flag" type="com.itcast.struts1.action.RegisterAction">
register.jsp:
用户注册
u:
p:
e:
t:
RegisterAction.java:
public class RegisterAction extends DispatchAction {
public ActionForward register(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
DynaActionForm dynaActionForm = (DynaActionForm) form;
String username = (String) dynaActionForm.get("username");
String password = (String) dynaActionForm.get("password");
String email = (String) dynaActionForm.get("email");
// 动态表单的意思是可以在这里动态添加属性
String telephone = (String) dynaActionForm.get("telephone");
System.out.println("username: " + username + " | "
+ "password: " + password + " | "
+ "email: " + email
+ "telephone: " + telephone);
return mapping.findForward("registerOk");
}
}
registerOk.jsp:
恭喜您! 注册成功!
测试: