1 1. 1 2 J2EE Struts 1 3 Struts: Introduction 1 4 Once you’ve coded a lot of JSP applications, you find yourself doing a lot of repetitive things. Also,

Slides:



Advertisements
Similar presentations
Struts Portlet Adding an Action Copyright © Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without.
Advertisements

Web Development with Karsten Schulz Terp-Nielsen Master Principal Sales Consultant Oracle Denmark.
Apache Struts Technology
Java II--Copyright © Tom Hunter. J2EE JSP Custom Tag Libraries.
MVC Frameworks Alternatives to coding the MVC pattern.
Struts1 Apache Struts Dr Jim Briggs. Struts2 What is Struts? Struts is an open source framework for building Java web applications Supports MVC/Model.
Object-Oriented Enterprise Application Development Tomcat 3.2 Configuration Last Updated: 03/30/2001.
An introduction to Java Servlet Programming
Struts Basics SSE USTC Qing Ding. Agenda What is and Why Struts? Struts architecture – Controller: Focus of this presentation – Model – View Struts tag.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
27-Jun-15 Directories and DDs. 2 Web apps A web application is basically a web site that: “Knows who you are”--it doesn’t just give you static pages,
Java Server Pages Russell Beale. What are Java Server Pages? Separates content from presentation Good to use when lots of HTML to be presented to user,
Objectives:1. Investigate the Web application deployment descriptor 2. Install and deploy Tomcat and struts 3. Design and deploy a struts application Struts.
Q: According to Intel, the Pentium conforms to the IEEE standards 754 and 854 for floating point arithmetic. If you fly in aircraft designed using a Pentium,
Apache Struts Technology A MVC Framework for Java Web Applications.
ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
1 Guide to JSP common functions 1.Including the libraries as per a Java class, e.g. not having to refer to java.util.Date 2.Accessing & using external.
UNIT-V The MVC architecture and Struts Framework.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Java Server Pages CS-422. What are JSPs A logical evolution of java servlets –most servlets dynamically create HTML and integrate it with some computational.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
J2EE Web Application Best Practices 1 3 Web Apps: Best Practices.
Author: DoanNX Version 2.0/Time: 30’. The ways to solve it There are 2 main ways:  Client-side: Using JavaScript (now one very good option is JQuery).
Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
Introduction to MVC and the Jakarta Struts Framework By Gyanendra Dwivedi Software Consultant.
JSP Java Server Pages Softsmith Infotech.
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces.
JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information 
Mark Dixon 1 12 – Java Beans. Mark Dixon 2 Session Aims & Objectives Aims –To cover the use of Java Beans Objectives, by end of this week’s sessions,
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Struts J2EE web application framework “ Model 2 ” Model View Controller Controller Servlet Key features XML metadata Struts taglib Simplified form validation.
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
J2EE Overview Web Programming CSCI J2EE multi-tier architecture Servlet: Java class loaded into Web server JSP page: enhanced HTML page that is.
How to Build a Struts Application with JBuilder 9.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Struts Framework Anna Paščenko. What is Struts?  An open source framework for building Java web applications.
Introduction to JavaScript CS101 Introduction to Computing.
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming –TextField Action Listeners, JEditorPane action listeners, HTML in a JEditorPane,
13 Copyright © 2004, Oracle. All rights reserved. Adding Validation and Error Handling.
11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions.
Struts Framework Day-2 Ashok Chakravarti. DataSource Usage Sample Struts-config.xml …
Implementation Struts Framework for well-architectured web applications Model-View-Controller design pattern.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Chapter 4 Request and Response. Servlets are controlled by the container.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
APACHE STRUTS ASHISH SINGH TOMAR ast2124. OUTLINE Introduction The Model-View-Controller Design Pattern Struts’ implementation of the MVC Pattern Additional.
J2EE JSP Custom Tag Libraries 1 3 JSP: Custom Tag Libraries.
Apache Struts Technology A MVC Framework for Java Web Applications.
V Web Tier Architecture MVC and Struts. v First, there were servlets And things were good Faster than CGI programs More scalable with built-in.
An Introduction to and This document is for purely educational purposes.
Navigation Mimi Opkins CECS 493 Fall Static Navigation  In a simple web application, navigation is static. That is, clicking a particular button.
Unit 6-Chapter 2 Struts.
The Model Layer What is Model?
Directories and DDs 25-Apr-19.
Directories and DDs 21-Jul-19.
Directories and DDs 14-Sep-19.
Struts BY: Tejashri Udavant..
Presentation transcript:

