1. How to create dynamic forms in struts (1) Step 1: create a simple registration page:
<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 = "registered user"/> </form> </body>
(2) Step 2: configure the dynamic form in the struts-config.xml file:
<? 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> <! -- Dynamic form creation is configured, not defined --> <! -- The type here is dynaactionform under the struts-core-1.3.8.jar in struts 1.3 libraries. class path --> <form-bean name = "userform" type = "org. apache. struts. action. dynaactionform "> <! -- The name here is the attribute in the form --> <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-limits/> <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-mappings> <message-resources parameter =" com. LC. struts. applicationresources "/> </Struts-config>
(3) create an action to process the business logic: registeraction: 1. Create an action
Package COM. LC. struts. action; // export the package to public class registeraction extends dispatchaction {public actionforward register (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) {dynaactionform userform = (dynaactionform) Form; /** Method for retrieving data from a dynamic form */string name = userform. get ("name "). tostring (); string Password = userform. get ("password "). tostring (); system. out. println (name + "" + password + ""); Return Mapping. findforward ("registerok"); // page returned after successful completion }}
(4) The struts view is as follows:
Create a dynamic struts form