Struts
Struts
Course Objectives
At the end of the course you will be able to
• Understand what is Struts and its benefits as an application framework
• Understand the Model-View-Controller design pattern
• Understand the concepts of Struts Framework and its components
Session Plan
JSP file
Pre-processed Generated
Servlet
Compiled Servlet
.class file
Fundamentals of Web computing (Servlet & JSP) (Cont.)
JSP page is translated and compiled into a Java Servlet
Fundamentals of Web computing (Servlet & JSP) (Cont.)
JSP Sample Code
<%@ page info="Books" errorPage="error.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Book Page</title></head><body>
<h1>Book Form</h1>
<jsp:useBean id="bookBean" class="BookForm" scope="request"/>
<table>
<tr><td>Title:</td>
<td><input name="title" type="text" value='<jsp:getProperty name="bookBean" property="title"/>'</td></tr>
<tr><td>Author:</td>
<td><input name="author" type="text" value='<jsp:getProperty name="bookBean" property="author"/>'></td></tr>
<tr><td>Publisher:</td>
<td><input name="publisher" type="text" value='<jsp:getProperty name="bookBean" property="publisher"/>'></td></tr>
<tr><td>Year:</td>
<td><input name="year" type="text" value='<jsp:getProperty name="bookBean" property="year"/>'></td></tr>
<tr><td>URL:</td>
<td><input name="url" type="text" value='<jsp:getProperty name="bookBean" property="url"/>'></td>
</table>
<A HREF="/clearBook.jsp">Clear form</A><BR>
…
JSP Model
JSP Model (Cont.)
JSP Model 1 Architecture
• JSP page processes request and returns response to client
• Separates content from presentation using JavaBeans
• Forms are submitted to the pages that created them
JSP Model (Cont.)
JSP Model 1 Architecture
JSP Model (Cont.)
Problems with JSP Model 1 Architecture
• Uses much scriplet code in JSP
• Pages can be large and complicated
• Still embeds navigation in page
• Boundaries of responsibility are still unclear.
• Can lead to spaghetti code
• Tends not to be modular
JSP Model (Cont.)
JSP Model 2 Architecture
JSP Model (Cont.)
JSP Model 2 Architecture
• A clear separation of the business logic, presentation output, and request processing.
• Clearly delineates responsibility
• Change layout without changing code
• This separation is often referred to as a Model-View-Controller (MVC)
JSP Model (Cont.)
MVC Design Pattern
The MVC pattern has three key components:
• The Model Component
Responsible for the business domain state knowledge
• The View Component
Responsible for a presentation view of the business domain
• The Controller Component
Responsible for controlling flow and state of the user input
OO Application Frameworks
OO Application Frameworks
What are OO Application Frameworks?
A framework is made up of multiple classes or components, each of which may
provide an abstraction of some particular concept
The framework defines how these abstractions work together to solve a problem
The framework components are reusable
Frameworks are tested, proven software designs and implementations that reduce
the cost, accelerate development speed and improve the quality of software
A good framework should provide generic behavior that can be utilized across
many different types of applications.
OO Application Frameworks (Cont.)
Benefits of using OO Application Frameworks
• Provide developers with modular, reusable and extensible software components
• Modular frameworks reduce the time and effort and investment required to
understand and maintain existing software
• Fosters reuse. Contributes improvements in programmer productivity, as well as
enhancing the quality, performance, reliability and interoperability of software
• Extensibility is essential to ensure timely customization of new application services
and features
• Reduces the cost of building and maintaining software
OO Application Frameworks (Cont.)
Business benefits of using OO Frameworks & Tools
• Adherence to standards
• Uniformity of Code
• Accelerates Software Development
• Reduces cost of development and maintenance
• Reduces developer intervention
Introduction to Struts Framework
Introduction to Struts Framework
What is the Struts Framework?
• Struts is an open source framework created within the Jakarta project by the Apache
Software Foundation
• Struts is a JSP/Servlet framework for building J2EE applications
• Struts accelerates application development based on the Model-View-Controller
(MVC) design pattern
• Current version 1.1
• For more info on struts go to:
http://jakarta.apache.org/struts/
Introduction to Struts Framework (Cont.)
Where does Struts Fit In?
Introduction to Struts Framework (Cont.)
Struts Strengths
• Integrates well with J2EE
• Open Source
• Good taglib support
• Works with existing web apps
• Easy to retain form state
• Unified error handling (via ActionError)
• Easily construct processes spanning multiple steps
• Clear delineation of responsibility makes long term maintenance easier (more
modular)
STRUTS MVC Design Pattern
STRUTS MVC Design Pattern (Cont.)
Names the context-relative path to the struts-config.xml file. The default location is in the /WEB-INF/struts-
config
config.xml directory. (Optional)
Names the context-relative path to the struts-config.xml file associated with a particular Struts Module.
config/${module}
(Optional)
A boolean parameter used to simulate Struts 1.0x functionality when populating an ActionForm. When this
convertNull parameter is set to true, numeric objects (java.lang.Integer, etc.) are initialized to null. If it is set to false,
these objects are set to 0.
Determines the debugging level for the ActionServlet. The default value is 0, which turns debugging off.
debug
(Optional)
Sets the debug level for the Digester object, which is used during ActionServlet initialization. The default
detail
value is 0. (Optional)
Names the fully qualified class of the MultipartRequestHandler implementation to be used when file
multipartClass uploads are being processed. The default value is
org.apache.struts.upload.DiskMultipartRequestHandler. (Optional)
If set to true, tells the ActionServlet that we want to validate the strut-config.xml file against its DTD. While
validating
this parameter is optional, it is highly recommended and therefore the default is set to true.
STRUTS Controller Components
Struts’ HTML/JSP View layer support can be broken down into the following
major components:
JSP pages
Form Beans
JSP tag libraries
Resource bundles
Tag Libraries
STRUTS Tag Libraries
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
Application Resources File
Application Resources File
The ApplicationResources.properties file
• The ApplicationResources.properties file, shown next, is based on Java’s Resource Bundle
functionality for externalizing and internationalizing application strings, messages, and labels.
# Label Resources
label.search.name=Name
label.search.ssNum=Social Security Number
# Error Resources
error.search.criteria.missing=<li>Search Criteria Missing</li>
error.search.ssNum.invalid=<li>Invalid Social Security Number</li>
errors.header=<font color="red"><b>Validation Error(s)</b></font><ul>
errors.footer=</ul><hr width="100%" size="1" noshade="true">
• You’ll notice that this file is simply comprised of name-value pairs, where the name is a key and
the value is a message corresponding to the key. Each of the name-value pairs is then used by
your Struts application whenever a string, message, or label needs to be displayed.
• Externalizing these strings in a separate file instead of embedding them in your application
allows the strings to be changed without having to recompile the application (separation of
concerns). Externalizing the strings also allows your application to support internationalization
so that it can be tailored to different locales.
Resources
STRUTS Resources
Thank You