1 1

1 2 J2EE Struts

1 3 Struts: Introduction

1 4 Once you’ve coded a lot of JSP applications, you find yourself doing a lot of repetitive things. Also, if you have a hard-coded link scattered around a lot of JSPs, the first time you need to change that link you discover a special kind of agony. Struts was designed to solve that problem and a few others. Struts is about moving code to xml property files. Struts: Introduction * * This lecture was based on “Struts in Action” by Ted Husted ISBN

1 5 Struts is based on the MVC or Model-View-Controller design pattern. Struts: Introduction

1 6 Under Struts the work of operating a web application is divided up: ActionServlet —this Struts component controls navigation. Action —this Struts component controls business logic. Struts: Introduction

1 7 Here’s the process: 1.) An ActionServlet receives a request from the container. 2.) The ActionServlet collects the information in the request. 3.) Using the request URI, the ActionServlet tries to match the URI with a so-called Action class. 4.) Whichever Action class is passed the request will take the correct course of action. Struts: Introduction

1 8 The preceding is just a general idea of how it works. Struts uses the following Java classes to work its magic: ActionForm ActionServlet ActionMapping ActionForward Action In addition to these, Struts places a lot of information in xml configuration files: strut-config.xml Struts: Introduction

1 9 We will explore these in depth, but here’s a thumbnail version of each: ActionForm —Collects form data from HTML page. ActionServlet —Controls everything. ActionMapping —Helps when deciding where to go. ActionForward —Where to send request Action —Called to do business logic. Struts: Introduction

1 10 Struts: ActionForm

1 11 A Web Application is driven by HTML pages. A typical WebApp contains text fields in a FORM. To extract information from this page, we use the NAME. Struts: ActionForm Simple JSP Username: Password:

1 12 Given the HTML page we see below, we could pull out the value of the tag with the NAME of user by using the following code in a Servlet’s doPost() method: String userField = request.getParameter("user"); Struts: ActionForm Simple JSP Username: Password:

1 13 However, with any fairly large page, this becomes tedious. A better way is to use a JavaBean. Using a JavaBean, we place all the data in the bean on the JSP side and then it’s convenient for us to unpack it on the servlet side. Struts has taken this one step further. If you follow the rules, Struts will automatically populate a JavaBean for you—saving you the trouble of loading it. Struts: ActionForm

1 14 Okay, what are these rules we’re suppose to follow?  Instead of making up your own JavaBean, you need to extend a special Struts JavaBean called the “ ActionForm ” extends org.apache.struts.action.ActionForm Struts: ActionForm

1 15  A Struts action form: “is a JavaBean that extends ActionForm.”  It captures the input fields sent through the request.  You must create a JavaBean (that extends ActionForm ) that has an instance variable (with the correctly named getters and setters) for each input field in the JSP page. Struts: ActionForm

1 16 So, let’s recall our HTML/JSP file, and then see how we would create the correct ActionForm: Struts: ActionForm Simple JSP Username: Password: import org.apache.struts.action.*; public class SimpleForm extends ActionForm { private String user = “”; private String pass = “”; public SimpleForm(){} public void setUser( String u ) { user = u; } public void setPass( String p ) { pass = p; } public String getUser() { return user; } public String getPass() { return pass; } } As we see, our SimpleForm extends ActionForm It has getters and setters named so they will be automatically found.

1 17 When compiled, this.class file should be placed here: webapps myapp WEB-INF classes Struts: ActionForm

1 18 Struts: ActionForm Here’s what will happen. The ActionServlet will receive a POST, thereby executing its doPost() method. The ActionServlet will try to match the parameters in the request with the properties in the ActionForm. For a parameter wxyz, the JavaBean ( ActionForm ) must have a corresponding setWxyz() and getWxyz() method.

1 19 Struts: ActionServlet

