Title: Introduction to
1Introduction to Struts Application Framework
Devesh Sinha CSE 598
2Outline
- Framework Concepts
- What is Struts?
- Model-View-Controller Design
- Basic Components of Struts
- Weaknesses/Strengths of Struts
3Framework Concepts
- Reusable, semi-complete application that can be
specialized to produce custom applications - Build on the common ground to provide a starting
point - Help developers focus on what the application
needs to do instead of how the application will
do it.
4Characteristics of Framework Components
- Known to work well in other applications
- Ready to use with next project
- Can be used by other teams in the organization
5Advantages of Frameworks
- Saves Time
- Promotes use of best practices
- Assists beginners
- Provides a starting point
6What is Struts?
- Java/JSP-based application development framework
that is designed for and used with J2EE - Part of Apache Jakarta Project
- July 2001, Struts 1.0 was released
7History of Struts
Java Servlet focused
JSP focused
Servlets along side JSPs
Struts
8Model-View-Controller Design
9Struts Diagram
10Basic Components of Struts
- Model
- ActionForms
- ActionClasses
- View
- JSPs
- Custom Tags
- Controller
- ControllerServlet
- struts-config.xml
11Browser -- Controller
- Controller receives user input from browser
- Controller sends data for display to browser
12Controller Struts Config
- Controller pulls ActionMapping from
struts-config.xml - ActionForm
- ActionClass
- JSPs
- Validate
- Scope
13Struts-config.xml
ltstruts-configgt ltform-beansgt ltform-bean
nameregistrationForm
typeRegistrationForm"/gt lt/form-beansgt
ltaction-mappingsgt ltaction
path"/registration" typeRegistrationAction" n
ame"registrationForm" scope"session" input"/w
eb/acct_edit.jsp" validate"true"gt ltforward
nameprofileSuccess path"/web/account_info.jsp"
/gt ltforward nameaccountSuccess
path"/web/profile_info.jsp"/gt ltforward
name"populateSuccess path"/web/acct_edit.jsp"/gt
lt/actiongt lt/action-mappingsgt lt/struts
-configgt
14Controller -- ActionForm
- Tries to retrieve an existing instance of the
ActionForm from the specified scope. If none
exists it creates a new instance. - Initializes all fields using reset() and
populates the fields from the request - Calls validate method which returns vector of
errors
15Controller Action FormAuto-populate
- ltinput typetext nameaccountNumbergt
- Form Property
- private String accountNumber
- public String getAccountNumber()
- public void setAccountNumber(String
accountNumber) - Replaces request.getParameter(accountNumber)
16Controller Action FormValidate
- If error vector has elements then controller will
forward to the error jsp - This path was retrieved from struts-config.xml
- Other wise it will forward to the ActionClass
17Controller Action Class
- Controller calls the perform() method of the
ActionClass passing in the populated ActionForm - perform(ActionMapping mp, HttpServletRequest req,
HttpServletResponse resp) throws
IOExceptrion,ServletException -
- The Action Class performs business logic and
returns the desired ActionForward to the
Controller
18ActionForm JSP
JSP
Action Form
Data
- If the JSP uses the Struts HTML tags and they see
the right ActionForm, Struts will auto-populate
the html form fields. - This eliminates a lot of the java within the JSP
- There is no need to pull in the instance of the
bean.
19Struts HTML Tags
- HTML Tag
- ltinput typetext nameaccountNumber
valueltbean.getAccountNumbergtgt - Struts Tag
- lthtmltext propertyaccountNumber/gt
- ltbeanwrite nameregistrationForm
propertyaccountNumber/gt
20Struts general Architecture
21(No Transcript)
22Example
- Step through show and save logging parameters
- Note extra beans which encapsulate interaction
with server - LoggingBean.java
- ServerBean.java
23logging.jsp
logging.jsp
index.htm
Web Browser
response sent to browser pure html
request/session
Request ShowLogging.do
expands custom tags
displays internationalized messages
View Layer
Jsp Engine
Logging Form
forwards logging.jsp
creates form bean in correct scope
Controller
setCategoryList(list)
returns forward(success)
perform(mapping, form, request,
response)
creates / reuses
Mappings
Show Logging Action .java
mainpulates data
calls business logic rule to get logging info
Business Logic Layer
LoggingBean.java
ServerBean.java
Data Layer
Granite
24results.jsp
results.jsp
logging.jsp
Web Browser
response sent to browser pure html
form submit actionSaveLogging.do
Displays internationalized messages from bean
expands custom tags
View Layer
request/session
Jsp Engine
Messages Bean
browser adds form variables to request
Logging Form
forwards result.jsp
reuses form bean in correct scope
Controller
getCategoryList()
add result messages
creates / reuses action instance
returns mapping.findForward("success")
calls perform(mapping, form,
request, response)
Mappings
Show Logging Action .java
For each one that user has changed
call business logic to change on server
Business Logic Layer
LoggingBean.java
ServerBean.java
Data Layer
Granite
25Struts Strengths
- Standard logging
- Lightweight
- Open source
- Strong community
- Highly compliant
- Well documented
- Tags
- Strongly founded in design patterns
- Extensible
26Struts Weaknesses
- Debugging
- Requires Struts knowledge
- Support
- Single controller
- Naming
27References
- Struts in Action by Ted Husted, 2003
- The Jakarta Project -- http// jakarta.apache.org
- Struts, an open-source MVC implementation
,Malcolm Davis, Apr 01
http//www-106.ibm.com/developerworks/java/library
/j-struts/ - An Introduction to Struts by Kevin Bedell,
November 2003 http//www.developer.com/java/ent/pr
int.php/1495931 - Struts a Solid Web-App Framework, Tim
Holloway http//www.fawcette.com/javapro/2002_04/m
agazine/features/tholloway/ - Issues in Struts Adoption
- http//www.scioworks.net/devnews/articles/struts_
adoption_issues/index.html - Books
- Struts Kick Start , James Turner Kevin Bedell
- Programming Jakarta Struts, Chuck Cavaness
- The Strus Framework Practical Guide for
Programmers, Sue Spielman
28Thanks
29Struts
- Framework provides
- Central controller ActionServlet
- All paths go through controller
- Configurable through xml
- Action classes
- Adaptors between business layer and web tier
- Can unit test at this level
- ActionForms
- Hold data for each jsp page
- Auto-magically updated
30Struts continued
- Framework provides
- Tag libraries
- Avoid scriptlets
- Page designers can handle
- Built In Internationalization
- Build In Validation of user input
- Advantages
- Excellent architecture and design
- Reuse, modularity, extensibility etc..
- Established framework
- Open source
31Struts general Architecture
- Each task a path
- Path contains info to do that task
- Action class whose perform() method will be
called - Form Bean to hold data to / from view
- Forwards to pass control to on success / failure
- Input jsp auto validate user input
- Graphically