Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error messsages in Spring MVC.

 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a jsp page where user selectes a file for download , the request goes to spring Controller where I am doing some file handling, in case FileNotFound exception occurs I want to show a error message on the same jsp page from where the request has been made. I have tried Spring reference but it explains about errors which are related to form bean and bind to fields. I don't have any form bean I want to show controller errors on JSP page. Can any one please help me on this? Thanks in advance!

Regards,
Pawan Chopra.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the easiest way is through configuration.

Adding a SimpleMappingExceptionResolver does the trick




I like that above the best.

or you can do it another way that is in the documentation here;

http://static.springframework.org/spring/docs/2.5.x/reference/mvc.html#mvc-exceptionhandlers

You implement the interface and inside the method you check to see if the Exception passed is that exception and then return the ModelAndView where the view string is the view that is the error page you wanted to display.

Mark
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark, I'm afraid that is not what pawan wants, pawan wants to display error message in the same page.

I suggest to use Validator interface, you will implement a Validator, and inject to the controller.
http://static.springframework.org/spring/docs/2.0.x/reference/validation.html.
 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kengkaj Sathianpantarit wrote:
I suggest to use Validator interface, you will implement a Validator, and inject to the controller.
http://static.springframework.org/spring/docs/2.0.x/reference/validation.html.


You are right I am looking for this thing only thanks!

I got following while searching for this.

http://www.salixalba.uklinux.net/willow/computing/spring.html

The <spring:hasBindErrors> tag is used to access all of the errors on the form, including those which are not tied to specific fields.


Following is the code which I need to use in JSP:



But In my case I am implementing Validator interface in controller only. I do not have any commandBean for controller. the above code is using a commandBean, so can I use this in my controller?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kengkaj Sathianpantarit wrote:Mark, I'm afraid that is not what pawan wants, pawan wants to display error message in the same page.

I suggest to use Validator interface, you will implement a Validator, and inject to the controller.
http://static.springframework.org/spring/docs/2.0.x/reference/validation.html.


Ah, I just saw the Exception, so assumed it was just about exception handling and sending them to an error page, not a validation.

Thanks

Mark
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pawan, the easiest way is using SimpleFormController, and create a command class like the following:

Then in the validator you can check filePath property of FileBean object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic