Reputation: 11
I am new to these technologies and I am using the following frameworks to develop an application:
I am implementing all business logic in Spring so in case any exception has occurred I will show the custom message to the end user.
Could you please explain me how to develop an exception handling functionality in these technologies?
Upvotes: 1
Views: 1003
Reputation: 23587
As per my understanding the best approach is to define some predefined Exceptions for your Business layer and throws these Exception back to your Action classes.
S2 provides a number of way to handle those exception and display them to the user, here are few of them
Using the Struts 2 framework you can specify in the struts.xml how the framework should handle uncaught exceptions. The handling logic can apply to all actions (global exception handling) or to a specific action. Let's first discuss how to enable global exception handling.
<global-exception-mappings>
<exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException" result="securityerror" />
<exception-mapping exception="java.lang.Exception" result="error" />
</global-exception-mappings>
<global-results>
<result name="securityerror">/securityerror.jsp</result>
<result name="error">/error.jsp</result>
</global-results>
Even if you want a fine level control you are free to configure exception handling at per Action basis
<action name="actionspecificexception" class="org.apache.struts.register.action.Register" method="throwSecurityException">
<exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException"
result="login" />
<result>/register.jsp</result>
<result name="login">/login.jsp</result>
</action>
Its your preference, how you want to configure them.For details refer to the doc
You have even the option to access Exception details from Value-Stack.By default, the ExceptionMappingInterceptor
adds the following values to the Value Stack:
and here is the way to access those object in JSP
<s:property value="%{exception.message}"/>
<s:property value="%{exceptionStack}"/>
For details refer to the details
Upvotes: 1