SlideShare une entreprise Scribd logo
1  sur  34
Télécharger pour lire hors ligne
1
Step by Step GuideStep by Step Guide
for building a simplefor building a simple
Struts ApplicationStruts Application
2
Sang Shin
sang.shin@sun.com
www.javapassion.com/j2ee
Java™
Technology Evangelist
Sun Microsystems, Inc.
3
Sample App We areSample App We are
going to buildgoing to build
4
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
5
Steps to followSteps to follow
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 pages
8.Build, deploy, and test the application
7
Step 1: Create DevelopmentStep 1: Create Development
Directory StructureDirectory Structure
8
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
9
Step 2: Write web.xmlStep 2: Write web.xml
Deployment DescriptorDeployment Descriptor
10
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
11
Example: web.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
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>
12
Step 3: WriteStep 3: Write
struts-config.xmlstruts-config.xml
13
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
14
struts-config.xml: <form-beans>
1 <?xml version="1.0" encoding="UTF-8" ?>
2
3 <!DOCTYPE struts-config PUBLIC
4 "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
5 "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
6
7
8 <struts-config>
9 <form-beans>
10 <form-bean name="submitForm"
11 type="submit.SubmitForm"/>
12 </form-beans>
15
struts-config.xml:
<action-mappings>
1
2 <!-- ==== Action Mapping Definitions ===============-->
3 <action-mappings>
4
5 <action path="/submit"
6 type="submit.SubmitAction"
7 name="submitForm"
8 input="/submit.jsp"
9 scope="request"
10 validate="true">
11 <forward name="success" path="/submit.jsp"/>
12 <forward name="failure" path="/submit.jsp"/>
13 </action>
14
15 </action-mappings>
16
Step 4: WriteStep 4: Write
ActionForm classesActionForm classes
17
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
18
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 = "Hansen"; // 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 }
19
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("")) {
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 }
20
Step 5: WriteStep 5: Write
Action classesAction classes
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
22
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("lastName", lastName.toUpperCase());
19
20 // Forward control to the specified success target
21 return (mapping.findForward("success"));
22 }
23 }
23
Step 6: CreateStep 6: Create
ApplicationResource.propertiesApplicationResource.properties
and Configure web.xmland Configure web.xml
accordinglyaccordingly
24
Resource file
● Create resource file for default locale
● Create resource files for other locales
25
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
26
Step 7: Write JSP pagesStep 7: Write JSP pages
27
JSP Pages
● Write one JSP page for each view
● Use Struts tags for
– Handing HTML input forms
– Writing out messages
28
Example: submit.jsp
1 <%@ page language="java" %>
2 <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
3 <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
4 <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
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="submit.do">
15 Last Name: <html:text property="lastName"/><br>
16 Address: <html:textarea property="address"/><br>
17 Sex: <html:radio property="sex" value="M"/>Male
18 <html:radio property="sex" value="F"/>Female<br>
19 Married: <html:checkbox property="married"/><br>
20 Age: <html:select property="age">
21 <html:option value="a">0-19</html:option>
22 <html:option value="b">20-49</html:option>
23 <html:option value="c">50-</html:option>
24 </html:select><br>
25 <html:submit/>
26 </html:form>
29
Example: submit.jsp
1 <logic:present name="lastName" scope="request">
2 Hello
3 <logic:equal name="submitForm" property="age" value="a">
4 young
5 </logic:equal>
6 <logic:equal name="submitForm" property="age" value="c">
7 old
8 </logic:equal>
9 <bean:write name="lastName" scope="request"/>
10 </logic:present>
11
12 </body>
13 </html>
30
Step 8: Build, Deploy,Step 8: Build, Deploy,
and Test Applicationand Test Application
31
Accessing Web Application
32
Accessing Web Application
33
Accessing Web Application
34
Passion!Passion!

Contenu connexe

Tendances

React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux IntroductionNikolaus Graf
 
Backday Xebia : Découvrez Spring Boot sur un cas pratique
Backday Xebia : Découvrez Spring Boot sur un cas pratiqueBackday Xebia : Découvrez Spring Boot sur un cas pratique
Backday Xebia : Découvrez Spring Boot sur un cas pratiquePublicis Sapient Engineering
 
Better web apps with React and Redux
Better web apps with React and ReduxBetter web apps with React and Redux
Better web apps with React and ReduxAli Sa'o
 
