Struts ist ein sehr flexibles und leistungsfähiges Open-Source-Framework für Thin-Client-Java-Webanwendungen zur Standardisierung der Verarbeitung von Requests.
Im ersten Teil "Struts: Erste Schritte" finden Sie grundlegende Informationen und es wird ein sehr einfaches Beispiel mit manueller Validierung vorgestellt (per 'validate()' in 'ActionForm').
In diesem zweiten Teil wird ein einfaches Beispiel mit dynamisch erzeugter Daten-JavaBean (DynaBean) und deklarativer Validierung (StrutsValidator) vorgestellt (per 'DynaValidatorForm' und 'validation.xml').
... siehe erster Teil.
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <logic:redirect forward="startForwardName" />
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <html:html locale="true"> <head> <title>MyPage01.jsp: <bean:message key="Common.title"/></title> <html:base/> </head> <body bgcolor="white"> <logic:notPresent name="org.apache.struts.action.MESSAGE" scope="application"> <font color="red">ERROR: Application resources not loaded</font> </logic:notPresent> <h3><bean:message key="MyPage01.heading"/></h3> <p> <bean:message key="MyPage01.message"/></p> <html:form action="/htmlForm01ActionPath" focus="username"> <p> <bean:message key="Common.Name"/>: <html:text property="username" size="40" maxlength="40"/><br/> <font color=red><html:errors property="username"/></font></p> <p> <bean:message key="Common.Password"/>: <html:text property="password" size="40" maxlength="40"/><br/> <font color=red><html:errors property="password"/></font></p> <p> <html:submit property="button2" value="Weiter" /></p> </html:form> </body> </html:html>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <html:html locale="true"> <head> <title>MyPage02.jsp: <bean:message key="Common.title"/></title> <html:base/> </head> <body bgcolor="white"> <logic:notPresent name="org.apache.struts.action.MESSAGE" scope="application"> <font color="red">ERROR: Application resources not loaded</font> </logic:notPresent> <h3><bean:message key="MyPage02.heading"/></h3> <html:form action="/htmlForm02ActionPath" focus="date"> <p> <bean:message key="Common.Date"/>: <html:text property="date" size="40" maxlength="40"/><br/> <font color=red><html:errors property="date"/></font></p> <p> <bean:message key="Common.Age"/>: <html:text property="age" size="40" maxlength="40"/><br/> <font color=red><html:errors property="age"/></font></p> <p> <bean:message key="Common.Email"/>: <html:text property="email" size="40" maxlength="40"/><br/> <font color=red><html:errors property="email"/></font></p> <p> <bean:message key="Common.HomepageUrl"/>: <html:text property="homepageUrl" size="40" maxlength="40"/><br/> <font color=red><html:errors property="homepageUrl"/></font></p> <p> <html:cancel property="button1" value="Zurück" /> <html:submit property="button2" value="Weiter" /></p> </html:form> </body> </html:html>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <html:html locale="true"> <head> <title>MyPage03.jsp: Ergebnisanzeige</title> </head> <body> <h1>Eingegebene Daten</h1> <p> <bean:message key="Common.Name"/>: <b><bean:write property="username" name="MeinDatenFormBeanName"/></b></p> <p> <bean:message key="Common.Password"/>: <b><bean:write property="password" name="MeinDatenFormBeanName"/></b></p> <p> <bean:message key="Common.Date"/>: <b><bean:write property="date" name="MeinDatenFormBeanName"/></b></p> <p> <bean:message key="Common.Age"/>: <b><bean:write property="age" name="MeinDatenFormBeanName"/></b></p> <p> <bean:message key="Common.Email"/>: <b><bean:write property="email" name="MeinDatenFormBeanName"/></b></p> <p> <bean:message key="Common.HomepageUrl"/>: <b><bean:write property="homepageUrl" name="MeinDatenFormBeanName"/></b></p> <html:form action="/htmlForm03ActionPath"> <p> <html:submit property="button1"><bean:message key="MyPage03.back1"/></html:submit> <html:submit property="button2"><bean:message key="MyPage03.back2"/></html:submit></p> </html:form> </body> </html:html>
package meinpackage; import javax.servlet.http.*; import org.apache.struts.action.*; import org.apache.commons.beanutils.*; public final class MeineAuswerteAction extends Action { public ActionForward execute( ActionMapping mp, ActionForm fm, HttpServletRequest rq, HttpServletResponse rp ) { DynaBean dfb = (DynaBean)fm; String sButton1 = (String)dfb.get( "button1" ); String sButton2 = (String)dfb.get( "button2" ); dfb.set( "button1", null ); dfb.set( "button2", null ); String sAction = "Action2"; if( null != sButton1 && 0 < sButton1.length() ) sAction = "Action1"; return mp.findForward( sAction ); } }
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <display-name>My Struts Application</display-name> <!-- Standard Action Servlet Configuration (with debugging) --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- The Usual Welcome File List --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <form-beans> <form-bean name="MeinDatenFormBeanName" type="org.apache.struts.validator.DynaValidatorForm" dynamic="true"> <form-property name="username" type="java.lang.String" /> <form-property name="password" type="java.lang.String" /> <form-property name="date" type="java.lang.String" /> <form-property name="age" type="java.lang.String" /> <form-property name="email" type="java.lang.String" /> <form-property name="homepageUrl" type="java.lang.String" /> <form-property name="button1" type="java.lang.String" /> <form-property name="button2" type="java.lang.String" /> </form-bean> </form-beans> <global-forwards> <forward name="startForwardName" path="/StartActionPath.do" /> </global-forwards> <action-mappings> <action path="/StartActionPath" forward="/pages/MyPage01.jsp" /> <action path="/htmlForm01ActionPath" type="meinpackage.MeineAuswerteAction" name="MeinDatenFormBeanName" scope="session" validate="true" input="/pages/MyPage01.jsp"> <forward name="Action2" path="/pages/MyPage02.jsp" /> </action> <action path="/htmlForm02ActionPath" type="meinpackage.MeineAuswerteAction" name="MeinDatenFormBeanName" scope="session" validate="true" input="/pages/MyPage02.jsp"> <forward name="Action1" path="/StartActionPath.do" /> <forward name="Action2" path="/ErgebnisanzeigeActionPath.do" /> </action> <action path="/ErgebnisanzeigeActionPath" forward="/pages/MyPage03.jsp" /> <action path="/htmlForm03ActionPath" type="meinpackage.MeineAuswerteAction" name="MeinDatenFormBeanName" scope="session" validate="true" input="/pages/MyPage03.jsp"> <forward name="Action1" path="/StartActionPath.do" /> <forward name="Action2" path="/pages/MyPage02.jsp" /> </action> </action-mappings> <message-resources parameter="resources.MessageResources" /> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml" /> </plug-in> </struts-config>
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN" "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd"> <form-validation> <formset> <form name="MeinDatenFormBeanName"> <field property="username" depends="required"> <arg0 key="Common.Name" /> </field> <field property="password" depends="required,minlength,mask"> <arg0 key="Common.Password" /> <arg1 key="${var:minlength}" resource="false" /> <var> <var-name>minlength</var-name> <var-value>3</var-value> </var> <var> <var-name>mask</var-name> <var-value>^[0-9a-zA-Z]*$</var-value> </var> </field> <field property="date" depends="date"> <arg0 key="Common.Date" /> </field> <field property="age" depends="short,intRange"> <arg0 key="Common.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>0</var-value> </var> <var> <var-name>max</var-name> <var-value>199</var-value> </var> </field> <field property="email" depends="email"> <arg0 key="Common.Email" /> </field> <field property="homepageUrl" depends="url"> <arg0 key="Common.HomepageUrl" /> </field> </form> </formset> </form-validation>
# -- standard errors -- errors.header=<UL> errors.prefix=<LI> errors.suffix=</LI> errors.footer=</UL> # -- validator -- errors.invalid='{0}' is invalid. errors.maxlength='{0}' can not be greater than {1} characters. errors.minlength='{0}' can not be less than {1} characters. errors.range='{0}' is not in the range {1} through {2}. errors.required='{0}' is required. errors.byte='{0}' must be an byte. errors.date='{0}' is not a date. errors.double='{0}' must be an double. errors.float='{0}' must be an float. errors.integer='{0}' must be an integer. errors.long='{0}' must be an long. errors.short='{0}' must be an short. errors.creditcard='{0}' is not a valid credit card number. errors.email='{0}' is an invalid e-mail address. errors.url='{0}' is an invalid url (web address). # -- other -- errors.cancel=Operation cancelled. errors.detail='{0}' errors.general=The process did not complete. Details should follow. errors.token=Request could not be completed. Operation is not in sequence. # -- Common -- Common.title=My Struts Application Common.Name=Name Common.Password=Password Common.Date=Date Common.Age=Age Common.Email=Email Common.HomepageUrl=Homepage URL # -- MyPage01.jsp -- MyPage01.heading=Welcome! MyPage01.message=Second small Struts Application. \ Reads some information and displays them. \ With invalid input an error message appears. # -- MyPage02.jsp -- MyPage02.heading=Please fill also these entry fields: # -- MyPage03.jsp -- MyPage03.back1=Back to formular page 1 MyPage03.back2=Back to formular page 2
# -- standard errors -- errors.header=<UL> errors.prefix=<LI> errors.suffix=</LI> errors.footer=</UL> # -- validator -- errors.invalid='{0}' ist ungültig. errors.maxlength='{0}' kann nicht länger als {1} Zeichen sein. errors.minlength='{0}' kann nicht kürzer als {1} Zeichen sein. errors.range='{0}' ist nicht im Bereich {1} bis {2}. errors.required='{0}' ist erforderlich. errors.byte='{0}' muss ein Byte sein. errors.date='{0}' ist kein Datum (z.B. 31.12.2004). errors.double='{0}' muss ein Double (Fließkommazahl) sein. errors.float='{0}' muss ein Float (Fließkommazahl) sein. errors.integer='{0}' muss eine Integer-Zahl (ganze Zahl) sein. errors.long='{0}' muss eine Integer-Long-Zahl (ganze Zahl) sein. errors.short='{0}' muss eine Integer-Short-Zahl (ganze Zahl) sein. errors.creditcard='{0}' ist keine gültige Kreditkartennummer. errors.email='{0}' ist eine ungültige E-Mail-Adresse (z.B. abc@xy.de). errors.url='{0}' ist eine ungültige URL (Webadresse, z.B. http://www.Torsten-Horn.de/index.htm). # -- other -- errors.cancel=Operation abgebrochen. errors.detail='{0}' errors.general=Der Prozess konnte nicht abgeschlossen werden. Details sollten folgen. errors.token=Der Prozess konnte nicht korrekt beendet werden. # -- Common -- Common.title=Meine Struts Applikation Common.Name=Name Common.Password=Passwort Common.Date=Datum Common.Age=Alter Common.Email=E-Mail Common.HomepageUrl=Homepage URL # -- MyPage01.jsp -- MyPage01.heading=Willkommen! MyPage01.message=Zweite kleine Struts-Anwendung. \ Liest Informationen ein und gibt sie wieder. \ Bei fehlerhafter Eingabe erscheint Fehlermeldung. # -- MyPage02.jsp -- MyPage02.heading=Bitte füllen Sie auch die weiteren Felder aus: # -- MyPage03.jsp -- MyPage03.back1=Zurück zu Formular Seite 1 MyPage03.back2=Zurück zu Formular Seite 2
... siehe hier.