You are on page 1of 32

Struts framework in J2EE application

Gouri Shankar
29 May 2003

 MVC Pattern
 Brief about Servlets, JSP’s
 About Struts
 Using Struts – with examples
 Pros / Cons
MVC Architecture
About Web Applications – handling dynamic
About Servlets & JSP’s

 Servlets/JSP’s aid web applications to serve

dynamic content.
 Servlets/JSP’s are executed by a servlet container.
 JSP’s are an extension of Servlet Technology. In
JSP’s we can write Java code inside a HTML
 Extends / enhances web applications using Java.
 Component based, platform independent method
for building web applications.
 Web Server & Servlet container together serve
Service Requests.
About Servlets & JSP’s

 Servlets are part of controller portion in MVC.

 JSP’s are part of View portion in MVC.
 A Controller performs the following
– Intercepts HTTP requests from a client.
– Translates requests into a specific business operation
to perform.
– Either invokes business operation or delegates to
– Helps to select the next view to display to the client.
– Returns the next view(HTML/JSP) to the client.
– Centralized point of control for the web application.
Necessity of Struts framework

 Provides solution to a specific problem.

 Prepare reusable components.
 Each may provide an abstraction of some
particular concept.
The Problem

 Scope of the problem dealt in this framework is View &

Controller parts of MVC
 Problems addressed in View
 Mixing code with presentation leading to problems
 Redundancy
 Readability
 Maintenance
 Problems addressed in Controller
 Hard coding resource details in servlets.
 With these problems developer needs to concentrate on
business objects as well as request processing.
 Above problems are addressed by emphasizing on
building components and reusability of components.
About Struts

 Open source framework.

 One of Apache – Jakarta projects from Apache
Software foundation(ASF).
 Originally created by Craig R. McClanahan and
later added to ASF in 2000.

About Struts

 Sits in web container.

 Built on top of JSP, Servlet & taglibs.
 Provides centralized point of control for web
 Because of single entry point, this framework
provides facilities like centralized logging,
authentication, authorization and
 Facilitates to scope the applications.
 Select either HTTP forward or HTTP request.

Struts in J2EE Environment
About Struts

 Custom tag libraries

– 5 types of taglibs
 View beans associated with JSP’s
– Action form, Scope of View Bean(one instance /
scope), dynamic View Beans, one View Bean for
multiple JSP’s, validating user inputs.
 Handle actions associated within a JSP
– Multiple results per action, multiple actions in a
action class, choose either redirect / forward,
validate, calling a new JSP, handling multiple
errors in each action.
About Struts

 Validate user inputs

 Error handling
 Maintain Message Resources
 Validator, tiles frameworks
 Approximately there are 200 classes and 15 java
packages in this framework.
 Main components are
– Configuration file, ActionMapping, ActionServlet,
ActionForm, Action, ActionError(s), Tag libraries
About Configuration file

 Configurations are maintained in struts-config.xml file.

 Contains following information
– Bean Declarations
– Global Exceptions
– Global Forwards
– Action Mappings
– Controller
– Message Resources
– Data Sources
– Plugin
 Possibility to create multiple configuration files.
Class diagram for config
Intialisation Process in Struts Framework
Life cycle for Request Processing an Action
Life cycle of an ActionForm
Flow of an Action in Struts
Exception handling

 Declarative exceptions
 Programmatic exceptions
Tags in struts

 No mixing of logic with presentation.

 Easy to maintain and more emphasis on code
 No code redundancy.
 Better readability.
 Facility to create custom tags.
 Custom tags in struts are
– Bean
– Logic
– Template
– Nested
Taglibs Examples

<html> <body>
<html:form action="/logon" focus="username">
</body> </html>
Equivalent HTML
<form name="logonForm" method="POST" action="/struts-
example/"> </form>
<script language="JavaScript" type="text/javascript">
// -->
</script> </body> <html> Contd….
Tag libs Examples

Example 2
<bean:write name="dept" property="name" />
Equivalent Code

Example 3
<title><bean:message key="title.message"/></title>
Tag libs Examples

<html:link page="/"
paramName="dept" paramProperty="id">show
Equivalent Code
How does Struts fit in MVC ?

 In View
– HTML / JSP,Custom taglibs, JavaScript, Style
Sheets, Message Bundles, ActionForm classes
 In Controller

– ActionServlet, Action, RequestProcessor,

ApplicationConfig, ActionMapping, Struts utility
 In Model

– Need to be implemented by the user.

How do I deploy ?

 Web applications using Struts can be deployed as a

standard WAR file.
 Struts comes as a Java Archive (JAR) file.

 Each action on a JSP page will be associated with

an Action class.
 If necessary same Action class can be used for
multiple action in JSP’s
 If JSP’s capture any data there will also be an
associated Action Form that handles the data for
the input.
 Action form is like a viewbean with some extra
 Effectively user develops a config file, JSP, Action
class and Action form class (if necessary).

 Advantages
– Server-side persistence of the form data
– Provides database support and includes JDBC connection pooling
– Define multiple data sources in configuration file.
– Exception handling.
– Multiple actions handled in a single class.
– Dynamic action form generation.
– As the source code is available, we can extend framework to meet
user needs.
– Maintain multiple configuration files.
 Disadvantages
– No vendor support
– Future compatibility with Struts in case of extension of framework
 Other similar products in the market
– Velocity, Freemarker, WebMacro, Barracuda.
Some Do’s , Don’t’s

 Be selective to use synchronize keyword in Struts

 No instance variables in Action / ActionForm
 Make sub applications as per business flow of
 Use declarative exception handling
 Use Pre built Action classes like ForwardAction,
DispatchAction, SwitchAction etc..
 Use Dynamic ActionForm
 Use visual tools for making config file

Questions & Answers
Thank You