一.在struts中如何实现动态表单的创建
(1)第一步:创建一个简单的注册页面:
<body> <form action="/DynamicForm/register.do?flag=register" method="post"> u:<input type="text" name="name"/><br/> p:<input type="password" name="password"/><br/> <input type="submit" value="注册用户" /> </form> </body>
(2)第二步:在struts-config.xml文件中配置动态表单:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <!-- 动态创建表单 表单是配置出来的 不是定义出来的--> <!-- 这里的type是在 Struts 1.3 Libraries中的struts-core-1.3.8.jar 下的DynaActionForm.class的路径--> <form-bean name="userForm" type="org.apache.struts.action.DynaActionForm"> <!-- 这里的name即是表单中的属性 --> <form-property name="name" type="java.lang.String" /> <form-property name="password" type="java.lang.String" /> <form-property name="name" type="java.lang.String" /> </form-bean> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings> <action attribute="userForm" input="/WEB-INF/register.jsp" name="userForm" parameter="flag" path="/register" scope="request" type="com.lc.struts.action.RegisterAction" cancellable="true" > <forward name="registerok" path="/WEB-INF/ok.jsp" /> </action> </action-mappings> <message-resources parameter="com.lc.struts.ApplicationResources" /> </struts-config>
(3)创建处理业务逻辑的action :RegisterAction
过程如下:
1.新建Action
package com.lc.struts.action; //导包即可 public class RegisterAction extends DispatchAction { public ActionForward register(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { DynaActionForm userForm = (DynaActionForm) form; /* * 从动态表单中取出数据的方法 */ String name = userForm.get("name").toString(); String password = userForm.get("password").toString(); System.out.println(name+""+password+""); return mapping.findForward("registerok"); //成功后返回的页面 } }
(4)struts的视图如下:
时间: 2024-11-08 15:42:07