Struts2

Struts 2 Exception Handling

<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;<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>In this chapter we will learn about the exception handling in struts2 framework&period; If any exception occur between the code it redirected to the dedicated page whatever you define or map into the configuration file of struts2&period;<&sol;p>&NewLine;<p>In our web application&comma; there might occur exception&period; To overcome this problem&comma; struts 2 provides a mechanism of global exception handling where we can display a global result to the user&period;<&sol;p>&NewLine;<h2><b>Example of Exception Handling in struts 2 &&num;8211&semi;<&sol;b><&sol;h2>&NewLine;<p>Struts makes the exception handling easy by the use of the &&num;8220&semi;<b>exception<&sol;b>&&num;8221&semi; interceptor&period; The &&num;8220&semi;exception&&num;8221&semi; interceptor is included as part of the default stack&comma; so you don&&num;8217&semi;t have to do anything extra to configure it&period; It is available out-of-the-box ready for you to use&period; Let us see a simple Hello World example with some modification in<i><b> LoginAction&period;java<&sol;b><&sol;i> file&period; Here we purposely introduced a <i><b>NullPointerException <&sol;b><&sol;i>in our <i><b>LoginAction <&sol;b><&sol;i>action code&period;<&sol;p>&NewLine;<&sol;div>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;struts2&period;login&semi; &NewLine; &NewLine;import com&period;opensymphony&period;xwork2&period;ActionSupport&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;SuppressWarnings&lpar;"serial"&rpar; &NewLine;public class LoginAction extends ActionSupport&lbrace; &NewLine; private String username&semi; &NewLine; private String password&semi; &NewLine; private String userid&semi; &NewLine; &NewLine; public String execute&lpar;&rpar;&lbrace; &NewLine; String x &equals; null&semi; &NewLine; x &equals; x&period;substring&lpar;0&rpar;&semi; &NewLine; return SUCCESS&semi; &NewLine; &rcub; &NewLine; &NewLine; public String getUsername&lpar;&rpar; &lbrace; &NewLine; return username&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setUsername&lpar;String username&rpar; &lbrace; &NewLine; this&period;username &equals; username&semi; &NewLine; &rcub; &NewLine; &NewLine; public String getPassword&lpar;&rpar; &lbrace; &NewLine; return password&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setPassword&lpar;String password&rpar; &lbrace; &NewLine; this&period;password &equals; password&semi; &NewLine; &rcub; &NewLine; &NewLine; public String getUserid&lpar;&rpar; &lbrace; &NewLine; return userid&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setUserid&lpar;String userid&rpar; &lbrace; &NewLine; this&period;userid &equals; userid&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<p><b>Let us keep the content of Login&period;jsp as follows&colon;<&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; &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;Struts 2 - Login Application &vert; dineshonjava&period;com&lt&semi;&sol;title&gt&semi; &NewLine;&lt&semi;&sol;head&gt&semi; &NewLine;&lt&semi;body&gt&semi; &NewLine;&lt&semi;h2&gt&semi;Struts 2 - Login Application&lt&semi;&sol;h2&gt&semi; &NewLine;&lt&semi;font color&equals;"red"&gt&semi;&lt&semi;s&colon;actionerror &sol;&gt&semi;&lt&semi;&sol;font&gt&semi; &NewLine;&lt&semi;s&colon;form action&equals;"welcome&period;action" method&equals;"post"&gt&semi; &NewLine; &lt&semi;s&colon;textfield name&equals;"username" key&equals;"label&period;username" size&equals;"20" &sol;&gt&semi; &NewLine; &lt&semi;s&colon;password name&equals;"password" key&equals;"label&period;password" size&equals;"20" &sol;&gt&semi; &NewLine; &lt&semi;s&colon;submit method&equals;"execute" key&equals;"label&period;login" align&equals;"center" &sol;&gt&semi; &NewLine;&lt&semi;&sol;s&colon;form&gt&semi; &NewLine;&lt&semi;&sol;body&gt&semi; &NewLine;&lt&semi;&sol;html&gt&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p><b>Let us keep the content of Welcome&period;jsp as follows&colon;<&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;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>Your <b>struts&period;xml<&sol;b> should look like&colon;<&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;&sol;struts&gt&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p>Now right click on the project name and click <b>Export &gt&semi; WAR<&sol;b> File to create a War file&period; Then deploy this WAR in the Tomcat&&num;8217&semi;s webapps directory&period; Finally&comma; start Tomcat server and try to access<&sol;p>&NewLine;<p><b>URL <i>http&colon;&sol;&sol;localhost&colon;8080&sol;doj&sol;Login&period;jsp&period;<&sol;i><&sol;b><i> <&sol;i><&sol;p>&NewLine;<p>This will give you following screen&colon;<&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;08&sol;22222-3&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<&sol;div>&NewLine;<p><b>Enter a value of user name and password and submit the page&period; You should see the following page&colon;<&sol;b><&sol;p>&NewLine;<p><b>http&colon;&sol;&sol;localhost&colon;8080&sol;doj&sol;welcome&period;action&semi;jsessionid&equals;8043CCAD28F8A538283A322BFF761C11 <&sol;b><&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;08&sol;121111-1024x575&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<&sol;div>&NewLine;<p>As shown in the above example&comma; the default exception interceptor does a great job of handling the exception&period; Let us now create a dedicated error page for our Exception&period; Create a file called <b>globalerror&period;jsp<&sol;b> with the following contents&colon;<br &sol;>&NewLine;<i><b>globalresult&period;jsp<&sol;b><&sol;i><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&percnt;&commat; page language&equals;"java" contentType&equals;"text&sol;html&semi; charset&equals;ISO-8859-1" &NewLine; pageEncoding&equals;"ISO-8859-1"&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" &NewLine;"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;title&gt&semi;&lt&semi;&sol;title&gt&semi; &NewLine;&lt&semi;&sol;head&gt&semi; &NewLine;&lt&semi;body&gt&semi; &NewLine; This is my custom error page &NewLine; &lt&semi;form&gt&semi; &NewLine; &lt&semi;input type&equals;"button" value&equals;"back" onclick&equals;"history&period;back&lpar;&rpar;"&gt&semi; &NewLine; &lt&semi;&sol;form&gt&semi; &NewLine;&lt&semi;&sol;body&gt&semi; &NewLine;&lt&semi;&sol;html&gt&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p>For exception handling&comma; we specify the global-result and global-exception-mappings in the struts&period;xml file&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;global-results&gt&semi; &NewLine; &lt&semi;result name&equals;"myresult"&gt&semi;&sol;globalresult&period;jsp&lt&semi;&sol;result&gt&semi; &NewLine; &lt&semi;&sol;global-results&gt&semi; &NewLine; &NewLine; &lt&semi;global-exception-mappings&gt&semi; &NewLine; &lt&semi;exception-mapping result&equals;"myresult" exception&equals;"java&period;lang&period;Exception"&gt&semi;&lt&semi;&sol;exception-mapping&gt&semi; &NewLine; &lt&semi;&sol;global-exception-mappings&gt&semi; &NewLine; &NewLine; &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;&sol;struts&gt&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p>The <b>global-results<&sol;b> sub-element of package specifies the global-result for this package&period;<&sol;p>&NewLine;<p>The result sub-element of <b>global-result <&sol;b>specifies the result page that will be rendered to the user as a view&period;<&sol;p>&NewLine;<p>The global-exception-mappings sub-element of package specifies the exception mapping for all the actions of this package&period;<&sol;p>&NewLine;<p>The exception-mapping sub-element of global-exception-mapping maps the given result for the given exception type&period; In this example&comma; we are using the Exception which the parent of many exception classes such as <b>IOException<&sol;b>&comma; <b>ArithmeticException <&sol;b>etc&period; It means if any exception occurs&comma; specified result will be invoked&period;<&sol;p>&NewLine;<p><b><i>Note<&sol;i>&colon; Notice that <i>global-results<&sol;i> must be specified before <i>global-exception-mappings<&sol;i> as we are using global result in<i> global-exception-mappings<&sol;i>&period;<&sol;b><&sol;p>&NewLine;<&sol;div>&NewLine;<p>As shown in the example above&comma; now we have configured Struts to use the dedicated<i><b> globalerror&period;jsp<&sol;b><&sol;i> for the <b>NullPointerException<&sol;b>&period; If you rerun the program now&comma; you shall now see the following output&colon;<&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;08&sol;3333-5&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<h2><b>Download SourceCode<&sol;b><br &sol;>&NewLine;<b><a href&equals;"https&colon;&sol;&sol;sites&period;google&period;com&sol;a&sol;dineshonjava&period;com&sol;dineshonjava&sol;dineshonjava&sol;Struts2Exception&period;zip&quest;attredirects&equals;0&amp&semi;d&equals;1" target&equals;"&lowbar;blank" rel&equals;"noopener">Struts2Exception&period;zip<&sol;a><&sol;b><&sol;h2>&NewLine;<p>&nbsp&semi;<&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;themes-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;annotations-in-struts2-with-example&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;themes-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;annotations-in-struts2-with-example&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; '379'&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