Our first Struts program will be a user registration program. Users will see a registration screen that contains 3 fields: user name, password, and password confirmation. Successful registration requires two passwords. If the registration is successful, the control will turn to a page, display the registration success Successful! .. If the input password is different twice, the control stream will turn to a page display failed.
This simple exercise will show the following:
l Create an HTML form;
l Get input from the HTML form;
l Handling input (business logic);
l Change the control flow based on dynamic input;
To accomplish this program, you need to build:
l an ActionForm
l an action
l struts-config.xml file
l three pages
that's it!
Create an ActionForm
ActionForm is a JavaBean extended org.apache.struts.Actionform class. This object captures the input by requesting. When the browser submits a form, it creates a parameter for the fields in each form in the request. ActionForm has a corresponding attribute for the fields in each HTML form. The ActionServlet matching the parameters in the request and the properties in the ActionForm. When the match is matched, the ActionServlet calls the setter method and enters the value in the request. In our exercise, the UserName field in the form requires a setUserName (String) method. The Password field requires SetPassword1 (String) and SetPassword2 (String) method. These methods are responsible for assembling instance variables hidden in RegisterForm JavaBean. The source code of RegisterForm is displayed in Listing 1.
Create registerAction
Action A Java class extends org.apache.struts.Action. ActionServlet is assembled, and then passes it to the action. Action is typically responsible for entering the check, access business information, and deciding which ActionForward returns to servlet.
Now, create a file, named registeraction.java, its content is the content of the code list 2:
Package app;
Import org.apache.struts.action. *;
Public class registerform eXtends actionform {
Protected string username;
Protected string password1;
Protected string password2;
Public string getUsername () {return this.username;
Public string getpassword1 () {return this.password1;};
Public string getpassword2 () {return this.password2;};
Public void setusername (string username) {this.username = usrname;
Public void setPassword1 (string password) {this.password1 = password;};
Public void setPassword2 (string password) {this.password2 = password;};
Code List 1.1 RegistrationForm
Create registerAction
Action A Java class extends org.apache.struts.Action. ActionServlet is assembled, and then passes it to the action. Action is typically responsible for entering the check, access business information, and deciding which ActionForward returns to servlet.
Now, create a file, named registeraction.java, its content is the code list 1.2:
Package app;
Import org.apache.struts.action. *;
Import javax.servlet.http. *;
Import java.io. *;
Public class registeraction extends action {
Public ActionForward Perform (ActionMapping Mapping,
Actionform Form,
HTTPSERVLETREQUEST REQ,
HttpservletResponse res) {
// 1cast the form to the registerform
Registerform rf = (registerform) form;
String username = rf.getusername ();
String password1 = rf.getpassword1 ();
String password2 = rf.getpassword2 ();
// 2appily business logic
IF (Password1.Equals (password2)) {
Try {
// 3 Return ActionForward for Success
UserDirectory.getInstance (). Setuser (username, password1);
Return mapping.findforward ("Success");
} catch (userdirectoryexception e) {
Return mapping.findforward ("failure");
}
}
// 4 Return ActionForward for Failure
Return mapping.findforward ("failure");
}
}
Code List 1.2 Registeraction.java
Although it is very simple, our registerAction has been a typical thing for Action. At 1, the input ActionForm is converted to RegisterForm. We can get the contents of UserName, Password1, and Password2. If the password matches two times, we add the user to the userDirectory 3 and return to the ActionForward corresponding to the Success. UserDirectory is a Helper class that records usernames and passwords to a standard properties file. Otherwise, return an actionforward corresponding to Failure.
When we create a struts-config file next step, we will identify an ActionForward object representative of Success and Failure.
Create a struts configuration file (struts-config.xml)
The struts-config.xml file contains the ActionServlet to process more information on the application request. To practice, we create an empty Struts-Config.xml file. What you need to do is to fill in some details. The file is stored in the
First, add / register to the Path property of
Next step
Registerform
Name attribute to
Then, add
App.Registerction
The type attribute of the
Next, under the
Success
Go to the Name property, and
/success.html
Go to the Path property. Finally, add it in another
Failure
To the Name property,
/FAILURE.HTML
Go to the Path property.
These elements will create an ActionForward object, we will use it to select the program's control flow. The
Struts-config.xml Source code See Code 3.
Xml Version = "1.0" encoding = "ISO-8859-1"?>
"- // Apache Software Foundation // DTD Struts Configuration 1.0 // En"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
form-beans>
Type = "app.register" Name = "registerform"> action> action-mappings> Struts-Config> Code list 1.3 struts-config.xml The STRUTS framework treats the struts-config.xml file as deployment descriptor. It allows us to create and change the association of actionmapping and paths without recompiling Java classes. We can also change the connection between the page without changing the JSP template. Create a page The final step is to create success.html, failure.html, and register.jsp page. The source code of 3 files is as follows, see the code list 1. 4, 1. 5, 1.6. Store in the
HEAD>
Registration succeeded!
Try Another? p>
Body>
Html>
Code List 1.4 Success HTML
HEAD>
Registration failed!
try again? p>
Body>
Html>
Code list 1.5 Failure.html
<% @ Taglib URI = "/ Web-INF / STRUTS-FORM.TLD" prefix = "form"%>
Username:
ENTER Password:
RE-ENTER Password:
form: form>
Code list 1.6 register.jsp
At this time, all work to build a simple Struts application is done.
How do it work?
When you know the browser to the address http: // localhost: 8080 / register / register.jsp, Tomcat is machined in the usual situation. Enter username and password, click on the Register submission page. The browser is in the request of the POST form. Which registered path will be sent to the container check. Then request forward to the ActionServlet and processed by registerAction. The validity of the registerAction check the input before returning to success or failure. The last servlet forwards the control to the response page based on the returned ActionForWard.
Look at the register.jsp in the code 1.6, you can see that the form is submitted to the URI / Register. But if you observe the pages submitted, it will be found to be submitted to register.do. The struts form tag is automatically plus the .do prefix. When we set up a program skeleton, we asked all requests for matching * .do to be passed to the ActionServlet. When receiving a request, the first thing ActionServlet is to find an actionMApping to match the request to the request. ActionMapping is a JavaBean created according to Struts-Config.xml file. We give XML specific files, but when runtime, Struts references is object instead of an XML document.
From the code 1.3, we can use this element to create a mapping to the Path / Register:
PATH = "/ register" Type = "app.register" Name = "registerform" INPUT = "/ register.jsp"> Then, the ActionServlet checks if there is Name property and this mapping: PATH = "/ register" Type = "app.register" Name = "registerform" INPUT = "/ register.jsp"> Here / Register mapping identifies a Form Bean through the name of RegisterForm. ActionServlet uses this A name property to find the corresponding ActionFormBean object. The type (TYPE) identified by the from bean is used to create an actionform object: Name = "registerform" TYPE = "registerform" /> form-beans> PATH = "/ register" Type = "app.register" Name = "registerform" INPUT = "/ register.jsp"> Name = "success" PATH = "/ success.html" /> Name = "failure" Path = "/ failure.html" /> action> action-mappings> Here, servlet will use the RegisterForm class: Name = "registerform" TYPE = "app.registerform" /> form-beans> Once RegisterForm is instantiated, the ActionServlet is attempting to call the RegisterForm's setter method for the input domain in the request. In the example, they are setUsername, setpassword1, and setpassword2. This parameter will be ignored if a setter method does not exist. The TYPE attribute of the ActionMapping object is an ActionServlet to instantiate the class name of the ActionForm. Here, you will use the RegisterAction object you created. The Perform method of the RegisterAction object is called and passed a reference to the registerform that is created and assembled in front: PATH = "/ register" Type = "app.register" Name = "registerform" INPUT = "/ register.jsp"> Name = "success" PATH = "/ success.html" /> Name = "failure" Path = "/ failure.html" /> action> Depending on the execution result of the Perform method, one of the two ActionForward will be returned. The FindForward () method uses a string parameter to find the Forward object that matches the Name property. The Path property is used by the ActionServlet to decide which page to complete the response: Name = "success" PATH = "/ success.html" /> Name = "failure" Path = "/ failure.html" />