SlideShare une entreprise Scribd logo
1  sur  34
Télécharger pour lire hors ligne
03/26/2007




                           Step by Step Guide
                          for building a simple
                           Struts Application


                                                                           1




             In this session, we will try to build a very simple Struts application
             step by step.




                                                                                 1
03/26/2007




                 Sang Shin
                 sang.shin@sun.com
              www.javapassion.com/j2ee
             Java™ Technology Evangelist
               Sun Microsystems, Inc.
                                           2




                                               2
03/26/2007




             Sample App We are
               going to build



                                 3




                                     3
03/26/2007




                  Sample App
                   ?   Keld Hansen's submit application
                   ?   Things to do
                        –   Creating ActionForm object
                        –   Creating Action object
                        –   Forwarding at either success or failure through
                            configuration set in struts-config.xml file
                        –   Input validation
                        –   Internationalizaition
                   ?   You can also build it using NetBeans

                                                                                  4




             The sample application we are going to use and show is Keld Hansen's
             submit application. The source files and ant build.xml script can be found
             in the handson/homework material you can download from class website.

             This is a simple application but it uses most of Struts framework.




                                                                                      4
03/26/2007




                           Steps to follow



                                                                   5




             Now let's take a look at the steps you will follow.




                                                                       5
03/26/2007




                     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 pages
                      8.Build, deploy, and test the application

                                                                        6




             So this is the list of steps. Of course, you don't exactly follow
             these steps in sequence. In fact, it is expected that some of these
             steps will be reiterated.




                                                                                   6
03/26/2007




                  Step 1: Create Development
                      Directory Structure


                                                                            7




             The first step is to create development directory structure.




                                                                                7
03/26/2007




                     Development Directory
                     Structure
                     ?   Same development directory structure for
                         any typical Web application
                     ?   Ant build script should be written
                         accordingly
                     ?   If you are using NetBeans, the
                         development directory structure is
                         automatically created



                                                                       8




             The source directory structure is the same directory structure we
             used for other Web application development under Java WSDP.
             Of course, the build.xml script should be written accordingly.




                                                                                 8
03/26/2007




                   Step 2: Write web.xml
                   Deployment Descriptor


                                                9




             Step 2 is to write web.xml file.




                                                    9
03/26/2007




                     web.xml
                     ?   Same structure as any other Web
                         application
                         –   ActionServlet is like any other servlet
                         –   Servlet definition and mapping of ActionServlet
                             needs to be specified in the web.xml
                     ?   There are several Struts specific
                         <init-param> elements
                         –   Location of Struts configuration file
                     ?   Struts tag libraries could be defined


                                                                               10




             Because Struts application is a genuine Web application, it has
             to follow the same rules that any Web application has to follow.
             And one of them is the presence of web.xml deployment
             descriptor file.

             The web.xml file should define ActionServlet, which is the
             controller piece that is provided by the Struts framework, and its
             mapping with URI.

             As you will see in the example web.xml file in the following
             slide, there are several Struts specific initialization parameters.
             Also the Struts tag libraries also need to be declared.




                                                                                    10
03/26/2007




                   Example: web.xml
                   1 <?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?>
                   2 <web-app version=quot;2.4quot; xmlns=quot;http://java.sun.com/xml/ns/j2eequot;
                   xmlns:xsi=quot;http://www.w3.org/2001/XMLSchema-instancequot;
                   xsi:schemaLocation=quot;http://java.sun.com/xml/ns/j2ee
                   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsdquot;>
                   3       <servlet>
                   4         <servlet-name>action</servlet-name>
                   5         <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
                   6         <init-param>
                   7             <param-name>config</param-name>
                   8             <param-value>/WEB-INF/struts-config.xml</param-value>
                   9         </init-param>
                   10        ...
                   11       </servlet>
                   12       <servlet-mapping>
                   13          <servlet-name>action</servlet-name>
                   14          <url-pattern>*.do</url-pattern>
                   15       </servlet-mapping>
                                                                                                     11



             This is the continuation of the web.xml file. Here you see the declarations of
             Struts tag libraries.




                                                                                                          11
03/26/2007




                              Step 3: Write
                            struts-config.xml


                                                           12




             The next step is to write struts-config.xml




                                                                12
