SlideShare a Scribd company logo
Thomas Tu
thomas.tu@ehealth-china.com
Agenda
 Struts 2 Architecture & Request Processing
 Action Mapping
 Action Context & Data Binding
 Interceptors
 Result Types

 Initialization & Configuration
 Struts 2 in EHI Software Architecture
*1 action mapping
*2 interceptors
*3 action & result

1
2
3

ActionContext
Agenda
 Struts 2 Architecture & Request Processing
 Action Mapping
 Action Context & Data Binding
 Interceptors
 Result Types

 Initialization & Configuration
 Struts 2 in EHI Software Architecture
Action Mapping – find the right action/method
Task: url + parameters  namespace, action, method
 How to do?
 namespace: url
 actionName: url < special
parameter
 methodName: special
parameter < dynamic
method, config < default

Related codes:
DefaultActionMapper.java
ActionMapping mapping = new ActionMapping();
……
parseNameAndNamespace(uri, mapping, configMan
ager);
handleSpecialParameters(request, mapping);
if (mapping.getName() == null) {
return null;
}
parseActionName(mapping);
return mapping;
Agenda
 Struts 2 Architecture & Request Processing
 Action Mapping
 Action Context & Data Binding
 Interceptors
 Result Types

 Initialization & Configuration
 Struts 2 in EHI Software Architecture
OGNL Context (ActionContext)

OGNL

Action Context

Value Stack (OGNL root)
|___Action
|___other objects…

#parameters
#request
#session
#application
#attr (searches p, r, s, then a)
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")
Agenda
 Struts 2 Architecture & Request Processing
 Action Mapping
 Action Context & Data Binding
 Interceptors
 Result Types

 Initialization & Configuration
 Struts 2 in EHI Software Architecture
Interceptors
request

– do common jobs before or after

exceptionInterceptor

… loginInterceptor
… sessionValidator
…

action
result

response
error output
Interceptors in EHI
from struts.xml
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception">
<param name="logEnabled">true</param>
<param name="logLevel">WARN</param>

</interceptor-ref>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="setupContexts"/>
<interceptor-ref name="https"/>
<interceptor-ref name="sessionTimeout"/>
<interceptor-ref name="login"/>
<interceptor-ref name="alliance"/>
<interceptor-ref name="ehiServlet"/>
<interceptor-ref name="webAppContext"/>

<interceptor-ref name="checkbox"/>
<interceptor-ref name="params"/>
<interceptor-ref name="sessionValidator"/>
<interceptor-ref name="prepare"/>
<interceptor-ref
name="changeSessionInactiveInterval"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel</param>

</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel</param>

</interceptor-ref>
<interceptor-ref name="ab"/>
<interceptor-ref name="tracking"/>
</interceptor-stack>
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
Agenda
 Struts 2 Architecture & Request Processing
 Action Mapping
 Action Context & Data Binding
 Interceptors
 Result Types

 Initialization & Configuration
 Struts 2 in EHI Software Architecture
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
Agenda
 Struts 2 Architecture & Request Processing
 Action Mapping
 Action Context & Data Binding
 Interceptors
 Result Types

 Initialization & Configuration
 Struts 2 in EHI Software Architecture
Configuration
In struts-default.xml:
<bean class="com.opensymphony.xwork2.ObjectFactory" name="xwork" />
<bean type="com.opensymphony.xwork2.ObjectFactory" name="struts" class=“…”/>
<bean type="com.opensymphony.xwork2.ActionProxyFactory" name="xwork" class=“…"/>
<bean type="com.opensymphony.xwork2.ActionProxyFactory" name="struts" class=“…"/>

In struts-plugin.xml (spring plugin):
<bean type="com.opensymphony.xwork2.ObjectFactory" name="spring"
class="org.apache.struts2.spring.StrutsSpringObjectFactory" />

In struts.xml
<constant name="struts.ui.templateDir" value="/WEB-INF/tags"/>
<constant name="struts.ui.theme" value="simple"/>
<constant name="struts.objectFactory" value="spring"/>
<constant name="struts.action.extension" value=""/>
<constant name="struts.mapper.class" value="com.ehi.struts.core.ActionMapper"/>

In struts/xwork codes:
@Inject
public void setContainer(Container container) {
…
@Inject(StrutsConstants.STRUTS_ACTION_EXTENSION)
public void setExtensions(String extensions) {
Initialization
In web.xml:
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

When starting app server:
StrutsPrepareAndExecuteFilter  Dispatcher.init()

When incoming request (in StrutsPrepareAndExecuteFilter.doFilter(req, resp) ):
prepare.setEncodingAndLocale(request, response);
prepare.createActionContext(request, response);
prepare.assignDispatcherToThread();
if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
chain.doFilter(request, response);
} else {
request = prepare.wrapRequest(request);
ActionMapping mapping = prepare.findActionMapping(request, response, true);
if (mapping == null) {
boolean handled = execute.executeStaticResourceRequest(request, response);
if (!handled) {
chain.doFilter(request, response);
}
} else {
execute.executeAction(request, response, mapping);
}
}
Agenda
 Struts 2 Architecture & Request Processing
 Action Mapping
 Action Context & Data Binding
 Interceptors
 Result Types

 Initialization & Configuration
 Struts 2 in EHI Software Architecture
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
Thanks!

More Related Content

Similar to Sturts 2 in EHI

Krazykoder struts2 intro
Krazykoder struts2 introKrazykoder struts2 intro
Krazykoder struts2 intro
Krazy Koder
 
What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2
Santosh Singh Paliwal
 
Common Global Parameters
Common Global ParametersCommon Global Parameters
Common Global Parameters
Roman Agaev
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
Nicole Gomez
 

Similar to Sturts 2 in EHI (20)

Struts Ppt 1
Struts Ppt 1Struts Ppt 1
Struts Ppt 1
 
Struts 2
Struts 2Struts 2
Struts 2
 
Krazykoder struts2 intro
Krazykoder struts2 introKrazykoder struts2 intro
Krazykoder struts2 intro
 
What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2
 
Django
DjangoDjango
Django
 
Agile Data Science
Agile Data ScienceAgile Data Science
Agile Data Science
 
Agile Data Science 2.0
Agile Data Science 2.0Agile Data Science 2.0
Agile Data Science 2.0
 
Relevance trilogy may dream be with you! (dec17)
Relevance trilogy  may dream be with you! (dec17)Relevance trilogy  may dream be with you! (dec17)
Relevance trilogy may dream be with you! (dec17)
 
Agile Data Science 2.0
Agile Data Science 2.0Agile Data Science 2.0
Agile Data Science 2.0
 
Struts2 - 101
Struts2 - 101Struts2 - 101
Struts2 - 101
 
Agile Data Science 2.0
Agile Data Science 2.0Agile Data Science 2.0
Agile Data Science 2.0
 
Agile Data Science 2.0 - Big Data Science Meetup
Agile Data Science 2.0 - Big Data Science MeetupAgile Data Science 2.0 - Big Data Science Meetup
Agile Data Science 2.0 - Big Data Science Meetup
 
IRJET- Automatic Database Schema Generator
IRJET- Automatic Database Schema GeneratorIRJET- Automatic Database Schema Generator
IRJET- Automatic Database Schema Generator
 
Common Global Parameters
Common Global ParametersCommon Global Parameters
Common Global Parameters
 
Introduction to struts
Introduction to strutsIntroduction to struts
Introduction to struts
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
 
Struts by l n rao
Struts by l n raoStruts by l n rao
Struts by l n rao
 
Struts2 in a nutshell
Struts2 in a nutshellStruts2 in a nutshell
Struts2 in a nutshell
 
Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Recently uploaded (20)

Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 

Sturts 2 in EHI

  • 2. Agenda  Struts 2 Architecture & Request Processing  Action Mapping  Action Context & Data Binding  Interceptors  Result Types  Initialization & Configuration  Struts 2 in EHI Software Architecture
  • 3. *1 action mapping *2 interceptors *3 action & result 1 2 3 ActionContext
  • 4. Agenda  Struts 2 Architecture & Request Processing  Action Mapping  Action Context & Data Binding  Interceptors  Result Types  Initialization & Configuration  Struts 2 in EHI Software Architecture
  • 5. Action Mapping – find the right action/method Task: url + parameters  namespace, action, method  How to do?  namespace: url  actionName: url < special parameter  methodName: special parameter < dynamic method, config < default Related codes: DefaultActionMapper.java ActionMapping mapping = new ActionMapping(); …… parseNameAndNamespace(uri, mapping, configMan ager); handleSpecialParameters(request, mapping); if (mapping.getName() == null) { return null; } parseActionName(mapping); return mapping;
  • 6. Agenda  Struts 2 Architecture & Request Processing  Action Mapping  Action Context & Data Binding  Interceptors  Result Types  Initialization & Configuration  Struts 2 in EHI Software Architecture
  • 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")
  • 9. Agenda  Struts 2 Architecture & Request Processing  Action Mapping  Action Context & Data Binding  Interceptors  Result Types  Initialization & Configuration  Struts 2 in EHI Software Architecture
  • 10. Interceptors request – do common jobs before or after exceptionInterceptor … loginInterceptor … sessionValidator … action result response error output
  • 11. Interceptors in EHI from struts.xml <interceptor-stack name="defaultStack"> <interceptor-ref name="exception"> <param name="logEnabled">true</param> <param name="logLevel">WARN</param> </interceptor-ref> <interceptor-ref name="servletConfig"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="setupContexts"/> <interceptor-ref name="https"/> <interceptor-ref name="sessionTimeout"/> <interceptor-ref name="login"/> <interceptor-ref name="alliance"/> <interceptor-ref name="ehiServlet"/> <interceptor-ref name="webAppContext"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="params"/> <interceptor-ref name="sessionValidator"/> <interceptor-ref name="prepare"/> <interceptor-ref name="changeSessionInactiveInterval"/> <interceptor-ref name="chain"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel</param> </interceptor-ref> <interceptor-ref name="ab"/> <interceptor-ref name="tracking"/> </interceptor-stack>
  • 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
  • 13. Agenda  Struts 2 Architecture & Request Processing  Action Mapping  Action Context & Data Binding  Interceptors  Result Types  Initialization & Configuration  Struts 2 in EHI Software Architecture
  • 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
  • 15. Agenda  Struts 2 Architecture & Request Processing  Action Mapping  Action Context & Data Binding  Interceptors  Result Types  Initialization & Configuration  Struts 2 in EHI Software Architecture
  • 16. Configuration In struts-default.xml: <bean class="com.opensymphony.xwork2.ObjectFactory" name="xwork" /> <bean type="com.opensymphony.xwork2.ObjectFactory" name="struts" class=“…”/> <bean type="com.opensymphony.xwork2.ActionProxyFactory" name="xwork" class=“…"/> <bean type="com.opensymphony.xwork2.ActionProxyFactory" name="struts" class=“…"/> In struts-plugin.xml (spring plugin): <bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" /> In struts.xml <constant name="struts.ui.templateDir" value="/WEB-INF/tags"/> <constant name="struts.ui.theme" value="simple"/> <constant name="struts.objectFactory" value="spring"/> <constant name="struts.action.extension" value=""/> <constant name="struts.mapper.class" value="com.ehi.struts.core.ActionMapper"/> In struts/xwork codes: @Inject public void setContainer(Container container) { … @Inject(StrutsConstants.STRUTS_ACTION_EXTENSION) public void setExtensions(String extensions) {
  • 17. Initialization In web.xml: <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> When starting app server: StrutsPrepareAndExecuteFilter  Dispatcher.init() When incoming request (in StrutsPrepareAndExecuteFilter.doFilter(req, resp) ): prepare.setEncodingAndLocale(request, response); prepare.createActionContext(request, response); prepare.assignDispatcherToThread(); if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) { chain.doFilter(request, response); } else { request = prepare.wrapRequest(request); ActionMapping mapping = prepare.findActionMapping(request, response, true); if (mapping == null) { boolean handled = execute.executeStaticResourceRequest(request, response); if (!handled) { chain.doFilter(request, response); } } else { execute.executeAction(request, response, mapping); } }
  • 18. Agenda  Struts 2 Architecture & Request Processing  Action Mapping  Action Context & Data Binding  Interceptors  Result Types  Initialization & Configuration  Struts 2 in EHI Software Architecture
  • 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

Editor's Notes

  1. 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.