Redux training
Redux trainingRedux training
Redux trainingdasersoft
 
A full introductory guide to React
A full introductory guide to ReactA full introductory guide to React
A full introductory guide to ReactJean Carlo Emer
 
React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with ReduxVedran Blaženka
 
Photo Insert and Retrieve App
Photo Insert and Retrieve AppPhoto Insert and Retrieve App
Photo Insert and Retrieve AppPeeyush Ranjan
 
React JS and Redux
React JS and ReduxReact JS and Redux
React JS and ReduxGlib Kechyn
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practicesClickky
 
ReactJs presentation
ReactJs presentationReactJs presentation
ReactJs presentationnishasowdri
 
React.js and Redux overview
React.js and Redux overviewReact.js and Redux overview
React.js and Redux overviewAlex Bachuk
 
Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3 Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3 DreamLab
 

Tendances (20)

The JavaFX Ecosystem
The JavaFX EcosystemThe JavaFX Ecosystem
The JavaFX Ecosystem
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 
Backday Xebia : Découvrez Spring Boot sur un cas pratique
Backday Xebia : Découvrez Spring Boot sur un cas pratiqueBackday Xebia : Découvrez Spring Boot sur un cas pratique
Backday Xebia : Découvrez Spring Boot sur un cas pratique
 
From Swing to JavaFX
From Swing to JavaFXFrom Swing to JavaFX
From Swing to JavaFX
 
Better web apps with React and Redux
Better web apps with React and ReduxBetter web apps with React and Redux
Better web apps with React and Redux
 
React & redux
React & reduxReact & redux
React & redux
 
Redux training
Redux trainingRedux training
Redux training
 
A full introductory guide to React
A full introductory guide to ReactA full introductory guide to React
A full introductory guide to React
 
React / Redux Architectures
React / Redux ArchitecturesReact / Redux Architectures
React / Redux Architectures
 
React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with Redux
 
Photo Insert and Retrieve App
Photo Insert and Retrieve AppPhoto Insert and Retrieve App
Photo Insert and Retrieve App
 
React JS and Redux
React JS and ReduxReact JS and Redux
React JS and Redux
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practices
 
ReactJS for Beginners
ReactJS for BeginnersReactJS for Beginners
ReactJS for Beginners
 
JavaFX Advanced
JavaFX AdvancedJavaFX Advanced
JavaFX Advanced
 
Intro to ReactJS
Intro to ReactJSIntro to ReactJS
Intro to ReactJS
 
ReactJs presentation
ReactJs presentationReactJs presentation
ReactJs presentation
 
Learn react-js
Learn react-jsLearn react-js
Learn react-js
 
React.js and Redux overview
React.js and Redux overviewReact.js and Redux overview
React.js and Redux overview
 
Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3 Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3
 

Similaire à Stepbystepguideforbuidlingsimplestrutsapp 090702025438-phpapp02

Step By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts AppStep By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts AppSyed Shahul
 
Struts tutorial
Struts tutorialStruts tutorial
Struts tutorialOPENLANE
 
Project Description Of Incident Management System Developed by PRS (CRIS) , N...
Project Description Of Incident Management System Developed by PRS (CRIS) , N...Project Description Of Incident Management System Developed by PRS (CRIS) , N...
Project Description Of Incident Management System Developed by PRS (CRIS) , N...varunsunny21
 
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
 
Strut2-Spring-Hibernate
Strut2-Spring-HibernateStrut2-Spring-Hibernate
Strut2-Spring-HibernateJay Shah
 
Javascript ui for rest services
Javascript ui for rest servicesJavascript ui for rest services
Javascript ui for rest servicesIoan Eugen Stan
 
JavaDo#09 Spring boot入門ハンズオン
JavaDo#09 Spring boot入門ハンズオンJavaDo#09 Spring boot入門ハンズオン
JavaDo#09 Spring boot入門ハンズオンharuki ueno
 
JavaServer Faces 2.0 - JavaOne India 2011
JavaServer Faces 2.0 - JavaOne India 2011JavaServer Faces 2.0 - JavaOne India 2011
JavaServer Faces 2.0 - JavaOne India 2011Arun Gupta
 
Mobile App Development: Primi passi con NativeScript e Angular 2
Mobile App Development: Primi passi con NativeScript e Angular 2Mobile App Development: Primi passi con NativeScript e Angular 2
Mobile App Development: Primi passi con NativeScript e Angular 2Filippo Matteo Riggio
 
Ajax, JSF, Facelets, Eclipse & Maven tutorials
Ajax, JSF, Facelets, Eclipse & Maven tutorialsAjax, JSF, Facelets, Eclipse & Maven tutorials
Ajax, JSF, Facelets, Eclipse & Maven tutorialsRaghavan Mohan
 
Rest web service_with_spring_hateoas
Rest web service_with_spring_hateoasRest web service_with_spring_hateoas
Rest web service_with_spring_hateoasZeid Hassan
 
BlackBerry DevCon 2011 - PhoneGap and WebWorks
BlackBerry DevCon 2011 - PhoneGap and WebWorksBlackBerry DevCon 2011 - PhoneGap and WebWorks
BlackBerry DevCon 2011 - PhoneGap and WebWorksmwbrooks
 
Introduction to Struts
Introduction to StrutsIntroduction to Struts
Introduction to Strutselliando dias
 
Angular JS2 Training Session #2
Angular JS2 Training Session #2Angular JS2 Training Session #2
Angular JS2 Training Session #2Paras Mendiratta
 
Introduction to Palm's Mojo SDK
Introduction to Palm's Mojo SDKIntroduction to Palm's Mojo SDK
Introduction to Palm's Mojo SDKBrendan Lim
 
Rich Portlet Development in uPortal
Rich Portlet Development in uPortalRich Portlet Development in uPortal
Rich Portlet Development in uPortalJennifer Bourey
 

Similaire à Stepbystepguideforbuidlingsimplestrutsapp 090702025438-phpapp02 (20)

Step By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts AppStep By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts App
 
Struts tutorial
Struts tutorialStruts tutorial
Struts tutorial
 
Project Description Of Incident Management System Developed by PRS (CRIS) , N...
Project Description Of Incident Management System Developed by PRS (CRIS) , N...Project Description Of Incident Management System Developed by PRS (CRIS) , N...
Project Description Of Incident Management System Developed by PRS (CRIS) , N...
 
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
 
Strut2-Spring-Hibernate
Strut2-Spring-HibernateStrut2-Spring-Hibernate
Strut2-Spring-Hibernate
 
Jsf
JsfJsf
Jsf
 
Unit 07: Design Patterns and Frameworks (3/3)
Unit 07: Design Patterns and Frameworks (3/3)Unit 07: Design Patterns and Frameworks (3/3)
Unit 07: Design Patterns and Frameworks (3/3)
 
Javascript ui for rest services
Javascript ui for rest servicesJavascript ui for rest services
Javascript ui for rest services
 
Struts Intro
Struts IntroStruts Intro
Struts Intro
 
JavaDo#09 Spring boot入門ハンズオン
JavaDo#09 Spring boot入門ハンズオンJavaDo#09 Spring boot入門ハンズオン
JavaDo#09 Spring boot入門ハンズオン
 
JavaServer Faces 2.0 - JavaOne India 2011
JavaServer Faces 2.0 - JavaOne India 2011JavaServer Faces 2.0 - JavaOne India 2011
JavaServer Faces 2.0 - JavaOne India 2011
 
Mobile App Development: Primi passi con NativeScript e Angular 2
Mobile App Development: Primi passi con NativeScript e Angular 2Mobile App Development: Primi passi con NativeScript e Angular 2
Mobile App Development: Primi passi con NativeScript e Angular 2
 
Ajax, JSF, Facelets, Eclipse & Maven tutorials
Ajax, JSF, Facelets, Eclipse & Maven tutorialsAjax, JSF, Facelets, Eclipse & Maven tutorials
Ajax, JSF, Facelets, Eclipse & Maven tutorials
 
Rest web service_with_spring_hateoas
Rest web service_with_spring_hateoasRest web service_with_spring_hateoas
Rest web service_with_spring_hateoas
 
BlackBerry DevCon 2011 - PhoneGap and WebWorks
BlackBerry DevCon 2011 - PhoneGap and WebWorksBlackBerry DevCon 2011 - PhoneGap and WebWorks
BlackBerry DevCon 2011 - PhoneGap and WebWorks
 
Introduction to Struts
Introduction to StrutsIntroduction to Struts
Introduction to Struts
 
Jsf intro
Jsf introJsf intro
Jsf intro
 
