This article explains the reference of DynaForms in Struts1.1 in instance code - Translator Note
If you have used Struts Previous versions, you will notice that you need to spend a lot of time to write an actionform class file, and these types of files are very critical for Struts (it acts as part of "View"), usually it The structure is Bean Properties plus a validate method (sometimes reset method).
With the launch of the Struts1.1, the developer has another way to complete the previous tasks: use Dynabeans. Dynabeans dynamically generate Java Beans. This means that we can configure (usually using XML)
To generate FormBean instead of hard coding in FormBean.
To learn how Dynabeans (Struts is DynaForms), let's take a simple form, the fields are: Name, Address, Telephone, etc., the following code is the usual way of writing (not using DynaForms).
Article1.customerform
Package article1;
Import org.apache.struts.Action.actionform;
Import org.apache.struts.Action.Actionerro;
Import org.apache.struts.action.actionmapping;
Import org.apache.struts.Action.Actionerror;
Import javax.servlet.http.httpservletRequest;
Public class customerform extends actionform {
Protected Boolean NullorBlank (String Str) {
Return ((Str == Null) || (str.length () == 0));
}
Public ActionerRors Validate (ActionMapping Mapping,
HttpservletRequest request) {
ActionerroS Errors = new actionerrors ();
IF (NullorBlank (Lastname)) {
Errors.Add ("lastname",
New Actionerror ("Article1.lastname.Missing")));
}
IF (nullorblank (firstname)) {
Errors.Add ("firstname",
New ActionError ("Article1.Firstname.Missing")));
}
IF (Nulloblank (Street) {
Errors.Add ("street",
New ActionError ("Article1.street.Missing");
}
IF (nullorblank (city) {
Errors.Add ("City",
New Actionerror ("Article1.city.Missing"));
}
IF (nullorblank (state)) {
Errors.Add ("State",
New Actionerror ("Article1.State.Missing");
}
IF (NullorBlank (Postalcode)) {
Errors.add ("Postalcode", New Actionerror ("Article1.Postalcode.Missing");
}
IF (NullorBlank (Phone)) {
Errors.Add ("Phone",
New ActionError ("Article1.Phone.Missing"));
}
Return Errors;
}
PRIVATE STRING LASTNAME;
Private string dimstname;
PRIVATE STRING STREET;
PRIVATE STRING CITY;
PRIVATE STRING State;
PRIVATE STRING POSTALCODE;
PRIVATE STRING
Public string getlastname () {
Return Lastname;
}
Public void setlastname (String lastname) {
THIS.LASTNAME = LastName;
}
Public string getFirstName () {
Return firstname;
}
Public void setfirstname (String firstname) {
THIS.FIRSTNAME = firstName;
}
Public string getStreet () {
Return street;
}
Public void setstreet (String Street) {
THIS.STREET = STREET;
}
Public string getcity () {
Return city;
}
Public void setcity (string city) {
THIS.CITY = CITY;
}
Public string getState () {
Return State;
}
Public void setState (string state) {
THIS.STATE = State;
}
Public string getpostalcode () {
Return Postalcode;
}
Public void setPostalcode (String Postalcode) {
THIS.POSTALCODE = Postalcode;
}
Public String getphone () {
Return Phone;
}
Public void setphone (string phone) {
This.Phone = Phone;
}
}
Seeing the next way (such a long code [Although most of the tools can automatically generate set and get methods]? If you want to match a FORMBEAN for each form, you will be a more painful thing - - Translator's Note), you know that it is a standard JavaBean, just a validate method, the Validate method ensures that the Client break is legal.
The corresponding JSP page is also very simple, as follows:
Customer.jsp
<% @ Taglib Uri = "/ Web-INF / C.TLD" prefix = "c"%>
<% @ taglib prefix = "FMT" URI = "/ Web-INF / FMT.TLD"%>
<% @ Taglib Uri = "/ Web-INF / STRUTS-TILES.TLD" prefix = "tiles"%> <% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>
hEAD>
Last Name:
First Name:
Street Addr:
City:
State:
Postal code: SIZE = "5" /> Telephone: html: form> The corresponding action does not have a complex service code, just prints the values passed from the Client end to the console. Article1.addcustomerction Package article1; Import org.apache.struts.Action.action; Import org.apache.struts.action.actionmapping; Import org.apache.struts.Action.actionForward; Import org.apache.struts.Action.actionform; Import javax.servlet.http.httpservletRequest; import javax.servlet.http.httpservletResponse; Import javax.servlet.servletException; Import java.io.ioException; Public class addcustomerction extends action { Public ActionForward Execute (ActionMapping Mapping, Actionform Form, HTTPSERVLETREQUEST REQUEST, Httpservletresponse response Throws servletexception, ioException { Customerform Custform = (CUSTOMERFORM) Form; System.out.println ("Lastname =" Custform.getLastName ()); System.out.println ("firstname =" Custform.GetFirstName ()); System.out.println ("street =" Custform.getStreet ()); System.out.println ("City =" Custform.getCity ()); System.out.println ("State =" Custform.getState ()); System.out.println ("Postalcode =" Custform.getPostalcode ()); System.out.println ("Phone =" Custform.getphone ()); Return mapping.findforward ("Success"); } } Let's take a look at the configuration of Struts-Config.xml. form-beans> Name = "Customerform" Scope = "Request" INPUT = "/ addcustom.jsp"> REDIRECT = "false" /> action> action-mappings> Property = "pathnames" /> Struts-config.xml plug-in> struts-config> XML Version = "1.0" encoding = "UTF-8"?>
"- // Apache Software Foundation // DTD Struts Configuration 1.1 // En" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> form-beans> Name = "Customerform" Scope = "Request" Input = "/ Customer.JSP"> REDIRECT = "false" /> action> action-mappings> Property = "pathnames" /> plug-in> Struts-Config> By configuring, Customerform, "/ AddCustomer" Action uses CUSTEMERFORM, and triggered Article1.AddCustomraction to process requests. The following figure adds a Customer empty list picture If nothing is filled, the following figures will appear feedback information: If you have legal all your information, you can see the following information on the console: Lastname = bushfirstname = georgestreet = 1600 pennsylvania avenue nwcity = WashingtonState = DCPOSTALCODE = 20500Phone = 2024561414 Up to now, the upper code is familiar with Struts. It should be very familiar. If the new features of Struts1.1 are applied, you will use fewer code to complete the same functions described above. Using DynaForms, we should change the information of Customerform in Struts-Config.xml to use org.apache.struts.Action.DynaActionform (for easy readers to use the difference before and after use, we will use new types of JSP pages to complete the same Function) Use DynaActionform, you can use the Form-Property XML tab, which allows you to define the properties of FormBean in struts-config.xml. In our example, Struts-Config.xml will look like this: The above changes have no impact on the JSP page. However, you have to make a slight change for the original Action should be: You are now not transferring FORMBEAN (no get set method) in Execute (), so you should transform the Form to DynaActionform, then use method get (filename) to get The new Action code of the Client end data is as follows: Article1.adddynacustomactionpackage article1; Import org.apache.struts.action. *; Import javax.servlet.http.httpservletRequest; import javax.servlet.http.httpservletResponse; import javax.servlet.servletException; import java.ioException; public class AddDynaCustomerAction extends Action {public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {DynaActionForm custForm = (DynaActionForm) form; System.out.println ( "lastName =" custForm.get ( "lastname"); system.out.println ("firstname =" Custform.get ("firstname")); system.out.println ("street =" Custform.get ("street"); system. Out.println ("City =" Custform.get ("City")); System.out.Println ("State =" Custform.get ("State"); System.out.Println ("Postalcode =" Custform.get ("postalcode"); system.out.println ("Phone =" Custform.get ("Phone")); return maping.findforward ("Success");}} As can be seen from the code, it seems that "shield" Actionform, but we are also "lost" some other things, such as: strict input of legitimacy. There are two ways to recover the calibration function: First, create a subclass of DynaActionForm, then implement the validate () method in the subclass. As follows: Article1.dynacustomerformpackage articles; Import org.apache.struts.action. *; Import javax.servlet.http.httpservletRequest; Public class Dynacustomerform Extends DynaActionform { Protected Boolean NullorBlank (String Str) {Return ((Str == Null) || (Str.Length () == 0);} public ActionErrors validate (ActionMapping mapping, HttpServletRequest request) {ActionErrors errors = new ActionErrors (); if (nullOrBlank ((String) this.get ( "lastName"))) {errors.add ( "lastName", new ActionError ( "article1 .lastname.missing "));}} (NullorBlank ((String) this.get (" firstname ")) {Errors.Add (" firstname ", new actionerror (" article1.firstname.missing));} (NullorBlank ("street"))) {ErrorS.Add ("street", new actionerror ("article1.street.missing");}} (nullorblank ((String) this.get ( "City")) {Errors.Add ("City", new actionerror ("article1.city.missing"));}} (nullorblank ((string) this.get ("state")) {ErrorS.Add ("State", New Actionerror ("Article1.State.Missing")));}} (NullorBlank ((String) this.get ("Postalcode")) {Errors.Add ("Postalcode", New Actionerror ("Article1 .postalcode.missing "));}} (nullorblank ((string) this.get (" phone "))) {Errors.Add (" Phone " New Actionerror ("Article1.Phone.Missing"));} Return Errors;}} If this is the case, we must change Struts-config.xml to use the subclass of DynaActionform, which seems to be returned to the previous Sample (write DynaActionform for each table), huh, huh. . . So the recommended practice is to use the Validator Framework of Struts1.1, in this regard in the subsequent article