Struts Dynavalidatoraction Form en
Struts Dynavalidatoraction Form en
Generals
Autor: Sascha Wolski Sebastian Hennebrder http://www.laliluna.de/tutorials.html Tutorials for Struts, EJB, xdoclet und eclipse. Datum: February, 16th 2005 Development Tools Eclipse 3.x Dependencies Struts 1.1 Jboss oder Tomcat PDF download: http://www.laliluna.de/download/struts-dynavalidatoraction-form-en.pdf Source download: http://www.laliluna.de/download/struts-dynavalidatoraction-form-source.zip
The form bean can than be reused in an action definition. Below you see an example for an ActionMapping in the Struts Config. Example:
<action attribute="exampleForm" input="/form/example.jsp" name="exampleForm" path="/example" scope="request" type="my.package.ExampleAction" />
Validation of properties
The form bean DynaValidatorActionForm uses the Struts validation capabilities using validation rules defined in XML files. Struts offers a wide choice of rules, you can all find in the file validator-
rules.xml. You configure the rules for each property of a FormBean. These validations have to be written in the XML file (validation.xml) Example validation file validation.xml:
<form-validation> <formset> <!-- validation mapping fr example action --> <form name="/example"> <field property="name" depends="required, minlength"> <arg0 key="exampleForm.name" /> <arg1 key="${var:minlength}" resource="false" /> <var> <var-name>minlength</var-name> <var-value>3</var-value> </var> </field> </form> </formset> </form-validation>
Output the name and the age to the log. The complete source code is shown below.
public class ExampleAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { //ActionForm zuweisen DynaValidatorActionForm exampleForm = (DynaValidatorActionForm) //Zugriff auf Eigenschaften der BeanValidatorForm //Klasse innerhalb der Action Klasse System.out.println(exampleForm.get("name")); System.out.println(exampleForm.get("age")); } } return mapping.findForward("showExample");
form;
input defines the JSP which is shown when an error occured during the validation. In our case this is the same JSP we used to show the form. type defines the action class which is called for the action <forward ...> defines the forwards of the action. Here the JSP file are example1.jsp and example2.jsp.
<action-mappings > <action attribute="exampleForm" input="/form/example1.jsp" name="exampleForm" path="/example1" scope="request" type="de.laliluna.tutorial.dynavalidatoractionform.action.ExampleAction "> <forward name="showExample" path="/form/example1.jsp" /> </action> <action attribute="exampleForm" input="/form/example2.jsp" name="exampleForm" path="/example2" scope="request" type="de.laliluna.tutorial.dynavalidatoractionform.action.ExampleAction
">
<field property="age" depends="required, intRange, integer"> <arg0 key="exampleForm.age"/> <arg1 name="intRange" key="${var:min}" resource="false" /> <arg2 name="intRange" key="${var:max}" resource="false" /> <var> <var-name>min</var-name> <var-value>1</var-value> </var> <var> <var-name>max</var-name> <var-value>150</var-value> </var> </field> </form> <!-- validation mapping for action /example2 --> <form name="/example2"> <field property="name" depends="required, minlength"> <arg0 key="exampleForm.name" /> <arg1 key="${var:minlength}" resource="false" /> <var> <var-name>minlength</var-name> <var-value>6</var-value> </var> </field> </form> </formset> </form-validation>
exampleForm.age=Age
Open the struts-config.xml and add the following lines to configure your resource file.
<message-resources parameter="de.laliluna.tutorial.dynavalidatoractionform.ApplicationResources" />