To use dynamic form in struts, you must take the following steps:
1)Dynamic ConfigurationForm
To use a dynamic form, configure it in the struts-config.xml first
The instance code is as follows:
<Form-beans>
<Form-bean name = "voform" type = "org. Apache. Struts. Action. dynaactionform">
<Form-property name = "uname" type = "Java. Lang. String"/>
<Form-property name = "upass" type = "Java. Lang. String"/>
</Form-bean>
</Form-beans>
Dynamic form does not have a corresponding class. Therefore, if you need to extend the attributes, you only need to add an attribute home in the configuration file again.
But for actionform, if you add an attribute, you need to add an attribute in the attribute and an attribute in the class. This must be done well. Dynamic form is unnecessary.
Dynamic form is more convenient to use.
2)PageForm
Form is actually a transfer object, that is, an object that exchanges data and transmits data only in the viewer and action.
Therefore, from the form page, each attribute must correspond to the dynamic form attribute one by one.
Then, Struts maps the property values.
The instance code is as follows:
<Form method = "Post" Action = "login. Do">
Username: <input type = "text" name = "uname" value = "zhangyi"> <br>
Password: <input type = "password" name = "upass" value = "zhangyi"> <br>
<Input type = "Submit" name = "Submit" value = "login">
</Form>
The attributes in this form correspond one to one with those in the dynamic form.
3)Action
From the form on the above page, we can see that if this form is submitted, then the request is submitted to login. Do
This action.
In this action, how can we call a dynamic form? Like normal actionform, the action class and JSP can access dynamic actionform, And the access method is basically the same. The biggest difference between accessing a dynamic actionform and accessing a common actionform is that the access to attributes is different. In the standard actionform, The getter and setter methods are provided for each attribute to read and set attributes, while dynaactionform saves all attributes in a map object, therefore, accessing the attributes in dyanactionform is similar to accessing the map object:
The instance code is as follows:
Dynaactionform voform = (dynaactionform) form;
String uname = (string) voform. Get ("uname ");
String upass = (string) voform. getstring ("upass ");
// Voform. Set ("uname", "fengyan"); set the value!
System.Out. Println ("vo is:" + voform );
Because dynaactionform is passed, we need to forcibly convert form into a dynaactionform object when calling it.
4)Test Results
The execution result of the above particle is as follows:
Vo is: dynaactionform [dynaclass = voform, upass = bbbbbbbbb, uname = aaaaaaa]
That is to say, we have obtained this object.
Analysis of instantiated dynamic actionform
<Form-bean name = "logonform" type = "org. Apache. Struts. validator. dynavalidatorform">
<Form-property name = "username" type = "Java. Lang. String"/>
<Form-property name = "password" type = "Java. Lang. String"/>
<Form-property name = "forwardaction" type = "Java. Lang. String"/>
</Form-bean>
<Action Path = "/logon" name ="Logonform"Inputs ="/WEB-INF/JSP/bas/logon. jsp "Scope =" request "type =" com. Longtop. Bas. Web. logonaction "Validate =" false "/>
Formbeanconfig ----- and <form-bean> Configure the corresponding class
Protected moduleconfig;
Protected string name;
Protected string type;
Protected Boolean dynamic = false;
Protected hashmap formproperties = new hashmap (); // attributes corresponding to <form-property>.
Dynaactionformclass ---- specifically used to process dynamic classes, so that you do not need to define an actionform separately.
Protected transient static hashmap dynaclasses = new hashmap (); // static type. The variables of all dynaactionformclass instances are consistent, and the variables are saved as subclasses of dynaactionform or dynaactionform.
Protected transient class beanclass = NULL; // This variable indicates the Class Name of the dynamic actionform to be created (Org. Apache. Struts. validator. dynavalidatorform ).
Protected formbeanconfig Config = NULL; // class corresponding to <form-bean> Configuration. A <form-bean> Configuration generates a dynaactionformclass.
Protected hashmap propertiesmap = new hashmap (); // a set of attributes corresponding to <form-property>.
Protected dynaproperty properties [] = NULL; and The dynaproperty array corresponding to <form-property>.
Protected string name = NULL; // consistent with the name of <form-bean> = config. getname ().
Important methods include:
Public static dynaactionformclass createdynaactionformclass (formbeanconfig config)
First, check whether the static set dynaclasses of this class has a dynaactionformclass instance corresponding to <form-bean>. If yes, the dynaactionformclass instance is returned. If no, instantiate a dynaactionformclass class, initialize its config variable, name variable, properties array, propertiesmap set, and load config. the class corresponding to GetType () (Org. apache. struts. validator. dynavalidatorform), and finally add this instance (dynaactionformclass) to the static variable dynaclasses.
Public dynabean newinstance ()
Instantiate the beanclass (Org. Apache. Struts. validator. dynavalidatorform) loaded on dynaactionformclass, and set the dynaactionformclass = This (dynaactionformclass) and dynavalues (hashmap) attributes of beanclass.
Dynaactionform-each request must obtain or generate a <form-bean> corresponding to the address. If it is a dynamic form-bean, it must be a subclass of dynaactionform or dynaactionform.
Protected dynaactionformclass dynaclass = NULL; // instantiate the dynaactionformclass of this class
Protected hashmap dynavalues = new hashmap (); // a set of dynaproperty corresponding to <form-property>.
Important methods are:
(1) When struts is started, it loads the dynamic class dynaactionformclass during initmoduleconfig () in moduleconfig (Init () initialization.
Moduleconfig Config = .............................;
// Obtain the formbeanconfig object corresponding to the <form-bean> element configured in the struts-config.xml document if the type of the <form-bean> is dynamic actionform (type = "org. apache. struts. validator. dynavalidatorform ") to load the class.
Formbeanconfig FBS [] = config. findformbeanconfigs ();
For (INT I = 0; I <FBS. length; I ++ ){
If (FBS [I]. getdynamic ()){
Dynaactionformclass. createdynaactionformclass (FBS [I]);
}
}
(2) If the request address is/WEB-INF/JSP/bas/logon. jsp, the request process starts from the process of processing actionmapping as follows:
1. actionmapping mapping = processmapping (request, response, PATH );
Find out which of the following correspond to the requested path in the actionconfig set of moduleconfig (the actionconfig set of moduleconfig has been initialized at struts startup ). If you find this actionmapping object, add it to the request object. If no, the request is sent to the system's default actionmapping (<action> unknown = "true ").
2. actionform form = processactionform (request, response, Mapping );
If the <action> configuration does not have the name attribute or the name attribute does not have the corresponding <form-bean> element, exit. Or, you can find the formbeanconfig corresponding to the name attribute of actionmapping in the formbeanconfig set of moduleconfig. Check whether the request or session has the corresponding actionform (attribute) based on the name attribute of <action> ). If yes, the actionform instance is returned. If not, perform the next step.
3. If you determine whether the dynamic attribute of <action> is true, instantiate a dynaactionformclass.
Dynaactionformclass dynaclass = dynaactionformclass. createdynaactionformclass (config );
First, check whether the static set dynaclasses of this class has a dynaactionformclass instance corresponding to <form-bean>. If yes, the dynaactionformclass instance is returned. If no, instantiate a dynaactionformclass class, initialize its config variable, name variable, properties array, propertiesmap set, and load config. the class corresponding to GetType () (Org. apache. struts. validator. dynavalidatorform) to the beanclass variable, and finally add this instance (dynaactionformclass) to the static variable dynaclasses.
Dynaactionformclass dynaclass =
(Dynaactionformclass) dynaclasses. Get (key );
If (dynaclass = NULL ){
Dynaclass = new dynaactionformclass (config );
Dynaclasses. Put (Key, dynaclass );
}
Return (dynaclass );
4. instantiate a dynamic actionform.
Actionform instance = NULL;
Instance = (actionform) dynaclass. newinstance (); instantiate the class loaded in step 3.
Instantiate the beanclass (Org. Apache. Struts. validator. dynavalidatorform) loaded on dynaactionformclass and initialize the initial values of the dynaactionformclass and dynavalues (hashmap) Attribute Sets of beanclass.
5. initialize the initial value of the actionform property attribute.
Public void initialize (actionmapping Mapping );
<Form-property name = "username" type = "Java. Lang. String" Initial = "hh"/>
6. the following processes are the same as normal requests.