struts-intro
Download
Report
Transcript struts-intro
Implementation
Struts
http://jakarta.apache.org/struts/index.html
Framework for well-architectured web
applications
Model-View-Controller design pattern
Model:
application state
View:
presentation of that data
Controller: handles routing of app
Separates three elements distinctly
Model – View – Controller
Struts
Framework provides
Central controller – ActionServlet
Action classes
All paths go through controller
Configurable through xml
Adaptors between business layer and web tier
Can unit test at this level
ActionForms
Hold data for each jsp page
Auto-magically updated
Struts – 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
Struts – 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:
Jsp 2
Jsp 1
Page 1
Jsp 3
Web Browser
Web Server
Form
Bean
1
Jsp Engine
request/session
Form
Bean
2
Other
Bean
1
View
Layer
Controller
Strutsconfig.
xml
Mappings
•
•
•
•
path
action
[form bean]
[forwards]
Action1
.java
Business
Bean 1
CoreSession
Action2
.java
Action3
.java
Business
Bean 2
CoreSession
MarkVision Server
Action4
.java
Business
Bean 3
Business
Logic
Layer
Socket
Data
Layer
Example
Step through show and save logging
parameters
Note extra beans which encapsulate
interaction with server
LoggingBean.java
ServerBean.java
logging.jsp
index.htm
Web Browser
<form name="LoggingForm" …>
<logic:iterate id="category“ name="LoggingForm" property="categoryList">
<bean:write name="category" property="key"/>
<bean:write name="category" property="desc"/>
….
</logic:iterate>
</form>
response sent to
browser – pure html
Web Server
Request:
ShowLogging.do
Jsp Engine
populates
form values
expands internationalized
custom
tags
displays
messages
request/session
Messages
forwards – logging.jsp
Resources
creates form bean in
correct scope
Logging
Form
View
Layer
Controller
looks up
Mappings
<!-- Show logging -->
<action
path="/ShowLogging"
type="beans.ShowLoggingAction"
name="LoggingForm">
<forward name="success“
path="/logging.jsp" />
</action>
setCategoryList(list)
perform(mapping,
returns
creates / reuses form,
request, response)
forward(success)
Show
Logging
Action
.java
mainpulates data
calls business logic rule
to get logging info
LoggingBean.java
ServerBean.java
Granite
Business
Logic
Layer
Data
Layer
<form name="LoggingForm" …>
<select name="category[0].value" >
</form>
becomes request parameter
category[0].value
added to form using
LoggingForm.getCategory(0).setValue(value)
results.jsp
logging.jsp
Web Browser
response sent to
browser – pure html
Web Server
form submit –
action=‘SaveLogging.do’
Jsp Engine
browser adds form
variables to request
Displays internationalized
expands custom tags
messages from bean
request/session
• • ••
• • • Logging
forwards – result.jsp
Form
auto populates
using
reuses
form bean
in
bean getters/setters
correct
scope
Controller
looks up
Mappings
<!-- Save logging -->
<action
path="/SaveLogging"
type="beans.SaveLoggingAction"
name="LoggingForm">
<forward name="success“
path="/result.jsp" />
</action>
View
Layer
Messages
Bean
getCategoryList()
creates
/ reuses
returns
calls
perform(mapping,
form,
request, response)
action instance
mapping.findForward("success")
Show
Logging
Action
.java
add result messages
For each one that user has
changed…
call business logic to change on server
LoggingBean.java
ServerBean.java
Granite
Business
Logic
Layer
Data
Layer
Jsp 2
Jsp 1
Page 1
Jsp 3
pure HTML sent to
browser
Web Browser
Web Server
incoming requests
Jsp Engine
relevant page called
Bean
1
•• •
• • ••
if
submit, autoany
creates/reuses
populates
associatedform
formbean
bean
from request params
Controller
reads on start-up
processes custom tags – fill form elements
from beans, display internationalized
request/session
messages
Form
Form
Bean
2
Other
Bean
1
View
Layer
looks up path to
creates
passes control
to
appropriate
determine action/ returns
form bean
relevant action to handle
forward
Strutsconfig.
xml
Mappings
•
•
•
•
path
action
[form bean]
[forwards]
Action1
.java
Action2
.java
Action3
.java
gets
data with
to display
interacts
lower(adds
layersto- beans in
request/session)…or
saves data from
acts as adaptor between
beans
rules
HTTPvia
andbusiness
layers below
Business
Business
Bean 1
Bean 2
Business Data
Action4
.java
Business
Bean 3
Business
Logic
Layer
Data
Layer