Angular JS2 Training Session #2
Angular JS2 Training Session #2Angular JS2 Training Session #2
Angular JS2 Training Session #2
 
Introduction to Palm's Mojo SDK
Introduction to Palm's Mojo SDKIntroduction to Palm's Mojo SDK
Introduction to Palm's Mojo SDK
 
Rich Portlet Development in uPortal
Rich Portlet Development in uPortalRich Portlet Development in uPortal
Rich Portlet Development in uPortal
 

Dernier

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 

Dernier (20)

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 

Stepbystepguideforbuidlingsimplestrutsapp 090702025438-phpapp02

  • 1. 1 Step by Step GuideStep by Step Guide for building a simplefor building a simple Struts ApplicationStruts Application
  • 3. 3 Sample App We areSample App We are going to buildgoing to build
  • 4. 4 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
  • 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 pages 8.Build, deploy, and test the application
  • 7. 7 Step 1: Create DevelopmentStep 1: Create Development Directory StructureDirectory Structure
  • 8. 8 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
  • 9. 9 Step 2: Write web.xmlStep 2: Write web.xml Deployment DescriptorDeployment Descriptor
  • 10. 10 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
  • 11. 11 Example: web.xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 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>
  • 12. 12 Step 3: WriteStep 3: Write struts-config.xmlstruts-config.xml
  • 13. 13 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
  • 14. 14 struts-config.xml: <form-beans> 1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <!DOCTYPE struts-config PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" 5 "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> 6 7 8 <struts-config> 9 <form-beans> 10 <form-bean name="submitForm" 11 type="submit.SubmitForm"/> 12 </form-beans>
  • 15. 15 struts-config.xml: <action-mappings> 1 2 <!-- ==== Action Mapping Definitions ===============--> 3 <action-mappings> 4 5 <action path="/submit" 6 type="submit.SubmitAction" 7 name="submitForm" 8 input="/submit.jsp" 9 scope="request" 10 validate="true"> 11 <forward name="success" path="/submit.jsp"/> 12 <forward name="failure" path="/submit.jsp"/> 13 </action> 14 15 </action-mappings>
  • 16. 16 Step 4: WriteStep 4: Write ActionForm classesActionForm classes
  • 17. 17 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
  • 18. 18 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 = "Hansen"; // 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 }
  • 19. 19 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("")) { 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 }
  • 20. 20 Step 5: WriteStep 5: Write Action classesAction classes
  • 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
  • 22. 22 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("lastName", lastName.toUpperCase()); 19 20 // Forward control to the specified success target 21 return (mapping.findForward("success")); 22 } 23 }
  • 23. 23 Step 6: CreateStep 6: Create ApplicationResource.propertiesApplicationResource.properties and Configure web.xmland Configure web.xml accordinglyaccordingly
  • 24. 24 Resource file ● Create resource file for default locale ● Create resource files for other locales
  • 25. 25 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
  • 26. 26 Step 7: Write JSP pagesStep 7: Write JSP pages
  • 27. 27 JSP Pages ● Write one JSP page for each view ● Use Struts tags for – Handing HTML input forms – Writing out messages
  • 28. 28 Example: submit.jsp 1 <%@ page language="java" %> 2 <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> 3 <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> 4 <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> 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="submit.do"> 15 Last Name: <html:text property="lastName"/><br> 16 Address: <html:textarea property="address"/><br> 17 Sex: <html:radio property="sex" value="M"/>Male 18 <html:radio property="sex" value="F"/>Female<br> 19 Married: <html:checkbox property="married"/><br> 20 Age: <html:select property="age"> 21 <html:option value="a">0-19</html:option> 22 <html:option value="b">20-49</html:option> 23 <html:option value="c">50-</html:option> 24 </html:select><br> 25 <html:submit/> 26 </html:form>
  • 29. 29 Example: submit.jsp 1 <logic:present name="lastName" scope="request"> 2 Hello 3 <logic:equal name="submitForm" property="age" value="a"> 4 young 5 </logic:equal> 6 <logic:equal name="submitForm" property="age" value="c"> 7 old 8 </logic:equal> 9 <bean:write name="lastName" scope="request"/> 10 </logic:present> 11 12 </body> 13 </html>
  • 30. 30 Step 8: Build, Deploy,Step 8: Build, Deploy, and Test Applicationand Test Application