1 20 The ActionServlet behaves like an orchestra Conductor. It doesn’t do a lot of the work, but it manages the other components. The ActionServlet receives a request from the container and it routes the request to the correct place. 99% of the time, you will never need to change the existing ActionServlet Struts: ActionServlet

1 21 The ActionServlet uses a special xml configuration file called: struts-config.xml. Struts: ActionServlet When the web container receives a request in the form of /register-complete/enter.do, then—because of the.do extension—it knows to pass the request off to the ActionServlet. In turn, the ActionServlet looks in its struts-config.xml file to see where to send the request. Using this “mapping”, the ActionServlet knows to send the request to the JSP located at this path. Thus, we have decoupled the location of the actual JSP from its location as implied by URL.

1 22 So, let’s review the sequence so far: 1.) Web container gets a request with a.do extension. 2.) Web container passes the request to the ActionServlet 3.) ActionServlet grabs all the request parameters and tries to insert them in an ActionForm. 4.) ActionServlet strips the.do extension. 5.) ActionServlet looks in the struts-config.xml file for a matching URI pattern. 6.) … Struts: ActionServlet

1 23 Struts: ActionMapping

1 24 The Struts Java class called ActionMapping is used inside of the Action class (not yet covered). It has only one method we’re interested in: ActionMapping mapping = new ActionMapping(); mapping.findForward( “somesymbolicname” ); This means: “When you reach this statement, you should forward the request to the JSP page represented by this symbolic name ‘ somesymbolicname ’.” Struts: ActionMapping

1 25 Struts: Action

1 26 Recall that the business logic of a Struts application is all done in the Action class. You will often need to create your own subclass of this Action class. Your Action class must follow a very specific design pattern: Struts: Action

1 27 import org.apache.struts.action.*; import javax.servlet.http.*; public class MyAction extends Action { public ActionForward perform( ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res ) { } After you have extended the Action class, you will need to override the inherited method called “ perform() ”. In order for your override to work, it must match the signature of the superclass, and thus it must take the form we see here.

1 28 import org.apache.struts.action.*; import javax.servlet.http.*; public class MyAction extends Action { public ActionForward perform( ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res ) { SimpleForm sf = (SimpleForm) form; String username = sf.getUser(); String password = sf.getPass(); } Now, the first step is to cast the received ActionForm reference into your subclass of ActionForm. In this case, it’s called SimpleForm. Recall, since SimpleForm is an ActionForm, we can perform this cast. import org.apache.struts.action.*; public class SimpleForm extends ActionForm {

1 29 import org.apache.struts.action.*; import javax.servlet.http.*; public class MyAction extends Action { public ActionForward perform( ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res ) { SimpleForm sf = (SimpleForm) form; String username = sf.getUser(); String password = sf.getPass(); if( username == null || password == null ) { return mapping.findForward( “failure” ); } else { return mapping.findForward( “success” ); } Finally, we see how—depending on the results of the logic performed in this Action class—we use the ActionMapping class to decide where to send the request from here.

1 30 Remember, your Action class must do these three things:  Cast the incoming ActionForm class into your subclass.  Apply business logic.  Return the correct ActionForward. Struts: Action

1 31 And let’s review how the struts-config.xml would be modified to use our action class: Struts: Action Here we see a new attribute was added to the action. Within the Action class (our MyAction ) we saw the “ findForward() ” method referred to the symbolic name “ success ”. Here’s where that is defined.

1 32 Struts: Review So Far

1 33 As you no doubt see, this can be confusing. The names chosen for the components are not that meaningful and the process is non-intuitive. Still, let’s see if we can understand the entire process. Struts: Review So Far

) Point your browser to a web page. Struts: Review So Far First, we see that we’re executing the JSP called enter.jsp that lives in a web application called register-complete.

) This is the HTML within the page enter.jsp Struts: Review So Far <form name="registerForm" method="POST" action="/register-complete/enter.do"> UserName: enter password: re-enter password: When we click the Submit button, we trigger a POST against the page /register-complete/enter.do. The web container sees that we have asked it to render a page that ends in.do. The web container knows that all page requests that end in.do should be sent to the ActionServlet, which will be allowed to decide what to do.

) The ActionServlet receives the page request. It looks up the URI “mapping” pattern in its struts- config.xml file.  The ActionServlet also stores the request parameters in the ActionForm bean. Struts: Review So Far Since we are already inside the register-complete web application, you can omit that part of the name. So, we’re doing a post against /register- complete/enter.do. The ActionServlet knows to strip off the.do, so it’s looking for a path mapped to /enter. And, we see that does exist.

1 37 This is the RegisterAction.java class. Relying on the struts-config.xml file to identify this class, the ActionServlet will execute the perform() method.

) Next, the ActionServlet looks to see if a name property is associated with the mapping.  In this case, it does find a name property, called “ registerForm ”.  As we see, the name “ registerForm ” appears in the line above. It represents an ActionForm bean called “ RegisterForm ”. Struts: Review So Far The name property tells it which ActionForm class to use.

) Since the ActionServlet has discovered an ActionForm is associated, it instantiates the bean and tries to call its getters and setters for each of the parameters in the request. Struts: Review So Far This same line also informs the ActionServlet which ActionForm class it should use when it captures all the request parameter information.

) Finally, depending on the outcome of the business logic in the RegisterAction class, one of the findForward() methods is executed. Struts: Review So Far

