[go: up one dir, main page]

0% found this document useful (0 votes)
405 views30 pages

Struts 2

Struts2 is a model-view-controller framework for developing web applications in Java. It uses interceptors to modularize request processing logic. The core components are actions, interceptors, the value stack, result types, and views. Actions are POJOs mapped to URLs that perform work and return results. Configuration files map actions to interceptor stacks and results, and define common objects like interceptors and result types.

Uploaded by

Vikas Shekar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
405 views30 pages

Struts 2

Struts2 is a model-view-controller framework for developing web applications in Java. It uses interceptors to modularize request processing logic. The core components are actions, interceptors, the value stack, result types, and views. Actions are POJOs mapped to URLs that perform work and return results. Configuration files map actions to interceptor stacks and results, and define common objects like interceptors and result types.

Uploaded by

Vikas Shekar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Struts2

Introduction
Objective
• Web Applications in Java
• MVC
• Brief History
• Why Struts 2.x
• Components of Struts2.x.
• Writing First struts2.x Application
Web Application in Java
• What is Web Container.
• Role of Web Container.
• What is Web Component.
– What is Servlet.
– What is JSP.
– Directory Structure and role of web.xml.
MVC
• Split the request processing for the page the user sees
into processing logic and the presentation logic, letting
each part do what it does the best.

• The implementation used a pattern from Smalltalk known


as the model-view-controller pattern – or more recently
known as the front controller, or in Sun parlance Model 2.

• In this pattern the servlet is the controller, providing a


centralized point of control for all client page requests.
Struts 2.X Brief History
• There was a movement by Patrick Lightbody to
bring together leaders on several different web
frameworks with the goal of achieving a common
framework.
• Although the movement lost momentum, a
commonality between WebWork and the goals of
Struts Ti at the technology. and committer level
was found, and the projects were merged with
WebWork providing the base technology.
Struts2.x=webworks+xworks
• XWork is a generic command framework.
– It provides many of the core features such as
• Actions
• validation
• interceptors,
• is completely execution context independent.
• XWork also provides an internal dependency inject
mechanism that is used for configuration and factory
implementation management.
Struts2.x=webworks+xworks
• WebWork, on the other hand, is a completely
context dependent.
– It provides a wrapper around XWork with the
context that is needed when working on web
applications
– With specific implementations that make web
developer easier.
Goal of Struts2.0
• To make web development easier for the developer.
• To achieve this goal Struts2 provides features
– Reduces XML configuration via intelligent defaults, utilizes
annotations and provides conventions over configuration.
– Actions are now POJOs which increases testability and
reduces coupling in the framework.
– HTML form field data is converted to proper types for the
action to use.
– Still further decreasing coupling is request processing has
been made more modular by allowing a series of
interceptors (custom or Struts2 provided) to provide pre-
processing and post-processing functionality.
Why Struts2.0
• Action based framework.
• Mature with a vibrant developer and user
Community.
• Annotation and XML configuration options.
• POJO-based actions that are easy to test.
• Spring, SiteMesh and Tiles integration.
• OGNL expression language integration.
• Themes based tag libraries and Ajax tags.
• Multiple view options (JSP, Freemarker, Velocity and
XSLT).
• Plug-ins to extend and modify framework features.
Action Based Framework
• Struts2 is an action based MVC web framework.
– It maps the request URL to a unit of work know as an
action.
– The action’s job was to perform specific functionality
for a given URL by accessing the HTTP session, HTTP
request and form parameter, calling business services,
and then mapping the response into a model, whose
form is a plain old java object.
– Finally, the action returned a result, which was mapped
(via configuration files) to a JSP to render as the view.
Components Of Struts 2.x
The Model- View-Controller pattern in Struts2 is realized with five
core components

•Actions,

• Interceptors,

• Value stack / OGNL-


The value stack and OGNL provide common thread, linking
and enabling integration between the other Components.

•Result types and results

•View technologies
High Level Architecture
configuration of a Struts2 application
Configuration files
• web.xml
– configuration for the FilterDispatcher servlet filter.
• struts.properties
– customize the web application execution environment
• struts.xml
– configure the components for the web application

Note:All of the properties contained within the


