Struts2

Struts 2 Namespace Configuration Example

<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;<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>Struts 2 Namespace is a new concept to handle the multiple modules by given a namespace to each module&period; In addition&comma; it can used to avoid conflicts between same action names located at different modules&period;<&sol;p>&NewLine;<p>See this picture to understand how a URL match to Struts 2 action namespace&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&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;namespace&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<&sol;div>&NewLine;<h2><b>1&period; Namespace configuration-<&sol;b><&sol;h2>&NewLine;<p>Let go through a Struts 2 namescape configuration example to know how it match with URL and folder&period;<br &sol;>&NewLine;The package &&num;8220&semi;login&&num;8221&semi; will not affect the result&comma; just give a meaningful name&period;<br &sol;>&NewLine;<b>struts&period;xml<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8" &quest;&gt&semi; &NewLine;&lt&semi;&excl;DOCTYPE struts PUBLIC &NewLine; "-&sol;&sol;Apache Software Foundation&sol;&sol;DTD Struts Configuration 2&period;0&sol;&sol;EN" &NewLine; "http&colon;&sol;&sol;struts&period;apache&period;org&sol;dtds&sol;struts-2&period;0&period;dtd"&gt&semi; &NewLine; &NewLine;&lt&semi;struts&gt&semi; &NewLine; &lt&semi;constant name&equals;"struts&period;enable&period;DynamicMethodInvocation" value&equals;"false" &sol;&gt&semi; &NewLine; &lt&semi;constant name&equals;"struts&period;devMode" value&equals;"false" &sol;&gt&semi; &NewLine; &lt&semi;constant name&equals;"struts&period;custom&period;i18n&period;resources" value&equals;"myapp" &sol;&gt&semi; &NewLine; &NewLine; &lt&semi;package name&equals;"default" extends&equals;"struts-default" namespace&equals;"&sol;"&gt&semi; &NewLine; &lt&semi;action name&equals;"welcome" class&equals;"com&period;dineshonjava&period;struts2&period;login&period;LoginAction"&gt&semi; &NewLine; &lt&semi;result name&equals;"success"&gt&semi;&sol;Welcome&period;jsp&lt&semi;&sol;result&gt&semi; &NewLine; &lt&semi;result name&equals;"error"&gt&semi;&sol;Login&period;jsp&lt&semi;&sol;result&gt&semi; &NewLine; &lt&semi;&sol;action&gt&semi; &NewLine; &lt&semi;&sol;package&gt&semi; &NewLine; &NewLine; &lt&semi;package name&equals;"login" extends&equals;"struts-default" namespace&equals;"&sol;login"&gt&semi; &NewLine; &lt&semi;action name&equals;"welcome" class&equals;"com&period;dineshonjava&period;struts2&period;login&period;LoginAction"&gt&semi; &NewLine; &lt&semi;result name&equals;"success"&gt&semi;&sol;Welcome&period;jsp&lt&semi;&sol;result&gt&semi; &NewLine; &lt&semi;result name&equals;"error"&gt&semi;&sol;Login&period;jsp&lt&semi;&sol;result&gt&semi; &NewLine; &lt&semi;&sol;action&gt&semi; &NewLine; &lt&semi;&sol;package&gt&semi; &NewLine; &NewLine; &lt&semi;package name&equals;"welocome" extends&equals;"struts-default" namespace&equals;"&sol;welcome"&gt&semi; &NewLine; &lt&semi;action name&equals;"welcome" class&equals;"com&period;dineshonjava&period;struts2&period;login&period;LoginAction"&gt&semi; &NewLine; &lt&semi;result name&equals;"success"&gt&semi;&sol;Welcome&period;jsp&lt&semi;&sol;result&gt&semi; &NewLine; &lt&semi;result name&equals;"error"&gt&semi;&sol;Login&period;jsp&lt&semi;&sol;result&gt&semi; &NewLine; &lt&semi;&sol;action&gt&semi; &NewLine; &lt&semi;&sol;package&gt&semi; &NewLine;&lt&semi;&sol;struts&gt&semi; &NewLine;<&sol;pre>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<&sol;div>&NewLine;<h2><b>2&period; JSP View Pages<&sol;b><&sol;h2>&NewLine;<p>3 JSP view pages with same file name but locate at different modules&period;<br &sol;>&NewLine;<b>1&period; View Root namespace<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&percnt;&commat; page contentType&equals;"text&sol;html&semi; charset&equals;UTF-8"&percnt;&gt&semi; &NewLine;&lt&semi;&percnt;&commat; taglib prefix&equals;"s" uri&equals;"&sol;struts-tags"&percnt;&gt&semi; &NewLine;&lt&semi;&excl;DOCTYPE html PUBLIC "-&sol;&sol;W3C&sol;&sol;DTD HTML 4&period;01 Transitional&sol;&sol;EN" "http&colon;&sol;&sol;www&period;w3&period;org&sol;TR&sol;html4&sol;loose&period;dtd"&gt&semi; &NewLine;&lt&semi;html&gt&semi; &NewLine;&lt&semi;head&gt&semi; &NewLine;&lt&semi;meta http-equiv&equals;"Content-Type" content&equals;"text&sol;html&semi; charset&equals;ISO-8859-1"&gt&semi; &NewLine;&lt&semi;title&gt&semi;Welcome&lt&semi;&sol;title&gt&semi; &NewLine;&lt&semi;&sol;head&gt&semi; &NewLine; &NewLine;&lt&semi;body&gt&semi; &NewLine; &lt&semi;h2&gt&semi;Default namespace&lt&semi;&sol;h2&gt&semi; &NewLine; &lt&semi;h2&gt&semi;Hello Welcome &comma; &lt&semi;s&colon;property value&equals;"username" &sol;&gt&semi;&period;&period;&period;&excl; Dineshonjava&period;com&lt&semi;&sol;h2&gt&semi; &NewLine; &NewLine;&lt&semi;&sol;body&gt&semi; &NewLine;&lt&semi;&sol;html&gt&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p><b>URL &colon; <i>http&colon;&sol;&sol;localhost&colon;8080&sol;doj&sol;welcome&period;action<&sol;i><&sol;b><br &sol;>&NewLine;<b>Will match the root namespace&period;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;package name&equals;"default" extends&equals;"struts-default" namespace&equals;"&sol;"&gt&semi; &NewLine; &lt&semi;action name&equals;"welcome" class&equals;"com&period;dineshonjava&period;struts2&period;login&period;LoginAction"&gt&semi; &NewLine; &lt&semi;result name&equals;"success"&gt&semi;&sol;Welcome&period;jsp&lt&semi;&sol;result&gt&semi; &NewLine; &lt&semi;result name&equals;"error"&gt&semi;&sol;Login&period;jsp&lt&semi;&sol;result&gt&semi; &NewLine; &lt&semi;&sol;action&gt&semi; &NewLine; &lt&semi;&sol;package&gt&semi; &NewLine;<&sol;pre>&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;11&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>2&period; View login namespace<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&percnt;&commat; page contentType&equals;"text&sol;html&semi; charset&equals;UTF-8"&percnt;&gt&semi; &NewLine;&lt&semi;&percnt;&commat; taglib prefix&equals;"s" uri&equals;"&sol;struts-tags"&percnt;&gt&semi; &NewLine;&lt&semi;&excl;DOCTYPE html PUBLIC "-&sol;&sol;W3C&sol;&sol;DTD HTML 4&period;01 Transitional&sol;&sol;EN" "http&colon;&sol;&sol;www&period;w3&period;org&sol;TR&sol;html4&sol;loose&period;dtd"&gt&semi; &NewLine;&lt&semi;html&gt&semi; &NewLine;&lt&semi;head&gt&semi; &NewLine;&lt&semi;meta http-equiv&equals;"Content-Type" content&equals;"text&sol;html&semi; charset&equals;ISO-8859-1"&gt&semi; &NewLine;&lt&semi;title&gt&semi;Welcome&lt&semi;&sol;title&gt&semi; &NewLine;&lt&semi;&sol;head&gt&semi; &NewLine; &NewLine;&lt&semi;body&gt&semi; &NewLine; &lt&semi;h2&gt&semi;login namespace&lt&semi;&sol;h2&gt&semi; &NewLine; &lt&semi;h2&gt&semi;Hello Welcome &comma; &lt&semi;s&colon;property value&equals;"username" &sol;&gt&semi;&period;&period;&period;&excl; Dineshonjava&period;com&lt&semi;&sol;h2&gt&semi; &NewLine; &NewLine;&lt&semi;&sol;body&gt&semi; &NewLine;&lt&semi;&sol;html&gt&semi; &NewLine;<&sol;pre>&NewLine;<p><b>URL &colon; <i>http&colon;&sol;&sol;localhost&colon;8080&sol;doj&sol;login&sol;welcome&period;action<&sol;i><&sol;b><br &sol;>&NewLine;<b>Will match the login namespace&period;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;package name&equals;"login" extends&equals;"struts-default" namespace&equals;"&sol;login"&gt&semi; &NewLine; &lt&semi;action name&equals;"login" class&equals;"com&period;dineshonjava&period;struts2&period;login&period;LoginAction"&gt&semi; &NewLine; &lt&semi;result name&equals;"success"&gt&semi;&sol;Welcome&period;jsp&lt&semi;&sol;result&gt&semi; &NewLine; &lt&semi;result name&equals;"error"&gt&semi;&sol;Login&period;jsp&lt&semi;&sol;result&gt&semi; &NewLine; &lt&semi;&sol;action&gt&semi; &NewLine; &lt&semi;&sol;package&gt&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&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;22&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>3&period; View welcome namespace-<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&percnt;&commat; page contentType&equals;"text&sol;html&semi; charset&equals;UTF-8"&percnt;&gt&semi; &NewLine;&lt&semi;&percnt;&commat; taglib prefix&equals;"s" uri&equals;"&sol;struts-tags"&percnt;&gt&semi; &NewLine;&lt&semi;&excl;DOCTYPE html PUBLIC "-&sol;&sol;W3C&sol;&sol;DTD HTML 4&period;01 Transitional&sol;&sol;EN" "http&colon;&sol;&sol;www&period;w3&period;org&sol;TR&sol;html4&sol;loose&period;dtd"&gt&semi; &NewLine;&lt&semi;html&gt&semi; &NewLine;&lt&semi;head&gt&semi; &NewLine;&lt&semi;meta http-equiv&equals;"Content-Type" content&equals;"text&sol;html&semi; charset&equals;ISO-8859-1"&gt&semi; &NewLine;&lt&semi;title&gt&semi;Welcome&lt&semi;&sol;title&gt&semi; &NewLine;&lt&semi;&sol;head&gt&semi; &NewLine; &NewLine;&lt&semi;body&gt&semi; &NewLine; &lt&semi;h2&gt&semi;welcome namespace&lt&semi;&sol;h2&gt&semi; &NewLine; &lt&semi;h2&gt&semi;Hello Welcome &comma; &lt&semi;s&colon;property value&equals;"username" &sol;&gt&semi;&period;&period;&period;&excl; Dineshonjava&period;com&lt&semi;&sol;h2&gt&semi; &NewLine; &NewLine;&lt&semi;&sol;body&gt&semi; &NewLine;&lt&semi;&sol;html&gt&semi; &NewLine;<&sol;pre>&NewLine;<p><b>URL &colon; <i>http&colon;&sol;&sol;localhost&colon;8080&sol;doj&sol;welcome&sol;welcome&period;action<&sol;i><&sol;b><br &sol;>&NewLine;<b>Will match the welcome namespace&period;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;package name&equals;"login" extends&equals;"struts-default" namespace&equals;"&sol;welcome"&gt&semi; &NewLine; &lt&semi;action name&equals;"welcome" class&equals;"com&period;dineshonjava&period;struts2&period;login&period;LoginAction"&gt&semi; &NewLine; &lt&semi;result name&equals;"success"&gt&semi;&sol;Welcome&period;jsp&lt&semi;&sol;result&gt&semi; &NewLine; &lt&semi;result name&equals;"error"&gt&semi;&sol;Login&period;jsp&lt&semi;&sol;result&gt&semi; &NewLine; &lt&semi;&sol;action&gt&semi; &NewLine; &lt&semi;&sol;package&gt&semi; &NewLine;<&sol;pre>&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;33&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h1>Reference<&sol;h1>&NewLine;<ol>&NewLine;<li><a style&equals;"text-decoration&colon; underline&semi;" href&equals;"http&colon;&sol;&sol;struts&period;apache&period;org&sol;2&period;0&period;14&sol;docs&sol;namespace-configuration&period;html" target&equals;"&lowbar;blank" rel&equals;"noopener">Struts 2 Namespace Configuration Reference<&sol;a><&sol;li>&NewLine;<&sol;ol>&NewLine;<p>&nbsp&semi;<&sol;p>&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;struts-2-configuration-file&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;a-tag-and-business-service&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&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;struts-2-configuration-file&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;a-tag-and-business-service&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; '392'&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