1 41 Struts: Understanding the Architecture

1 42 Struts: Understanding the Architecture Let’s first review the sequence of actions: 1.) A client requests a web page that matches the Action URI pattern. 2.) Seeing the.do extension, the web container passes the request to the ActionServlet. 3.) The ActionServlet looks in its struts-config.xml for the mapping for that particular path. 4.) If that mapping specifies an ActionForm [JavaBean], the ActionServlet either uses an existing instance of that ActionForm or it instantiates a new one. The ActionServlet populates the ActionForm. 5.) The ActionServlet sees which Action class is mapped to that path and executes the perform() method of that Action class. 6.) The Action class does what it needs for business logic. 7.) The Action returns an ActionForward to the ActionServlet.

1 43 1, 2.) A client requests a web page that matches the Action URI pattern. Seeing the.do extension, the web container passes the request to the ActionServlet.  How does this happen? Recall that the web container [the server] has its web.xml configuration file. Within that config file, there is an element called— action *.do —that allows you to specify that all pages with this pattern should go to the servlet action. Struts: Understanding the Architecture

) The ActionServlet looks in its struts-config.xml for the mapping for that particular path.  When the ActionServlet receives a request, it has several things to accomplish: i.) process the locale ii.) process the mapping—to see which Action to send the request to and whether a bean is needed. iii.) if a bean is needed, either find one in scope or create a new one, then process the ActionForm bean—take the request parameters and put them into the bean using its setters. iv.) if the ActionMapping specifies the forward attribute, control is either transferred to another resource or to an Action object. v.) process the Action (if appropriate) by calling the perform() method. vi.) the Action returns an ActionForward Struts: Understanding the Architecture

1 45 Struts: Our First Example

1 46 Once again, we will take the organic approach and see what happens in a linear fashion. Before then, I would like to explore the set up of the web app using Struts in the directory. Struts: Our First Example

1 47 This “ logon ” is the root of the web app. Every path will be considered relative to this directory. In this example, the pages directory will hold the JSP files. You have some flexibility in where you place your JSPs, but wherever they go, you must make sure your paths reflect that location. This WEB-INF directory is very important. In it we will find the files at right. We will explore these files more on the next slide. Our Action classes must go in this directory. In this case, we see an app directory—which tells us that our Action class specified that it was in a package called app. The resources directory will hold.properties files, such as Messages.properties or any other resource we might need. Finally, the lib directory holds JAR files such as the struts.jar file.

1 48 As you recall from the previous slide, these files are contained in the WEB-INF directory. Struts: Our First Example The web.xml file is the same one that must be present in all web applications. In our case, aside from listing the ActionServlet as being present, this web.xml has a line that informs it to send all pages with a.do extension to the ActionServlet for processing. web.xml

1 49 This slide explores the struts-config.xml file Struts: Our First Example First, we create a key “logonForm” that gives the name of our ActionForm struts-config.xml These tell the ActionServlet which Action class to execute when it encounters a particular URI pattern. In this example, you see that a forward has been declared. This must work in concert with the business logic in the Action class.

1 50 This is a look at the HTML behind our first JSP. Struts: Our First Example Welcome.jsp