This document discusses Struts 2 architecture and concepts including:
1. Action mapping is used to map URLs and parameters to actions and methods through configuration.
2. The action context and OGNL context provide access to request parameters, session attributes, and other objects through the value stack.
3. Interceptors perform common tasks before and after actions and can prepare parameters and handle exceptions.
4. Result types help produce different response content types like JSON or page redirects by preparing models for templates.
5. Configuration, initialization, and the filter process set up Struts 2 and handle request processing and routing to actions.
7. OGNL Context (ActionContext)
OGNL
Action Context
Value Stack (OGNL root)
|___Action
|___other objects…
#parameters
#request
#session
#application
#attr (searches p, r, s, then a)
8. Data Binding & Type Conversion
Based on OGNL, struts 2 support data binding and accessing as
follows:
From request to action model:
census.zipCode=90001 action. getCensus(). setZipCode(“90001”)
census.requestEffectiveDate=5/1/2011 action. getCensus().
setRequestEffectiveDate(..)
From action context to template (freemarker):
${plan.officeVisitText} productView.getOfficeVisitText()
${plan.pastRate?string("$#,##0.00")}
formatter.format(productView.getPastRate(), "$#,##0.00")
10. Interceptors
request
– do common jobs before or after
exceptionInterceptor
… loginInterceptor
… sessionValidator
…
action
result
response
error output
12. Q: why struts 2 action needn’t be required to
implement any interface?
Why need a interface?
Because it can help core controller to find: action
method name and its parameter requirements
Action method was invoked by reflection, so it can be
any method name
Interceptors can help prepare any parameters declared
by action class
14. Result Types – help produce response content
Q: How struts 2 to support multiple result types?
How to produce response content?
Model + template = content (json, redirect)
Result type can help prepare the required model
19. Struts 2 in EHI Software Architecture
freemarker
jsp
template
template
template
page page
Struts 2
action
action
action
spring
Business
Object
Business
Object
action
screen
screen screen
Helper
Helper
Helper
Business
Object
db
EHIDataBase
resources
Business
Business
Object
Object
Request Processing:In the diagram, an initial request goes to the Servlet container (such as Jetty or Resin) which is passed through a standard filter chain. The chain includes the (optional) ActionContextCleanUp filter, which is useful when integrating technologies such as SiteMeshPlugin. Next, the required FilterDispatcher is called, which in turn consults the ActionMapper to determine if the request should invoke an action.If the ActionMapper determines that an Action should be invoked, the FilterDispatcher delegates control to the ActionProxy. The ActionProxy consults the framework Configuration Files manager (initialized from the struts.xmlfile). Next, the ActionProxy creates an ActionInvocation, which is responsible for the command pattern implementation. This includes invoking any Interceptors (the before clause) in advance of invoking the Action itself.Once the Action returns, the ActionInvocation is responsible for looking up the proper result associated with the Action result code mapped in struts.xml. The result is then executed, which often (but not always, as is the case for Action Chaining) involves a template written in JSP or FreeMarker to be rendered. While rendering, the templates can use the Struts Tags provided by the framework. Some of those components will work with the ActionMapper to render proper URLs for additional requests.Interceptors are executed again (in reverse order, calling the after clause). Finally, the response returns through the filters configured in the web.xml. If the ActionContextCleanUp filter is present, the FilterDispatcher will not clean up the ThreadLocal ActionContext. If the ActionContextCleanUp filter is not present, the FilterDispatcher will cleanup all ThreadLocals.Notes: All objects in this architecture (Actions, Results, Interceptors, and so forth) are created by an ObjectFactory. This ObjectFactory is pluggable. We can provide our own ObjectFactory for any reason that requires knowing when objects in the framework are created. A popular ObjectFactory implementation uses Spring as provided by the Spring Plugin.