Tom Joad
Tom Joad

Reputation: 585

Handling exceptions to JSP page from @Service class

I am doing Spring MVC, but I don't know how to work with unhandled exceptions. I want to show some custom error text to the user when the exception is thrown in code. I don't want to setup a 404 page in the web.xml, because then all exceptions go to one page and that isn't informative to the user.

This might be a silly example, but I have other code inside my @Service-classes which is throwing exceptions.

@Controller
public class Control {

    @RequestMapping(value = "something", method = RequestMethod.POST)
    public String Form(HttpServletRequest request) {
            String name = request.getParameter("name");

            Validate v = Validate();
            v.giveAName(name);
    }

@Service
public class Validate {

    public void giveAName(String name){
    if (name==null) {
        throw new MyException("Name is null");
      } catch (MyException e) {
        e.getMessage();
    //How do I here pass that getMessage value ("Name is null") back to Control-class
    //(and from there to JSP)?
    }      
}

public class MyException extends Exception {
  public MyException(String message) {
    super(message);
  }
}

Upvotes: 0

Views: 1586

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 148880

Spring MVC has two clean and neat ways to deal with exceptions coming from the service layer.

  • first at the controller level, you can annotate a method with @ExceptionHandler. This method will be called for any Exception it declares, can get access to the request, response or session as any other controller method and can return a view name or a ModelAndView
  • next at the global level (shared by all controllers) you can setup a HandlerExceptionResolver. You should notice that by default, DispatcherServlet registers a DefaultHandlerExceptionResolver to deal with certain standard Spring MVC exceptions.

You will find more details in the Spring reference manual

EDIT : example using @ExceptionHandler :

In service layer you simply throw your exceptions :

@Service
// as MyException is not a RunTimeException it must be declared
public class Validate throws MyException {

    public void giveAName(String name){
    if (name==null) {
        throw new MyException("Name is null"); // not caught locally ...
    }
}

In your controller, you put an exception handler that will catch the exception:

@ExceptionHandler(MyException.class)
public ModelAndView handler(MyException me){
                ModelAndView model = new ModelAndView("index");
                model.addObject("error", me.getMessage());

                return model;
        }
}

Upvotes: 1

xdhmoore
xdhmoore

Reputation: 9876

I would probably extend MyException from RuntimeException instead of Exception, and then put your try/catch block in the controller. Then you can catch MyException there. That said, usually error messages in exceptions aren't the best thing to display to a user, so you may want to do something with spring custom error messages.

Upvotes: 1

Related Questions