03/26/2007




                      struts-config.xml
                      ?   Identify required input forms and then define
                          them as <form-bean> elements
                      ?   Identify required Action's and then define them
                          as <action> elements within <action-mappings>
                          element
                          –   make sure same value of name attribute of <form-
                              bean> is used as the value of name attribute of
                              <action> element
                          –   define if you want input validation
                      ?   Decide view selection logic and specify them as
                          <forward> element within <action> element
                                                                             13




             (read theslide)




                                                                                  13
03/26/2007




                  struts-config.xml: <form-beans>
                   1    <?xml version=quot;1.0quot; encoding=quot;UTF-8quot; ?>
                   2
                   3    <!DOCTYPE struts-config PUBLIC
                   4    quot;-//Apache Software Foundation//DTD Struts Configuration 1.2//ENquot;
                   5    quot;http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtdquot;>
                   6
                   7
                   8    <struts-config>
                   9      <form-beans>
                   10          <form-bean   name=quot;submitFormquot;
                   11                       type=quot;submit.SubmitFormquot;/>
                   12     </form-beans>




                                                                                      14



             This is <form-beans> section of the struts-config.xml file. Here we
             define one <form-bean> element. The value of the name attribute of
             the <form-bean> element is the same as the value of name attribute of
             <action> element in the following slide.




                                                                                            14
03/26/2007




                   struts-config.xml:
                   <action-mappings>
                      1
                      2 <!-- ==== Action Mapping Definitions ===============-->
                      3    <action-mappings>
                      4
                      5       <action path=quot;/submitquot;
                      6              type=quot;submit.SubmitActionquot;
                      7              name=quot;submitFormquot;
                      8              input=quot;/submit.jspquot;
                      9              scope=quot;requestquot;
                      10               validate=quot;truequot;>
                      11          <forward name=quot;successquot; path=quot;/submit.jspquot;/>
                      12          <forward name=quot;failurequot; path=quot;/submit.jspquot;/>
                      13       </action>
                      14
                      15     </action-mappings>


                                                                                  15



             This is an example of <action-mappings> element.




                                                                                       15
03/26/2007




                Step 4: Write
             ActionForm classes


                                  16




                                       16
03/26/2007




             ActionForm Class
             ?   Extend org.apache.struts.action.ActionForm
                 class
             ?   Decide set of properties that reflect the input
                 form
             ?   Write getter and setter methods for each
                 property
             ?   Write validate() method if input validation is
                 desired


                                                               17




                                                                    17