“struts.properties” configuration file can also be
configured using the “init-param” tag in the
“web.xml”, as well using the “constant” tag in the
“struts.xml” configuration file
struts.properties
• This configuration file provides a mechanism to
change the default behavior of the framework.
• For the most up-to-date information on the
properties take a looks at the Struts2 wiki at
http://struts.apache.org/2.x/docs/strutsproperties.html.
• A default properties file named default.properties”
is contained in the Strut2-Core JAR distribution.
• To enable modifications to a property, simply create
a file called “struts.properties” in the root of your
projects source files classpath.
Some properties
• struts.i18n.reload = true – enables reloading of
internationalization files

• struts.devMode = true – enables development mode


that provides more comprehensive debugging

• struts.configuration.xml.reload = true – enables reloading of


XML configuration files (for the action) when a change is
made without reloading the entire web application in the
servlet container

• struts.url.http.port = 8080 – sets the port that the server


is run on (so that generated URLs are created correctly)
Struts.xml
• The “struts.xml” file contains the configuration
information that you will be modifying as actions are
developed.
• Depending on the functionality of your application, it is
possible to remove the “struts.xml” file from your
application completely. The configurations can be handled
by alternative methods that include annotations,
“web.xml” startup parameters, and alternate URL
mapping schemes.
• The only configurations that still need the “struts.xml” file
are global results, exception handling, and custom
interceptor stacks.
Structure of struts.xml
Other structure with Include

•When including files, order is important. The information from the included file will
be available from the point that the include tag is placed in the file. Hence, to use a
tag that is configured in another file, the include configuration must occur before it
is referenced.

•There are some that are included automatically. These are the “strutsdefault.
xml” and the “struts-plugin.xml” files. Both contains default configurations for
result types, interceptors, interceptor stacks, packages as well as configuration
information for the web application execution environment
Some Important Tags in struts.xml
• The Package Tag:
• The <package … /> tag is used to group together
configurations that share common attributes
such as interceptor stacks or URL namespaces.
Usually this consists of action configurations,
• It may include any type of configuration
information.
• It may also be useful to organizationally separate
functions, which may be further separated into
different configuration files.
Package tag attributes
• name – a developer provided unique name for this package

• extends – the name of a package that this package will extend; all
configuration information (including action configurations) from the
extended package will be available in the new package, under the new
namespace

• namespace – the namespace provides a mapping from the URL to the


package. i.e. for two different packages,with namespace attributes
defined as “package1” and “package2”, the URLs would be something like
“/myWebApp/package1/my.action” and
“/myWebApp/package2/my.action”

• abstract – if this attribute value is “true” the package is truly


configuration grouping, and actions configured will not be accessible via
the package name
Working with Actions
• Actions are a fundamental concept in most
web application frameworks, and they are the
most basic unit of work that can be associated
with a HTTP request coming from a user.
– Single Result Action
– Multiple Result Action
Single Result Action
Multi result Action
Configuring Result types
• The results that are generated, and returned to the
user from an action for different result values, do not
all need to be the same type.

• The result “success” may render a JSP page, but the


result “error” may need to send a HTTP header back to
the browser.

• The type of the result is configured using the “type”


attribute on the result node. Like the “name” attribute,
there is a default value for this attribute – “dispatcher”
which will render JSPs.
Interceptors
• Many of the features provided in the Struts2 framework are
implemented using interceptors; examples include exception
handling, file uploading, lifecycle callbacks and validation.

• Interceptors are conceptually the same as servlet filters or the


JDKs Proxy class.

• They provide a way to supply pre-processing and post-


processing around the action.

• Similar to servlet filters, interceptors can be layered and


ordered. They have access to the action being executed, as
well as all environmental variables and execution properties.
Enabling Interceptors
• Many interceptors have been preconfigured for you. Just make sure
that the package your actions are in extends the “struts-default”
package.
• To configure a new interceptor, we first need to define the
interceptor. The <interceptors … /> and <interceptor …/> tags are
placed directly under the <package> tag.
Applying Interceptor on Action
• We also need to ensure that the interceptor is
applied to the action that requires it.
• This can be achieved in two ways.
– The first is to assign the interceptor to each action
individually.
Assigning Interceptor to Package
• The second way is to assign a default
interceptor for the current package
• Only one interceptor can be assigned as the
default.
Exercises
Follow Trainer Exercises
Use Documentation for Tag references.

You might also like