This article explains the reference of DYNAFORMS in Struts1.1 in an instance code ?? Translator's 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.ActionErrors; 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) {ActionErrors errors = new ActionErrors (); IF (NullBLANK (Lastname)) {Errors.Add ("LastName", New Actionerror ("Article1.lastname.Missing"));}}} (Nulloblank (Firstname)) {Errors.Add ("Firstname", New Actionerror (" Article1.firstname.Missing "));} {Errors.Add (" street ", new actionerror (" article1.street.missing ");}} f (nulloblank (city) {Errors. Add ("City", New Actionerror ("Article1.city.Missing"));}} (NullorBLANK (State)) {Errors.Add ("State", New Actionerror ("Article1.State.Missing");} IF (NULLORBLANK ("Postalcode", new actionerror ("article1.postalcode.missing));}} f (Nulloblank (Phone) {Errors.Add (" Phone ", New Actionerror (" Article1.phone.missing "));} Return Errors; Private String Firstname; Private String Street; Private Stri Ng city; private string postalcode; private string phone; privat
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 write method in the upper side (such a long code [Although most of the tools can automatically generate SET and GET methods] What is it? If you want to match a formbean for each form, then a 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"%>
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.ioException;
public class AddCustomerAction 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 (" surcess ");}}
Let's take a look at the configuration of Struts-Config.xml.
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:
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 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 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) {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 "));} 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 Dynaac TIONFORM subclass, such an effect seems to return to the previous look (written for each table DYNAActionFORM), huh. . .
So the recommended practice is to use the Validator Framework for Struts1.1, in this regard, in the subsequent article.
About the Author: