Struts Workshop
Presented ByRajkumar Singh
www.rajkrrsingh.blogspot.com
Agenda
www.rajkrrsingh.blogspot.com
Struts 2 overview Request ,Response cycle Struts 2 architecture Struts 1 vs Struts 2 Struts2 Tags Validator framework Interceptor and Custom Interceptor ValueStack, OGNL and Type Converter
MVC
www.rajkrrsingh.blogspot.com
What is Struts2 Framework
www.rajkrrsingh.blogspot.com
A framework tries to automate the common tasks and provides a platform for the users to build applications quickly.
What is Struts2 Framework
www.rajkrrsingh.blogspot.com
Struts 2 is a very well-designed and flexible. An integration of Struts 1 with OpenSymphony Web Works Framework
What is Struts2 Framework
www.rajkrrsingh.blogspot.com
Struts 2 framework implements the Model-View-Controller (MVC) design pattern. –Model- use any data access technologies like JDBC, EJB, Hibernate etc –View- integrated with JSP, JSTL, JSF, Jakarta Velocity, Templates, PDF, XSLT etc. –Controller- based on many standard technologies like Java Filters, Java Beans, ResourceBundles, XML etc.
What is Struts2 Framework
www.rajkrrsingh.blogspot.com
Struts 2 Architecture
www.rajkrrsingh.blogspot.com
Struts 2 Architecture
www.rajkrrsingh.blogspot.com
The Core Components
www.rajkrrsingh.blogspot.com
Controller : Filter dispatcher, by which all the requests to the applications gets filtered.
Interceptors : called after filters, apply common functionalities like validation, conversion etc before Action methods.
Action : has execute methods, usually storing and/or retrieving information from a database for the view,i.e the jsp files.
Result : Render the output as a view component i.e jsp file.
Request Cycle:
www.rajkrrsingh.blogspot.com
1.Container pass request to filter chain. 2.Chain includes the (optional) ActionContextCleanUp filter (useful when integrating technologies such as SiteMesh Plugin) 3.Required FilterDispatcher is called and consults with ActionMapper to determine if the request should invoke an action. 4.If Action should be invoked, the FilterDispatcher delegates control to the ActionProxy. 5.The ActionProxy consults the framework Configuration Files manager (initialized from the struts.xml file). 6.Next, the ActionProxy creates an ActionInvocation, which is responsible for the command pattern implementation. 7.This includes invoking any Interceptors in advance of invoking the Action itself.
Response Cycle:
www.rajkrrsingh.blogspot.com
1. When Action returns, the ActionInvocation=>looking up=>Action result code mapped in struts.xml. 2.The result is then executed, which often involves a template written in JSP to be rendered. 3.While rendering, the templates can use the Struts Tags provided by the framework. 4.Interceptors are executed again in reverse order. 5.If the ActionContextCleanUp filter is present, the FilterDispatcher will not clean up the ThreadLocal ActionContext. (One can use ThreadLocal to keep certain objects/values available throught the thread execution.) 6.If the ActionContextCleanUp filter is not present, the FilterDispatcher will cleanup all ThreadLocals.
Why Struts 2 - Features
www.rajkrrsingh.blogspot.com
Simplified Design–Classes are based on interfaces–Action classes are simple POJOs
Simplified Actions–Any java class with execute() method can be used as an Action class.–Inversion of Control is introduced while developing the action classes•(actions to be neutral to the underlying framework .)
Why Struts 2 - Features
www.rajkrrsingh.blogspot.com
No more Action Forms Annotations introduced Stateful Checkboxes Easy Spring integration Easy plugin AJAX support Better Tag features Both JSP and FreeMarker tags are fully supported.
Difference in Struts 1 and Struts 2
www.rajkrrsingh.blogspot.com
Difference in Struts 1 and Struts 2
www.rajkrrsingh.blogspot.com
Difference in Struts 1 and Struts 2
www.rajkrrsingh.blogspot.com
Structure of URL
www.rajkrrsingh.blogspot.com
Struts.xml
www.rajkrrsingh.blogspot.com
Core configuration file for framework. Can contain multiple package.
<struts> <package name=“base” extends=“struts-default” namespace=“/”> <action name=“. . . .”> <result>. . . . . . . </action> </package> <include file=“hr.xml”/> </struts>
Struts.xml
www.rajkrrsingh.blogspot.com
Struts-default.xml
www.rajkrrsingh.blogspot.com
Shipped in struts2-core.x.x.jar Most of your packages will extends Directly / Indirectly Contains about everything you need. Package from inheritance hierarchy. The Key section are package result-Type interceptors/stack
Interceptors
www.rajkrrsingh.blogspot.com
Responsible for most of the processing done by the framework. Handle most of the fundamental preprocessing and post-processing tasks. Instead of invoking an action directly, this sits between the controller and the action. The invocation of an action is a layered process that always includes the execution of a stack of interceptors prior to and after the actual execution of the action itself.
Struts2 tags
www.rajkrrsingh.blogspot.com
Struts2 support following types of tags A.UI Tags
i.Form Tags ii.Non Form UI Tags iii.Ajax Tags
B.Generic Tags i.Control Tags ii.Data Tags
Struts2 tags
www.rajkrrsingh.blogspot.com
A.UI Tags
render the HTML form and non-form componnets. <textfield> <password> <hidden> <textarea> <radio> <checkbox> <checkboxlist> <select> <combobox> <head> <file>
OGNL
www.rajkrrsingh.blogspot.com
•Powerful expression language that is used to reference and manipulate data on the ValueStack.•Helps in data transfer and type conversion.•Very similar to the JSP Expression Language.•Based on a context and Struts builds an ActionContext map for use with OGNL.•The ActionContext map consists of the following:•application - application scoped variables•session - session scoped variables•root / value stack - all your action variables are stored here•request - request scoped variables•parameters - request parameters•atributes - the attributes stored in page, request, session and application scope
Action/ValueStack/OGNL
www.rajkrrsingh.blogspot.com
QA
www.rajkrrsingh.blogspot.com