03/26/2007




             Write ActionForm class
             1    package submit;
             2
             3    import javax.servlet.http.HttpServletRequest;
             4    import org.apache.struts.action.*;
             5
             6    public final class SubmitForm extends ActionForm {
             7
             8      /* Last Name */
             9      private String lastName = quot;Hansenquot;; // default value
             10     public String getLastName() {
             11       return (this.lastName);
             12     }
             13     public void setLastName(String lastName) {
             14       this.lastName = lastName;
             15     }
             16
             17      /* 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      }                                                     18
             25    ...




                                                                                18
03/26/2007




             Write validate() method
             1    public final class SubmitForm extends ActionForm {
             2
             3    ...
             4      public ActionErrors validate(ActionMapping mapping,
             5         HttpServletRequest request) {
             6
             7      ...
             8
             9        // Check for mandatory data
             10        ActionErrors errors = new ActionErrors();
             11        if (lastName == null || lastName.equals(quot;quot;)) {
             12          errors.add(quot;Last Namequot;, new ActionError(quot;error.lastNamequot;));
             13        }
             14        if (address == null || address.equals(quot;quot;)) {
             15          errors.add(quot;Addressquot;, new ActionError(quot;error.addressquot;));
             16        }
             17        if (sex == null || sex.equals(quot;quot;)) {
             18          errors.add(quot;Sexquot;, new ActionError(quot;error.sexquot;));
             19        }
             20        if (age == null || age.equals(quot;quot;)) {
             21          errors.add(quot;Agequot;, new ActionError(quot;error.agequot;));
             22        }
             23        return errors;
              When this “LogonForm” is associated with a
             24      }                                     19
             25    ..
                  controller, it will be passed to the controller
                  whenever it’s service is requested by the
                  user.
              JSP pages acting as the view for this
                  LogonForm 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 be
                  updated by Struts
              But what if the user screws up and enters
                  invalid data? ActionForms provide
                  validation…
              Before an ActionForm object is passed to a
                  controller for processing, a “validate”
                  method can be implemented on the form
                  which allows the form to belay processing
                  until the user fixes invalid input as we will
                  see on the next slide...                                             19
03/26/2007




             Step 5: Write
             Action classes


                              20




                                   20
03/26/2007




             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




                                                                     21




                                                                          21
03/26/2007




             Example: Action Class
             1 package submit;
             2
             3 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(quot;lastNamequot;, lastName.toUpperCase());
             19
             20     // Forward control to the specified success target
             21     return (mapping.findForward(quot;successquot;));
             22   }
             23 }                                                               22




                                                                                     22
03/26/2007




                     Step 6: Create
             ApplicationResource.properties
                and Configure web.xml
                       accordingly


                                              23




                                                   23
03/26/2007




             Resource file
             ?   Create resource file for default locale
             ?   Create resource files for other locales




                                                           24




                                                                24
03/26/2007




             Example:
             ApplicationResource.properties
             1   errors.header=<h4>Validation Error(s)</h4><ul>
             2   errors.footer=</ul><hr>
             3
             4   error.lastName=<li>Enter your last name
             5   error.address=<li>Enter your address
             6   error.sex=<li>Enter your sex
             7   error.age=<li>Enter your age




                                                                  25




                                                                       25
03/26/2007




                 Step 7: Write JSP pages


                                           26




             I




                                                26
03/26/2007




             JSP Pages
             ?   Write one JSP page for each view
             ?   Use Struts tags for
                 –   Handing HTML input forms
                 –   Writing out messages




                                                    27




                                                         27
03/26/2007




             Example: submit.jsp
             1    <%@ page language=quot;javaquot; %>
             2    <%@ taglib uri=quot;/WEB-INF/struts-bean.tldquot; prefix=quot;beanquot; %>
             3    <%@ taglib uri=quot;/WEB-INF/struts-html.tldquot; prefix=quot;htmlquot; %>
             4    <%@ taglib uri=quot;/WEB-INF/struts-logic.tldquot; prefix=quot;logicquot; %>
             5
             6    <html>
             7    <head><title>Submit example</title></head>
             8    <body>
             9
             10    <h3>Example Submit Page</h3>
             11
             12    <html:errors/>
             13
             14    <html:form action=quot;submit.doquot;>
             15    Last Name: <html:text property=quot;lastNamequot;/><br>
             16    Address: <html:textarea property=quot;addressquot;/><br>
             17    Sex:     <html:radio property=quot;sexquot; value=quot;Mquot;/>Male
             18          <html:radio property=quot;sexquot; value=quot;Fquot;/>Female<br>
             19    Married: <html:checkbox property=quot;marriedquot;/><br>
             20    Age:     <html:select property=quot;agequot;>
             21            <html:option value=quot;aquot;>0-19</html:option>
             22            <html:option value=quot;bquot;>20-49</html:option>
             23            <html:option value=quot;cquot;>50-</html:option>
             24          </html:select><br>
             25          <html:submit/>
                                                                                 28
             26    </html:form>




                                                                                      28
03/26/2007




             Example: submit.jsp
             1    <logic:present name=quot;lastNamequot; scope=quot;requestquot;>
             2    Hello
             3    <logic:equal name=quot;submitFormquot; property=quot;agequot; value=quot;aquot;>
             4     young
             5    </logic:equal>
             6    <logic:equal name=quot;submitFormquot; property=quot;agequot; value=quot;cquot;>
             7     old
             8    </logic:equal>
             9    <bean:write name=quot;lastNamequot; scope=quot;requestquot;/>
             10    </logic:present>
             11
             12   </body>
             13   </html>




                                                                             29




                                                                                  29
03/26/2007




             Step 8: Build, Deploy,
             and Test Application


                                      30




                                           30
03/26/2007




             Accessing Web Application




                                         31




                                              31
03/26/2007




             Accessing Web Application




                                         32




                                              32
03/26/2007




             Accessing Web Application




                                         33




                                              33
03/26/2007




             Passion!


                        34




                             34

Contenu connexe

En vedette

What do the national foresight activities tell us? The experience of Estonian...
What do the national foresight activities tell us? The experience of Estonian...What do the national foresight activities tell us? The experience of Estonian...
What do the national foresight activities tell us? The experience of Estonian...Marek Tiits
 
La apatía
La apatíaLa apatía
La apatíacampir
 
Social Studies - All About Me
Social Studies - All About MeSocial Studies - All About Me
Social Studies - All About MeCatherine Connor
 
Struts Tags Speakernoted
Struts Tags SpeakernotedStruts Tags Speakernoted
Struts Tags SpeakernotedHarjinder Singh
 
Como escribir un plan de mercado
Como escribir  un plan de mercadoComo escribir  un plan de mercado
Como escribir un plan de mercadocampir
 

En vedette (10)

What do the national foresight activities tell us? The experience of Estonian...
What do the national foresight activities tell us? The experience of Estonian...What do the national foresight activities tell us? The experience of Estonian...
What do the national foresight activities tell us? The experience of Estonian...
 
Barack Obama
Barack ObamaBarack Obama
Barack Obama
 
Exploratory2
Exploratory2Exploratory2
Exploratory2
 
La apatía
La apatíaLa apatía
La apatía
 
Process Skill 1
Process Skill 1Process Skill 1
Process Skill 1
 
Social Studies - All About Me
Social Studies - All About MeSocial Studies - All About Me
Social Studies - All About Me
 
Exploratory2
Exploratory2Exploratory2
Exploratory2
 
Struts Tags Speakernoted
Struts Tags SpeakernotedStruts Tags Speakernoted
Struts Tags Speakernoted
 
Como escribir un plan de mercado
Como escribir  un plan de mercadoComo escribir  un plan de mercado
Como escribir un plan de mercado
 
Struts Basics
Struts BasicsStruts Basics
Struts Basics
 

Similaire à Step By Step Guide For Buidling Simple Struts App Speakernoted

Step by Step Guide for building a simple Struts Application
Step by Step Guide for building a simple Struts ApplicationStep by Step Guide for building a simple Struts Application
Step by Step Guide for building a simple Struts Applicationelliando dias
 
Struts An Open-source Architecture for Web Applications
Struts An Open-source Architecture for Web ApplicationsStruts An Open-source Architecture for Web Applications
Struts An Open-source Architecture for Web Applicationselliando dias
 
Lab 5a) create a struts application
Lab 5a) create a struts applicationLab 5a) create a struts application
Lab 5a) create a struts applicationtechbed
 
Introduction To J Boss Seam
Introduction To J Boss SeamIntroduction To J Boss Seam
Introduction To J Boss Seamashishkulkarni
 
Seven Simple Reasons to Use AppFuse
Seven Simple Reasons to Use AppFuseSeven Simple Reasons to Use AppFuse
Seven Simple Reasons to Use AppFuseMatt Raible
 
01 Struts Intro
01 Struts Intro01 Struts Intro
01 Struts Introsdileepec
 
Struts 2 Overview
Struts 2 OverviewStruts 2 Overview
Struts 2 Overviewskill-guru
 
The Javascript Toolkit 2.0
The Javascript Toolkit 2.0The Javascript Toolkit 2.0
The Javascript Toolkit 2.0Marcos Vinícius
 
Spring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applicationsSpring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applicationsmichaelaaron25322
 
9 design factors for cloud applications
9 design factors for cloud applications9 design factors for cloud applications
9 design factors for cloud applicationsuEngine Solutions
 
Best practice adoption (and lack there of)
Best practice adoption (and lack there of)Best practice adoption (and lack there of)
Best practice adoption (and lack there of)John Pape
 
Introduction to sails.js
Introduction to sails.jsIntroduction to sails.js
Introduction to sails.jsAmit Bidwai
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2divzi1913
 
Struts Interview Questions
Struts Interview QuestionsStruts Interview Questions
Struts Interview Questionsjbashask
 
What is Vuejs.pptx
What is Vuejs.pptxWhat is Vuejs.pptx
What is Vuejs.pptxNhnHVn2
 

Similaire à Step By Step Guide For Buidling Simple Struts App Speakernoted (20)

Step by Step Guide for building a simple Struts Application
Step by Step Guide for building a simple Struts ApplicationStep by Step Guide for building a simple Struts Application
Step by Step Guide for building a simple Struts Application
 
Struts An Open-source Architecture for Web Applications
Struts An Open-source Architecture for Web ApplicationsStruts An Open-source Architecture for Web Applications
Struts An Open-source Architecture for Web Applications
 
Lab 5a) create a struts application
Lab 5a) create a struts applicationLab 5a) create a struts application
Lab 5a) create a struts application
 
jDriver Presentation
jDriver PresentationjDriver Presentation
jDriver Presentation
 
Introduction To J Boss Seam
Introduction To J Boss SeamIntroduction To J Boss Seam
Introduction To J Boss Seam
 
Seven Simple Reasons to Use AppFuse
Seven Simple Reasons to Use AppFuseSeven Simple Reasons to Use AppFuse
Seven Simple Reasons to Use AppFuse
 
01 Struts Intro
01 Struts Intro01 Struts Intro
01 Struts Intro
 
Struts Into
Struts IntoStruts Into
Struts Into
 
Struts 2 Overview
Struts 2 OverviewStruts 2 Overview
Struts 2 Overview
 
Fame
FameFame
Fame
 
The Javascript Toolkit 2.0
The Javascript Toolkit 2.0The Javascript Toolkit 2.0
The Javascript Toolkit 2.0
 
Spring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applicationsSpring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applications
 
9 design factors for cloud applications
9 design factors for cloud applications9 design factors for cloud applications
9 design factors for cloud applications
 
Best practice adoption (and lack there of)
Best practice adoption (and lack there of)Best practice adoption (and lack there of)
Best practice adoption (and lack there of)
 
Introduction to sails.js
Introduction to sails.jsIntroduction to sails.js
Introduction to sails.js
 
Struts ppt 1
Struts ppt 1Struts ppt 1
Struts ppt 1
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
 
Struts Interview Questions
Struts Interview QuestionsStruts Interview Questions
Struts Interview Questions
 
Struts 1
Struts 1Struts 1
Struts 1
 
What is Vuejs.pptx
What is Vuejs.pptxWhat is Vuejs.pptx
What is Vuejs.pptx
 

Dernier

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 

Dernier (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

Step By Step Guide For Buidling Simple Struts App Speakernoted

  • 1. 03/26/2007 Step by Step Guide for building a simple Struts Application 1 In this session, we will try to build a very simple Struts application step by step. 1
  • 2. 03/26/2007 Sang Shin sang.shin@sun.com www.javapassion.com/j2ee Java™ Technology Evangelist Sun Microsystems, Inc. 2 2
  • 3. 03/26/2007 Sample App We are going to build 3 3
  • 4. 03/26/2007 Sample App ? Keld Hansen's submit application ? Things to do – Creating ActionForm object – Creating Action object – Forwarding at either success or failure through configuration set in struts-config.xml file – Input validation – Internationalizaition ? You can also build it using NetBeans 4 The sample application we are going to use and show is Keld Hansen's submit application. The source files and ant build.xml script can be found in the handson/homework material you can download from class website. This is a simple application but it uses most of Struts framework. 4
  • 5. 03/26/2007 Steps to follow 5 Now let's take a look at the steps you will follow. 5
  • 6. 03/26/2007 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 pages 8.Build, deploy, and test the application 6 So this is the list of steps. Of course, you don't exactly follow these steps in sequence. In fact, it is expected that some of these steps will be reiterated. 6
  • 7. 03/26/2007 Step 1: Create Development Directory Structure 7 The first step is to create development directory structure. 7
  • 8. 03/26/2007 Development Directory Structure ? Same development directory structure for any typical Web application ? Ant build script should be written accordingly ? If you are using NetBeans, the development directory structure is automatically created 8 The source directory structure is the same directory structure we used for other Web application development under Java WSDP. Of course, the build.xml script should be written accordingly. 8
  • 9. 03/26/2007 Step 2: Write web.xml Deployment Descriptor 9 Step 2 is to write web.xml file. 9
  • 10. 03/26/2007 web.xml ? Same structure as any other Web application – ActionServlet is like any other servlet – Servlet definition and mapping of ActionServlet needs to be specified in the web.xml ? There are several Struts specific <init-param> elements – Location of Struts configuration file ? Struts tag libraries could be defined 10 Because Struts application is a genuine Web application, it has to follow the same rules that any Web application has to follow. And one of them is the presence of web.xml deployment descriptor file. The web.xml file should define ActionServlet, which is the controller piece that is provided by the Struts framework, and its mapping with URI. As you will see in the example web.xml file in the following slide, there are several Struts specific initialization parameters. Also the Struts tag libraries also need to be declared. 10
  • 11. 03/26/2007 Example: web.xml 1 <?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?> 2 <web-app version=quot;2.4quot; xmlns=quot;http://java.sun.com/xml/ns/j2eequot; xmlns:xsi=quot;http://www.w3.org/2001/XMLSchema-instancequot; xsi:schemaLocation=quot;http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsdquot;> 3 <servlet> 4 <servlet-name>action</servlet-name> 5 <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> 6 <init-param> 7 <param-name>config</param-name> 8 <param-value>/WEB-INF/struts-config.xml</param-value> 9 </init-param> 10 ... 11 </servlet> 12 <servlet-mapping> 13 <servlet-name>action</servlet-name> 14 <url-pattern>*.do</url-pattern> 15 </servlet-mapping> 11 This is the continuation of the web.xml file. Here you see the declarations of Struts tag libraries. 11
  • 12. 03/26/2007 Step 3: Write struts-config.xml 12 The next step is to write struts-config.xml 12
  • 13. 03/26/2007 struts-config.xml ? Identify required input forms and then define them as <form-bean> elements ? Identify required Action's and then define them as <action> elements within <action-mappings> element – make sure same value of name attribute of <form- bean> is used as the value of name attribute of <action> element – define if you want input validation ? Decide view selection logic and specify them as <forward> element within <action> element 13 (read theslide) 13
  • 14. 03/26/2007 struts-config.xml: <form-beans> 1 <?xml version=quot;1.0quot; encoding=quot;UTF-8quot; ?> 2 3 <!DOCTYPE struts-config PUBLIC 4 quot;-//Apache Software Foundation//DTD Struts Configuration 1.2//ENquot; 5 quot;http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtdquot;> 6 7 8 <struts-config> 9 <form-beans> 10 <form-bean name=quot;submitFormquot; 11 type=quot;submit.SubmitFormquot;/> 12 </form-beans> 14 This is <form-beans> section of the struts-config.xml file. Here we define one <form-bean> element. The value of the name attribute of the <form-bean> element is the same as the value of name attribute of <action> element in the following slide. 14
  • 15. 03/26/2007 struts-config.xml: <action-mappings> 1 2 <!-- ==== Action Mapping Definitions ===============--> 3 <action-mappings> 4 5 <action path=quot;/submitquot; 6 type=quot;submit.SubmitActionquot; 7 name=quot;submitFormquot; 8 input=quot;/submit.jspquot; 9 scope=quot;requestquot; 10 validate=quot;truequot;> 11 <forward name=quot;successquot; path=quot;/submit.jspquot;/> 12 <forward name=quot;failurequot; path=quot;/submit.jspquot;/> 13 </action> 14 15 </action-mappings> 15 This is an example of <action-mappings> element. 15
  • 16. 03/26/2007 Step 4: Write ActionForm classes 16 16
  • 17. 03/26/2007 ActionForm Class ? Extend org.apache.struts.action.ActionForm class ? Decide set of properties that reflect the input form ? Write getter and setter methods for each property ? Write validate() method if input validation is desired 17 17
  • 18. 03/26/2007 Write ActionForm class 1 package submit; 2 3 import javax.servlet.http.HttpServletRequest; 4 import org.apache.struts.action.*; 5 6 public final class SubmitForm extends ActionForm { 7 8 /* Last Name */ 9 private String lastName = quot;Hansenquot;; // default value 10 public String getLastName() { 11 return (this.lastName); 12 } 13 public void setLastName(String lastName) { 14 this.lastName = lastName; 15 } 16 17 /* 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 } 18 25 ... 18
  • 19. 03/26/2007 Write validate() method 1 public final class SubmitForm extends ActionForm { 2 3 ... 4 public ActionErrors validate(ActionMapping mapping, 5 HttpServletRequest request) { 6 7 ... 8 9 // Check for mandatory data 10 ActionErrors errors = new ActionErrors(); 11 if (lastName == null || lastName.equals(quot;quot;)) { 12 errors.add(quot;Last Namequot;, new ActionError(quot;error.lastNamequot;)); 13 } 14 if (address == null || address.equals(quot;quot;)) { 15 errors.add(quot;Addressquot;, new ActionError(quot;error.addressquot;)); 16 } 17 if (sex == null || sex.equals(quot;quot;)) { 18 errors.add(quot;Sexquot;, new ActionError(quot;error.sexquot;)); 19 } 20 if (age == null || age.equals(quot;quot;)) { 21 errors.add(quot;Agequot;, new ActionError(quot;error.agequot;)); 22 } 23 return errors; When this “LogonForm” is associated with a 24 } 19 25 .. controller, it will be passed to the controller whenever it’s service is requested by the user. JSP pages acting as the view for this LogonForm 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 be updated by Struts But what if the user screws up and enters invalid data? ActionForms provide validation… Before an ActionForm object is passed to a controller for processing, a “validate” method can be implemented on the form which allows the form to belay processing until the user fixes invalid input as we will see on the next slide... 19
  • 20. 03/26/2007 Step 5: Write Action classes 20 20
  • 21. 03/26/2007 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 21 21
  • 22. 03/26/2007 Example: Action Class 1 package submit; 2 3 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(quot;lastNamequot;, lastName.toUpperCase()); 19 20 // Forward control to the specified success target 21 return (mapping.findForward(quot;successquot;)); 22 } 23 } 22 22
  • 23. 03/26/2007 Step 6: Create ApplicationResource.properties and Configure web.xml accordingly 23 23
  • 24. 03/26/2007 Resource file ? Create resource file for default locale ? Create resource files for other locales 24 24
  • 25. 03/26/2007 Example: ApplicationResource.properties 1 errors.header=<h4>Validation Error(s)</h4><ul> 2 errors.footer=</ul><hr> 3 4 error.lastName=<li>Enter your last name 5 error.address=<li>Enter your address 6 error.sex=<li>Enter your sex 7 error.age=<li>Enter your age 25 25
  • 26. 03/26/2007 Step 7: Write JSP pages 26 I 26
  • 27. 03/26/2007 JSP Pages ? Write one JSP page for each view ? Use Struts tags for – Handing HTML input forms – Writing out messages 27 27
  • 28. 03/26/2007 Example: submit.jsp 1 <%@ page language=quot;javaquot; %> 2 <%@ taglib uri=quot;/WEB-INF/struts-bean.tldquot; prefix=quot;beanquot; %> 3 <%@ taglib uri=quot;/WEB-INF/struts-html.tldquot; prefix=quot;htmlquot; %> 4 <%@ taglib uri=quot;/WEB-INF/struts-logic.tldquot; prefix=quot;logicquot; %> 5 6 <html> 7 <head><title>Submit example</title></head> 8 <body> 9 10 <h3>Example Submit Page</h3> 11 12 <html:errors/> 13 14 <html:form action=quot;submit.doquot;> 15 Last Name: <html:text property=quot;lastNamequot;/><br> 16 Address: <html:textarea property=quot;addressquot;/><br> 17 Sex: <html:radio property=quot;sexquot; value=quot;Mquot;/>Male 18 <html:radio property=quot;sexquot; value=quot;Fquot;/>Female<br> 19 Married: <html:checkbox property=quot;marriedquot;/><br> 20 Age: <html:select property=quot;agequot;> 21 <html:option value=quot;aquot;>0-19</html:option> 22 <html:option value=quot;bquot;>20-49</html:option> 23 <html:option value=quot;cquot;>50-</html:option> 24 </html:select><br> 25 <html:submit/> 28 26 </html:form> 28
  • 29. 03/26/2007 Example: submit.jsp 1 <logic:present name=quot;lastNamequot; scope=quot;requestquot;> 2 Hello 3 <logic:equal name=quot;submitFormquot; property=quot;agequot; value=quot;aquot;> 4 young 5 </logic:equal> 6 <logic:equal name=quot;submitFormquot; property=quot;agequot; value=quot;cquot;> 7 old 8 </logic:equal> 9 <bean:write name=quot;lastNamequot; scope=quot;requestquot;/> 10 </logic:present> 11 12 </body> 13 </html> 29 29
  • 30. 03/26/2007 Step 8: Build, Deploy, and Test Application 30 30
  • 31. 03/26/2007 Accessing Web Application 31 31
  • 32. 03/26/2007 Accessing Web Application 32 32
  • 33. 03/26/2007 Accessing Web Application 33 33
  • 34. 03/26/2007 Passion! 34 34