Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP Exception Handling in Same Page

 
Victor M. Pereira
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if it's possible but I would like to handle exceptions in the same page of the form to avoid creating new pages just for exception handling.

For example I have business method: findByLastVisit(Date date);

That returns data or throws an exception if no data is found. On my jsp I have:


I would like to display those exceptions in the same place as errorInForm. How can I do this? Do I have to modify the web.xml to add the same page for exceptions?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An exception means something has gone really really wrong. You should not be using exceptions for business errors such as form validation failures.
 
Victor M. Pereira
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well is not really for form validation, I couldn't think of a better way of automatized messages.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exceptions shouldn't be used for messaging. That's like using nuclear weapons to crack walnuts. Exceptions are for horrible conditions like null pointer exceptions and illegal states.

It looks as if you are using Spring. Why aren't you using Spring's messaging?
 
Victor M. Pereira
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean the resourcebundle of spring or by JMS?
 
Ken Rimple
author
Ranch Hand
Posts: 63
Mac OS X Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My quick answer - AJAXIZE the form post, and review the result. Come up with a JSON equivalent of an error list and convert it when received in your backend. There are probably built-in resources here for some of this. Roo includes a thin Spring Javascript livrary that wraps dojo and can convert form posts to ajax, but so does native Dojo and jQuery, etc.

So it could be (psuedo code here):

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken Rimple wrote:My quick answer - AJAXIZE the form post, and review the result. Come up with a JSON equivalent of an error list and convert it when received in your backend.

Great minds think alike. This is exactly what I do. I didn't know if Spring had something built in -- I use native jQuery with the Forms and Validation plugins.

The best part is that, in the case of errors, no context is lost that needs to be painstakingly re-created when using a traditional full-page form post.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic