Struts2

Architecture of Struts 2 Framework

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>Struts and webwork has joined together to develop the Struts 2 Framework&period; Struts 2 Framework is very extensible and elegant for the development of enterprise web application of any size&period; In this section we are going to explain you the architecture of Struts 2 Framework&period;<&sol;p>&NewLine;<p><i><b>Struts 2 Framework &equals;  Struts 1 &plus; webwork<&sol;b><&sol;i><&sol;p>&NewLine;<h2><b>Request Lifecycle in Struts 2 applications<&sol;b><&sol;h2>&NewLine;<p><b>User Sends request&colon;<&sol;b> User sends a request to the server for some resource&period;<&sol;p>&NewLine;<p><b>FilterDispatcher determines the appropriate action&colon;<&sol;b> The <i><b>FilterDispatcher <&sol;b><&sol;i>looks at the request and then determines the appropriate Action&period;<&sol;p>&NewLine;<p><b>Interceptors are applied&colon;<&sol;b> <i><b>Interceptors <&sol;b><&sol;i>configured for applying the common functionality such as workflow&comma; validation&comma; file upload etc&period; are automatically applied to the request&period;<&sol;p>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<p><b>Execution of Action&colon;<&sol;b> Then the action method is executed to perform the database related operations like storing or retrieving data from the database&period;<&sol;p>&NewLine;<p><b>Output rendering&colon;<&sol;b> Then the Result renders the output&period;<&sol;p>&NewLine;<p><b>Return of Request&colon; <&sol;b>Then the request returns through the interceptors in the reverse order&period; The returning request allows us to perform the clean-up or additional processing&period;<&sol;p>&NewLine;<p><b>Display the result to user&colon;<&sol;b> Finally the control is returned to the servlet container&comma; which sends the output to the user browser&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;struts-2-request-cycle&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>Exception Handling&colon;<&sol;b><&sol;p>&NewLine;<p>The Struts 2 Framework allows us to define exception handlers and interceptors&period;<&sol;p>&NewLine;<p><b>Exception Handlers&colon;<&sol;b><br &sol;>&NewLine;Exception handlers allows us to define the exception handling procedure on global and local basis&period; Framework catches the exception and then displays the page of our choice with appropriate message and exception details&period;<&sol;p>&NewLine;<p><b>Interceptors&colon;<&sol;b><br &sol;>&NewLine;The Interceptors are used to specify the <i><b>&&num;8220&semi;request-processing lifecycle&&num;8221&semi;<&sol;b><&sol;i> for an action&period; Interceptors are configured to apply the common functionality like workflow&comma; validation etc&period;&period; to the request&period;<&sol;p>&NewLine;<h2><b>Struts 2 Architecture<&sol;b><&sol;h2>&NewLine;<p>Struts 2 is a very elegant and flexible front controller framework based on many standard technologies like <i><b>Java Filters&comma; Java Beans&comma; ResourceBundles&comma; XML<&sol;b><&sol;i> etc&period;<&sol;p>&NewLine;<p>For the Model&comma; the framework can use any data access technologies like JDBC&comma; EJB&comma; Hibernate etc and for the View&comma; the framework can be integrated with JSP&comma; JTL&comma; JSF&comma; Jakarta Velocity Engine&comma; Templates&comma; PDF&comma; XSLT etc&period;<&sol;p>&NewLine;<p>The following diagram depicts the architecture of Struts 2 Framework and also shows the the initial request goes to the servlet container such as tomcat&comma; which is then passed through standard filer chain&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;Struts2-Architecture&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<h2><b>The filter chain includes&colon;<&sol;b><&sol;h2>&NewLine;<p><b><i>ActionContextCleanUp <&sol;i>filter&colon;<&sol;b><br &sol;>&NewLine;The <i><b>ActionContextCleanUp <&sol;b><&sol;i>filter is optional and it is useful when integration has to be done with other technologies like <i><b>SiteMash <&sol;b><&sol;i>Plugin&period;<&sol;p>&NewLine;<p><i><b>FilterDispatcher&colon;<&sol;b><&sol;i><br &sol;>&NewLine;Next the <i><b>FilterDispatch <&sol;b><&sol;i>is called&comma; which in turn uses the <i><b>ActionMapper <&sol;b><&sol;i>to determine whether to invoke an Action or not&period; If the action is required to be invoked&comma; the <i><b>FilterDispatcher <&sol;b><&sol;i>delegates the control to the <i><b>ActionProxy<&sol;b><&sol;i>&period;<&sol;p>&NewLine;<p><i><b>ActionProxy&colon;<&sol;b><&sol;i><br &sol;>&NewLine;The <i><b>ActionProxy <&sol;b><&sol;i>takes help from <i><b>Configuration Files manager&comma;<&sol;b><&sol;i> which is initialized from the <i><b>struts&period;xml&period;<&sol;b><&sol;i><&sol;p>&NewLine;<p>Then the <i><b>ActionProxy <&sol;b><&sol;i>creates an <i><b>ActionInvocation<&sol;b><&sol;i>&comma; which implements the command pattern&period; The <i><b>  <&sol;b><&sol;i><br &sol;>&NewLine;<i><b>ActionInvocation <&sol;b><&sol;i>process invokes the <i><b>Interceptors <&sol;b><&sol;i>&lpar;if configured&rpar; and then invokes the action&period; The the <i><b>ActionInvocation <&sol;b><&sol;i>looks for proper result&period; Then the result is executed&comma; which involves the rendering of JSP or templates&period;<&sol;p>&NewLine;<p>Then the <i><b>Interceptors <&sol;b><&sol;i>are executed again in reverse order&period; Finally the response returns through the filters configured in <i><b>web&period;xml<&sol;b><&sol;i> file&period; If the <i><b>ActionContextCleanUp <&sol;b><&sol;i>filter is configured&comma; the <i><b>FilterDispatcher <&sol;b><&sol;i>does not clean the <i><b>ThreadLocal <&sol;b><b>ActionContext<&sol;b><&sol;i>&period; If the <i><b>ActionContextCleanUp <&sol;b><&sol;i>filter is not present then the <i><b>FilterDispatcher <&sol;b><&sol;i>will cleanup all the <i><b>ThreadLocals <&sol;b><&sol;i>present&period;<&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;07&sol;introduction-to-struts-2-framework&period;html">Previous<&sol;a> &lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;07&sol;struts-2-baby-step-to-learn&period;html">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;07&sol;setting-up-struts-2-in-eclipse&period;html">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;model-1-and-model-2-mvc-architecture&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;setting-up-struts-2-in-eclipse&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '401'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago