The application of dynamic form of struts

Source: Internet
Author: User
Tags config mysql new features tld
Dynamic


This article illustrates the reference of dynaforms in struts1.1 by example code?? Translator note

If you've used a previous version of Struts, you'll notice that you'll need to spend a lot of time writing Actionform class files that are critical to struts (it acts as part of the "View"), which is usually structured as a bean Properties are added to a validate method (and sometimes the Reset method).

With the launch of the struts1.1 version, developers have another way to accomplish the previous task: using Dynabeans. Dynabeans dynamically generates Java Beans. This means that we can configure (usually using XML)

To generate Formbean instead of hard-coded in Formbean.

To see how Dynabeans (dynaforms in struts) works, let's look at a simple form where the fields are: Name,address,telephone, and so on, the following code is the usual notation (no use of 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 (Nullorblank (LastName)) {
Errors.add ("LastName",
New Actionerror ("article1.lastName.missing"));
}
if (Nullorblank (FirstName)) {
Errors.add ("FirstName",
New Actionerror ("article1.firstName.missing"));
}
if (Nullorblank (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 FirstName;
Private String Street;
Private String City;
Private String State;
Private String PostalCode;
Private String phone;

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 to Street;
}

public void Setstreet (String street) {
This.street = Street;
}

Public String getcity () {
return to 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;
}
}

See the way it's written (this long piece of code [although most of the tools can automatically generate set and get methods] how do you feel? Would it be a lot more painful to have one formbean for each form?? You know it's a standard JavaBean, just one more validate method, validate method to ensure that client-broken input is legitimate.

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"%>

<title>example of a standard Customer form</title>
Last Name: Name: Street Addr: City: State: Postal Code: Size= "5"/>
Telephone:

The corresponding action also has no complex business code, but prints the values passed from the client side to the console.

Article1. Addcustomeraction
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 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 ("Success");
}
}

Here's a look at the configuration of Struts-config.xml, which struts uses to connect these files together to accomplish the task.

<struts-config>
<form-beans>
<form-bean name= "CustomerForm" type= "Jdj.article1.Customer"/>
</form-beans>
<action-mappings>
<action path= "/addcustomer" type= "Article1". Addcustomeraction "
Name= "CustomerForm" scope= "Request"
input= "/addcustomer.jsp" >
<forward name= "Success" Path= "/addcustomersucceeded.jsp"
Redirect= "false"/>
</action>
</action-mappings>
<message-resources parameter= "Applicationresources"/>
<plug-in classname= "Org.apache.struts.validator.ValidatorPlugIn" >
<set-property value= "/web-inf/validator-rules.xml"
property= "Pathnames"/>
Struts-config.xml</plug-in></struts-config>
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Struts-config Public
"-//apache Software foundation//dtd Struts Configuration 1.1//en"
"Http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd" >
<struts-config>
<form-beans>
<form-bean name= "CustomerForm" type= "Article1". CustomerForm "/>
</form-beans>
<action-mappings>
<action path= "/addcustomer" type= "Article1". Addcustomeraction "
Name= "CustomerForm" scope= "Request" input= "/customer.jsp" >
<forward name= "Success" Path= "/addcustomersucceeded.jsp"
Redirect= "false"/>
</action>
</action-mappings>
<message-resources parameter= "Applicationresources"/>
<plug-in classname= "Org.apache.struts.validator.ValidatorPlugIn" >
<set-property value= "/web-inf/validator-rules.xml"
property= "Pathnames"/>
</plug-in>
</struts-config>

The top is configured, CustomerForm to refer to the Custemerform class, and the "/addcustomer" action uses customerform and triggers the article1.addcustomeraction to process the request.

Up to now, the top code is familiar with struts. However, if you apply the new features of struts1.1, you will use less code to accomplish the same function. Using Dynaforms, we should change the CustomerForm information in Struts-config.xml to use Org.apache.struts.action.DynaActionForm (to facilitate the reader to compare the differences before and after use, We will use the new class new JSP page to complete the same function.

Using Dynaactionform, you can use the Form-property XML tag, which allows you to define Formbean attribute elements in Struts-config.xml. In our case, the struts-config.xml will look like this:

<form-bean name= "Dynacustomerform"
Type= "Org.apache.struts.action.DynaActionForm" >
<form-property name= "LastName" type= "java.lang.String"/>
<form-property name= "FirstName" type= "java.lang.String"/>
<form-property type= "java.lang.String" name= "Street"/>
<form-property name= "City" type= "java.lang.String"/>
<form-property name= "state" type= "java.lang.String"/>
<form-property name= "PostalCode" type= "java.lang.String"/>
</form-bean>

The above changes have no effect on the JSP page. But you have to make a slight change to the original action: you are not passing Formbean (without a Get Set method) to execute (), so you should transform the form to dynaactionform and then use the method get ( FileName) to get the client-side data for the new action code as follows:

Article1. Adddynacustomeraction
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");
}
}

From the above code can be seen, it seems that "shielding" the actionform, but we also "lost" some other, such as: the question of the legality of the importation of legitimacy. There are two ways to restore checksums: One is to create a dynaactionform subclass and then implement the Validate () method in subclasses. The following code:

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"));
}
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 will change the struts-config.xml to use the Dynaactionform subclass, the effect seems to be back to the previous appearance (for each form to write dynaactionform), hehe ...

So the recommended approach is to use the validator Framework of the struts1.1 species, which is described in a later 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 C RM Solutions delivery. He is also the author of "MySQL and JSP Web applications:data-driven programming Using Tomcat and MySQL" (isbn:067232309 5) and is the co-author of "Struts:kick Start" (isbn:0672324725), which'll be published in November. He can is reached at turner@blackbear.com.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.