Upload
smita-r-s
View
215
Download
0
Embed Size (px)
Citation preview
7/28/2019 22-GuideBuidlingStrutsApp36
1/36
1
1
Step by Step Guidefor building a simple
Struts Application
In this session, we will try to build a very simple Struts applicationstep by step.
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
2/36
2
2
Sang Shin
[email protected]/j2ee
Java
Technology EvangelistSun Microsystems, Inc.
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
3/36
3
3
Sample App We aregoing to build
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
4/36
4
4
Sample App? Keld Hansen's submit application
? Things to do
Creating ActionForm object
Creating Action object
Forwarding at either success or failure throughconfiguration set in struts-config.xml file
Input validation
Internationalizaition
? You can also build it using NetBeans
The sample application we are going to use and show is Keld Hansen'ssubmit application. The source files and ant build.xml script can be foundin the handson/homework material you can download from class website.
This is a simple application but it uses most of Struts framework.
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
5/36
5
5
Steps to follow
Now let's take a look at the steps you will follow.
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
6/36
6
6
Steps
1.Create development directory structure
2.Write web.xml
3.Write struts-config.xml
4.Write ActionForm classes
5.Write Action classes
6.Create ApplicationResource.properties
7.Write JSP pages8.Write ant build script
9.Build, deploy, and test the application
So this is the list of steps. Of course, you don't exactly followthese steps in sequence. In fact, it is expected that some of thesesteps will be reiterated.
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
7/36
7
7
Step 1: Create DevelopmentDirectory Structure
The first step is to create development directory structure.
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
8/36
8
8
Development DirectoryStructure
? Same development directory structure forany typical Web application
? Ant build script should be writtenaccordingly
? If you are using NetBeans, it is taken careof by the IDE
The source directory structure is the same directory structure weused for other Web application development under Java WSDP.Of course, the build.xml script should be written accordingly.
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
9/36
9
9
Step 2: Write web.xmlDeployment Descriptor
Step 2 is to write web.xml file.
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
10/36
10
10
web.xml? Same structure as any other Web
application
ActionServlet is like any other servlet
Servlet definition and mapping of ActionServlet
? There are several Struts specific elements
? Struts tag libraries could be defined
Because Struts application is a genuine Web application, it hasto follow the same rules that any Web application has to follow.And one of them is the presence of web.xml deploymentdescriptor file.
The web.xml file should define ActionServlet, which is thecontroller piece that is provided by the Struts framework, and itsmapping with URI.
As you will see in the example web.xml file in the followingslide, there are several Struts specific initialization parameters.Also the Struts tag libraries also need to be declared.
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
11/36
11
11
Example: web.xml1
2
3
4 action
5 org.apache.struts.action.ActionServlet
6
7 config
8 /WEB-INF/struts-config.xml
9
10 ...
11
12
13 action
14 *.do
15
This is the continuation of the web.xml file. Here you see the declarations ofStruts tag libraries.
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
12/36
12
12
Step 3: Writestruts-config.xml
The next step is to write struts-config.xml
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
13/36
13
13
struts-config.xml? Identify required input forms and then define
them as elements
? Identify required Action's and then define themas elements within element
make sure same value of name attribute of is used as the value ofname attribute of element
define if you want input validation
? Decide view selection logic and specify them as element within element
(read theslide)
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
14/36
14
14
struts-config.xml:
1
23 678 9 10 12
This is section of the struts-config.xml file. Here wedefine one element. The value of the name attribute ofthe element is the same as the value of name attribute of element in the following slide.
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
15/36
15
15
struts-config.xml:
12 3 45 11 12 13 1415
This is an example of element.
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
16/36
16
16
Step 4: WriteActionForm classes
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
17/36
17
17
ActionForm Class? Extend org.apache.struts.action.ActionForm
class
? Decide set of properties that reflect the inputform
? Write getter and setter methods for eachproperty
? Write validate() method if input validation is
desired
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
18/36
18
18
Write ActionForm class1 package submit;23 import javax.servlet.http.HttpServletRequest;
4 import org.apache.struts.action.*;56 public final class SubmitForm extends ActionForm {78 /* Last Name */9 private String lastName = "Hansen"; // default value10 public String getLastName() {11 return (this.lastName);12 }13 public void setLastName(String lastName) {14 this.lastName = lastName;15 }1617 /* Address */18 private String address = null;19 public String getAddress() {20 return (this.address);21 }22 public void setAddress(String address) {
23 this.address = address;24 }25 ...
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
19/36
19
19
Write validate() method1 public final class SubmitForm extends ActionForm {23 ...4 public ActionErrors validate(ActionMapping mapping,5 HttpServletRequest request) {67 ...89 // Check for mandatory data10 ActionErrors errors = new ActionErrors();11 if (lastName == null || lastName.equals("")) {12 errors.add("Last Name", new ActionError("error.lastName"));13 }14 if (address == null || address.equals("")) {15 errors.add("Address", new ActionError("error.address"));16 }17 if (sex == null || sex.equals("")) {18 errors.add("Sex", new ActionError("error.sex"));19 }20 if (age == null || age.equals("")) {21 errors.add("Age", new ActionError("error.age"));
22 }23 return errors;24 }
When this LogonForm is associated with a
controller, it will be passed to the controller
whenever its service is requested by theuser.
JSP pages acting as the view for thisLogonForm are automatically updated by
Struts with the current values of the
UserName and Password properties.If the user changes the properties via the JSP
page, the LogonForm will automatically beupdated by Struts
But what if the user screws up and enters
invalid data? ActionForms providevalidation
Before an ActionForm object is passed to acontroller for processing, a validate
method can be implemented on the formwhich allows the form to belay processing
until the user fixes invalid input as we willsee on the next slide...
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
20/36
20
20
Step 5: WriteAction classes
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
21/36
21
21
Action Classes? Extend org.apache.struts.action.Action class
? Handle the request
Decide what kind of server-side Model objects(EJB, JDO, etc.) can be invoked
? Based on the outcome, select the next view
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
22/36
22
22
Example: Action Class1 package submit;
23 import javax.servlet.http.*;
4 import org.apache.struts.action.*;
5
6 public final class SubmitAction extends Action {
7
8 public ActionForward execute(ActionMapping mapping,
9 ActionForm form,
10 HttpServletRequest request,
11 HttpServletResponse response) {
12
13 SubmitForm f = (SubmitForm) form; // get the form bean
14 // and take the last name value
15 String lastName = f.getLastName();
16 // Translate the name to upper case
17 //and save it in the request object
18 request.setAttribute("lastName", lastName.toUpperCase());
1920 // Forward control to the specified success target
21 return (mapping.findForward("success"));
22 }
23 }
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
23/36
23
23
Step 6: CreateApplicationResource.properties
and Configure web.xmlaccordingly
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
24/36
24
24
Resource file? Create resource file for default locale
? Create resource files for other locales
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
25/36
25
25
Example:ApplicationResource.properties
1 errors.header=Validation Error(s)
2 errors.footer=
3
4 error.lastName=Enter your last name
5 error.address=Enter your address
6 error.sex=Enter your sex
7 error.age=Enter your age
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
26/36
26
26
Step 7: Write JSP pages
I
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
27/36
27
27
JSP Pages? Write one JSP page for each view
? Use Struts tags for
Handing HTML input forms
Writing out messages
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
28/36
28
28
Example: submit.jsp1
2
3
4
56
7 Submit example
8
9
10 Example Submit Page
11
12
13
14
15 Last Name:
16 Address:
17 Sex: Male
18 Female
19 Married:
20 Age:
21 0-19
22 20-4923 50-
24
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
29/36
29
29
Example: submit.jsp
1
2 Hello
3
4 young
5 6
7 old
8
9
10
11
12
13
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
30/36
30
30
Step 8: Write Ant BuildScript
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
31/36
31
31
Write Ant Script? For building and running the application
? If you use NetBeans, this step is not necessary
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
32/36
32
32
Step 9: Build, Deploy,and Test Application
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
33/36
33
33
Accessing Web Application
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
34/36
34
34
Accessing Web Application
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
35/36
35
35
Accessing Web Application
2/30/2006
7/28/2019 22-GuideBuidlingStrutsApp36
36/36
36
36
Live your lifewith Passion!
2/30/2006