Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom Error Messages with Global Exception Mappings in Struts 2 1

Status
Not open for further replies.

troyarch

MIS
Mar 12, 2003
29
0
0
US
I am developing with Struts 2 and using global-exception-mappings in the struts config file to handle all HibernateOptimisticLockingFailureException exceptions that occur. I am also setting result="input" in order to render back to the same screen instead of a global error screen.


<global-exception-mappings>
<exception-mapping exception="org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException" result="input"/>
</global-exception-mappings>


This works correctly, but now I need to figure out how to set a customized error message when HibernateOptimisticLockingFailureException occurs which be displayed near the top of the screen. In addition, I need to be able to set custom error messages for each type of exception that is handled in the struts.xml file.

Are there any suggestions on how this could be done?
 
I figured out how to do this.
1. Create a custom Exception Mapping Interceptor class that extends ExceptionMappingInterceptor. Make this class will override the publishExeption method so that it creates an ActionError if a certain exception is thrown.
2. Add a global-exception-mapping in your struts config file and set result="input".

<global-exception-mappings>
<exception-mapping exception="org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException" result="input"/>
</global-exception-mappings>

3. Add your interceptor to the interceptor-stack
4. print out the ActionError in your JSP
 
I found that I am losing user data when the screen is rendered with the error message so I need to figure that out now. If I find a fix I'll add it to this post.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top