Agenda Part – I : Understanding Struts. Introduction. Jakarta Struts. Struts 2 Architecture. Part – II : Setting Up Struts. Getting required Packages. “Welcome to Struts 2” – Application. Part – III : Application Development. 10 Applications using Struts 2. Prof. Vinod Pillai
2
Part – I Understanding Struts Prof. Vinod Pillai
3
Introduction Java Application = Desktop || Web || Mobile.
Struts = Apache Struts Web Framework. Framework: Peace of Software that Automates all tedious
task. Use Design patterns commonly agreed by Industry. In built features that commonly needed by most of project. Prof. Vinod Pillai
4
Jakarta Struts The Apache Struts web framework is a free open-
source solution for creating Java web applications. Apache Struts was launched in May 2000, with version 1.0 officially released in July 2001. Why we need it? Web applications based on JSP sometimes combines
database code, page design code, and control flow code. In practice, we find that unless these concerns are separated, larger applications become difficult to maintain. Prof. Vinod Pillai
5
Jakarta Struts One way to separate concerns in a software
application => architecture. MVC
Model-View-Controller
(MVC)
Model = Business or database code View = Page design code Controller = Navigational code.
Struts framework is designed to help developers
create web applications that utilize a MVC architecture. Prof. Vinod Pillai
6
Model View Controller (MVC)
Prof. Vinod Pillai
7
Struts 2 Apache Struts Project offers two major versions: Struts 1 is recognized as the most popular web application
framework for Java. Struts 2 was originally known as WebWork 2.WebWork and Struts communities joined forces to create Struts 2.
Struts 2 is a pull-MVC framework. i.e. the data that is to be displayed to user has to be pulled from the Action. Action class in Struts 2 act as the model in the web application. Unlike Struts, Struts 2 Action class are plain POJO objects thus simplifying the testing of the code. Prof. Vinod Pillai
8
Struts 2 Struts2 also comes with power APIs to configure Interceptors that reduce greatly the coupling in application. The view part of Struts 2 is highly configurable and it supports different result-types such as Velocity, FreeMarker, JSP, etc.
Prof. Vinod Pillai
9
Struts 2 Architecture
Prof. Vinod Pillai
10
Struts 2 Architecture 1. 2.
3.
4. 5.
Request is generated by user and sent to Servlet container. Servlet container invokes FilterDispatcher filter which in turn determines appropriate action. One by one Intercetors are applied before calling the Action. Interceptors performs tasks such as Logging, Validation, File Upload, Double-submit guard etc. Action is executed and the Result is generated by Action.
The output of Action is rendered in the view (JSP, Velocity, etc) and the result is returned to the user. Prof. Vinod Pillai
11
Part – II Setting Up Struts Prof. Vinod Pillai
12
Setting Up Struts *Before we start we need the following tools:
JDK 1.7 (Download)
Eclipse Java EE IDE for Web Developers (Indigo) Tomcat 7 or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (Download) Apache Struts-2.0.6 JAR (Download)
MySQL Database. MySQL Query Browser. MySQL Java JAR File – Database connectivity. Prof. Vinod Pillai
13
JDK 1.7
/>Prof. Vinod Pillai
14
Eclipse Java EE IDE
/> Prof. Vinod Pillai
15
Apache Tomcat 7
/>Prof. Vinod Pillai
16
Apache Struts 2
/>Prof. Vinod Pillai
17
MySQL
/>Prof. Vinod Pillai
18
Setting Up Struts JDK 1.7 = .exe Eclipse Java EE IDE = zip Tomcat 7 = zip
Apache Struts-2.0.6 JAR = zip MySQL Database = .exe Simply Install the software & make sure you remember the path.
Prof. Vinod Pillai
19
Setting Up Struts
Prof. Vinod Pillai
20
1 “Welcome to Apache Struts 2” Our Goal : Our goal is to create a basic Struts2 application with a Submit Button. User will click on the button and the page will be redirected to a Welcome page which will display message “Welcome to Apache Struts 2”.
Prof. Vinod Pillai
21
Getting Started Start Eclipse and enter the workspace name:
Prof. Vinod Pillai
22
Open Eclipse and go to File -> New -> Project and select
Dynamic Web Project in the New Project wizard screen.
Prof. Vinod Pillai
23
Project Name: Welcome Struts. Target runtime: New Runtime> & show the path where you have unzipped the Tomcat Server. Dynamic web module version: 2.5.
Prof. Vinod Pillai
24
Copy JAR files in WebContent -> WEB-INF -> lib folder.