Struts dynamic form application
Original connection: http://www.developer.com/java/web/Article.php/2214681
By James M. Turner
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 = Bush Firstname = george Street = 1600 Pennsylvania Avenue NW City = WashingtonState = DC Postalcode = 20500 Phone = 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 Features) With DynaActionForm, you can use the Form-Property XML tab, which allows you to define the property elements of the FormBean in struts-config.xml. In our example, Struts-Config.xml will look like this: Type = "org.apache.struts.action.dynaactionform"> form-bean> 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.adddynacustomaction Package article1; Import org.apache.struts.action. *; Import javax.servlet.http.httpservletRequest; Import javax.servlet.http.httpservletResponse; Import javax.servlet.servletException; Import java.io.ioException; Public class adddynacustomaction 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 mapping.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.dynacustomerform Package article1; 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) { ActionerroS Errors = new actionerrors (); IF (NullorBlank (String) this.get ("Lastname"))) { Errors.Add ("lastname", New Actionerror ("Article1.lastname.Missing"))); } IF (nullorblank ((string) this.get ("firstname"))) { Errors.Add ("firstname", New ActionError ("Article1.Firstname.Missing"))); } IF (NullorBlank ((String) this.get ("street"))) { Errors.Add ("street", New ActionError ("Article1.street.Missing"); } IF (NullorBlank (String) this.get ("city"))) { Errors.Add ("City", New Actionerror ("Article1.city.Missing"); } IF (nullorblank (String) this.get ("state"))) { Errors.Add ("State", New Actionerror ("Article1.State.Missing"); } IF (NullorBlank ((String) this.get ("postalcode"))) { Errors.Add ("Postalcode", New ActionError ("Article1.Postalcode.Missing")); } IF (NullorBlank ((String) this.get ("Phone"))) { Errors.Add ("Phone", New Actionerror ("Article1.Phone.Missing")); } Return Errors; } } If so, we must change struts-config.xml to use the subclass of DynaActionform, such an effect seems to return to the previous look (write DynaActionform for each table), huh, huh. . . So the recommended practice is to use the Validator Framework for Struts1.1, in this regard, in the subsequent article. About the Author: James Turner is the owner and manager of Black Bear Software, LLC, which specializes in custom Java-based e-Commerce and CRM solutions delivery He is also the author of "MySQL and JSP Web Applications:. Data-Driven Programming Using Tomcat and MySQL (ISBN: 0672323095) AND is The Co-Author of "Struts: Kick Start" (ISBN: 0672324725), Which Will Be Published in November@blackbear.com.