Presentation_Ashish_Tomar

Download Report

Transcript Presentation_Ashish_Tomar

APACHE STRUTS
ASHISH SINGH TOMAR
ast2124
OUTLINE





Introduction
The Model-View-Controller Design Pattern
Struts’ implementation of the MVC Pattern
Additional Features
Summary
What is a Web Application
Framework?
“A structure for supporting or enclosing
something else, especially a skeletal
support used as the basis for something
being constructed.”
What is Apache Struts?

Struts is an open-source framework for building more
flexible, maintainable and structured front-ends in
Java web applications

There are two key components in a web application:

Struts
◦ the data and business logic performed on this data
◦ the presentation of data
◦ helps structuring these components in a Java web app.
◦ controls the flow of the web application, strictly separating
these components
◦ unifies the interaction between them
JSP/Servlet Web-Applications

Traditionally, there are 3 ways to generate
dynamic output (typically HTML or XML) in Java
web applications:
◦ Servlets
 Java classes with some special methods (doGet(), doPost(),
…)
 Example: out.println("<H1>" + myString + "</H1>");
 no separation between code and presentation!
◦ JSPs (Java Server Pages)
 HTML (or other) code with embedded Java code
(Scriptlets)
 compiled to Servlets when used for the first time
 Example: <H1><% out.println(myString); %></H1>
 better, but still no separation between code and
presentation!
The Model-View-Controller Pattern

Splits up responsibilities for handling user
interactions in an application into three layers:
◦ Model, View, Controller
The Model-View-Controller Pattern

Model
◦ holds application data and business logic
◦ is absolutely independent from the UIs

View
◦ independent from the internal implementation of the Model
◦ there can be different Views presenting the same Model data

Controller
◦ “bridge” between Model and View
◦ controls the flow of the application
 receives/interprets user input
 performs operations on the Model
Struts Implementation of MVC
Controller



ActionServlet
manages the flow of the application
◦ receives user requests and delegates them
to the corresponding Action classes
◦ selects the appropriate View to be displayed next
(according to ActionForward returned by an Action class)
Action
◦ are Java classes that extend Struts’ Action
class org.apache.struts.action.Action
◦ The Action's execute() method is called by
the ActionServlet
◦ return an appropriate ActionForward object that tells the
ActionServlet which View component it should forward to
Controller(cont.)

ActionForm
◦ are Java classes that extend Struts’ ActionForm
class org.apache.struts.action.ActionForm
◦ are filled with the form data by the
ActionServlet
◦ provide getter/setter methods to access them
◦ Validation
External Configuration of
struts-config.xml
Defines the control flow, the mapping between






components and other global options:
Example:
action-mappings
<struts-config>
form-beans
<!– [...] -->
Forwards
<form-beans>
<form-bean name="searchForm"
plug-ins
type="com.japp.SearchForm"/>
The entire logical flow of the
application is in a
hierarchical text file
</form-beans>
<action-mappings>
<action path="/search"
type="com.japp.SearchAction"
name="searchForm"
scope="request“ validate="true"
<forward
name="success“path="/next.jsp"/>
input="/search.jsp">
</action>
</action-mappings>
<!– [...] -->
</struts-config>
View

Technologies for View
JSP
 Cocoon
 Swing
 Velocity


Struts tag libraries
◦
◦
◦
◦
provide access to Model data
enable interaction with ActionForms
provide simple structural logic (such as iteration)
...
Model

Holds the data of an application and provides
business logic methods

Model classes access and manipulates the data
from database and return it to the invoker Action
class

The Model is usually built of different kinds
of Business Objects:
◦ JavaBeans
 simple Java classes, that follow certain naming conventions
 contain attributes and corresponding getters/setters
 reside in the Web Container
Tiles

Templates for Web pages
◦ many different page components can be
assembled to a “big” page
 very useful when having content that is used on
many different pages (e.g. sidebars)

Layout various tiles of a web page
◦ Header, Navigation, Body, Footer

Specified in tiles-definition.xml
◦ Specifies the layout
◦ Logical names for various tiles
◦ Associate filenames to the tiles
Tiles(cont.)
Sketch the layout
 Make Template file that represents layout

◦ Use <tiles:insert> tag to stub out sections that
will be filled in by templates
Create JSP pages that define layout pieces
 Create JSP pages that populate layout

◦ Use <tiles:put> tag to specify the layout pieces
that apply to this specific page
i18n
◦ Struts offers some features to easily
internationalize an application
◦ Text output can be defined in "resource
bundles" that can be provided for many
different languages
◦ Struts automatically detects the users language
through the HTTP request
Ajax Support
◦ Only the part of the page that has to be
processed will be transferred to the web server
and the processed data will be loaded in the
page without a page reload
◦ Used mainly when cascading is required or high
performance is required
◦ Built in support: DWR and Dojo
Summary
◦
◦
◦
◦
◦
◦
Open source
Good documentation
Stable and Mature
Separation of Presentation and Business logic
Maintainability and Reusability of Code
Central Flow Control
THANK YOU!!