Struts2

Struts 2 OGNL, ActionInvocation, ActionContext

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>The <b>Object Graph Navigation Language<&sol;b> &lpar;OGNL&rpar; is an expression language&period; It simplifies the accessibility of data stored in the <b>ActionContext<&sol;b>&period;<&sol;p>&NewLine;<p>The struts framework sets the <b>ValueStack<&sol;b> as the root object of OGNL&period; Notice that action object is pushed into the <b>ValueStack<&sol;b>&period; We can direct access the action property&period;<&sol;p>&NewLine;<div class&equals;"codeblock">&NewLine;<pre class&equals;"highlight">&lt&semi;s&colon;property value&equals;"username"&sol;&gt&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p>Here&comma; username is the property key<br &sol;>&NewLine;&period;<br &sol;>&NewLine;The struts framework places other objects in <b>ActionContext <&sol;b>also e&period;g&period; map representing the <b>request<&sol;b>&comma; <b>session<&sol;b>&comma; <b>application<&sol;b> scopes&period;<&sol;p>&NewLine;<p>To get these values i&period;e&period; not the action property&comma; we need to use <b>&num; <&sol;b> notation&period; For example to get the data from session scope&comma; we need to use<b> &num;session<&sol;b> as given in the following example&colon;<&sol;p>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<div class&equals;"codeblock">&NewLine;<pre class&equals;"highlight">&lt&semi;s&colon;property name&equals;"&num;session&period;username"&sol;&gt&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p>&lpar;or&rpar;<&sol;p>&NewLine;<div class&equals;"codeblock">&NewLine;<pre class&equals;"highlight">&lt&semi;s&colon;property name&equals;"&num;session&lbrack;'username'&rsqb;"&sol;&gt&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<h2><b id&equals;"h1"><br &sol;>&NewLine;Struts 2 <i>ActionInvocation<&sol;i><&sol;b><&sol;h2>&NewLine;<p>The <b>ActionInvocation<&sol;b> represents the execution state of an action&period; It holds the action and interceptors objects&period;<&sol;p>&NewLine;<p>The struts framework provides <b>ActionInvocation interface<&sol;b> to deal with <b>ActionInvocation<&sol;b>&period; It provides many methods&comma; some of them can be used to get the instance of <b>ValueStack<&sol;b>&comma; <b>ActionProxy<&sol;b>&comma; <b>ActionContext<&sol;b>&comma; Result etc&period;<&sol;p>&NewLine;<h2 id&equals;"h3tag">Methods of <i>ActionInvocation <&sol;i>Interface<&sol;h2>&NewLine;<p>The commonly used methods of ActionInvocation interface are as follows&colon;<br &sol;>&NewLine;1&rpar;<b>public ActionContext getInvocationContext&lpar;&rpar;<&sol;b> returns the ActionContext object associated with the ActionInvocation&period;<br &sol;>&NewLine;2&rpar;<b>public ActionProxy getProxy&lpar;&rpar;<&sol;b> returns the ActionProxy instance holding this ActionInvocation&period;<br &sol;>&NewLine;3&rpar;<b>public ValueStack getStack&lpar;&rpar;<&sol;b> returns the instance of ValueStack&period;<br &sol;>&NewLine;4&rpar;<b>public Action getAction&lpar;&rpar;<&sol;b> returns the instance of Action associated with this ActionInvocation&period;<br &sol;>&NewLine;5&rpar;<b>public void invoke&lpar;&rpar;<&sol;b> invokes the next resource in processing this ActionInvocation&period;<br &sol;>&NewLine;6&rpar;<b>public Result getResult&lpar;&rpar;<&sol;b> returns the instance of Result&period;<&sol;p>&NewLine;<h2><b id&equals;"h1"><br &sol;>&NewLine;Struts 2 <i>ActionContex<&sol;i>&&num;8211&semi; <&sol;b><&sol;h2>&NewLine;<p>The <b>ActionContext <&sol;b>is a container of objects in which action is executed&period; The values stored in the <b>ActionContext <&sol;b>are unique per thread &lpar;i&period;e&period; <b>ThreadLocal<&sol;b>&rpar;&period; So we don&&num;8217&semi;t need to make our action thread safe&period;<&sol;p>&NewLine;<p>We can get the reference of <b>ActionContext<&sol;b> by calling the <b>getContext&lpar;&rpar; <&sol;b>method of <b>ActionContext <&sol;b>class&period; It is a static factory method&period; <b>For example&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"codeblock">&NewLine;<pre class&equals;"highlight">ActionContext context &equals; ActionContext&period;getContext&lpar;&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<&sol;div>&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;the-valuestack-in-struts-2&sol;">Previous<&sol;a> &lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;struts-2-baby-step-to-learn&sol;">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;struts-2-actions&sol;">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;the-valuestack-in-struts-2&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;struts-2-actions&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; '389'&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