Create Struts dynamic forms and struts dynamic forms
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:
Struts dynamic forms
Write the resource file as follows:
# Resources for parameter 'com. fj. bdl. chain. bean. action. applicationresources'
# Project chainstore
# Struts Validator Error Messages
Name = <B> name </B>
Password = <B> passWord </B>
Errors. required = {0} is required.
Errors. minlength = {0} can not be less than {1} characters.
Errors. maxlength = {0} can not be greater than {1} characters.
Errors. invalid = {0} is invalid.
Errors. byte = {0} must be a byte.
Errors. short = {0} must be a short.
Errors. integer = {0} must be an integer.
Errors. long = {0} must be a long.
Errors. float = {0} must be a float.
Errors. double = {0} must be a double.
Errors. date = {0} is not a date.
Errors. range = {0} is not in the range {1} through {2 }.
Errors. creditcard = {0} is an invalid credit card number.
Errors. email = {0} is an invalid e-mail address.
Error message displayed on the JSP page:
<Html: errors property = "name"/>
<Html: errors property = "password"/>
The resource file does not seem to be encoded. I'm not sure.
I usually use internationalization to output error information about resource files in Chinese.
Struts dynamic Form Verification Failed
Exception creating bean of class
This is because the DynaValidatorFrom class is not found.
Check whether the Struts jar package is added to your project.
This is what it means.
It makes it impossible for you to create a class.