Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Spring mvc: Exception handling at controller level

In this post, you are going to learn,
         a. What is the behaviour of spring application, when there is no Exception Handling mechanism.
         b. How to customise the exception handling process.
        
What is the behaviour of spring application, when there is no exception handling mechanism.
Let’s try to find out with an example. I written handler method 'getHelloMessage' like below.

@RequestMapping("/welcome")
public String getHelloMessage() {
         boolean flag = true;

         if (flag) {
                  throw new RuntimeException("Throwning blindly");
         }

         return "welcome";
}

As you see the definition of 'getHelloMessage' method, it throw RuntimeException in all the cases. Follow below step-by-step procedure and set up the application first.

HelloWorldController.java
package com.sample.myApp.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorldController {

@RequestMapping("/welcome")
public String getHelloMessage() {
boolean flag = true;

if (flag) {
throw new RuntimeException("Throwning blindly");
}

return "welcome";
}

}


Create welcome.jsp file under WEB-INF/jsp folder.

welcome.jsp
taglib uri="http://www.springframework.org/tags" prefix="spring"%>
page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>



http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
Hello World Spring Web MVC



Hello, Good Morning





Create web.xml, HelloWorld-servlet.xml files under WEB-INF folder.

web.xml
id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

Spring MVC Hello WorldApplication


HelloWorld
org.springframework.web.servlet.DispatcherServlet
1



HelloWorld
/





HelloWorld-servlet.xml
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

/>


base-package="com.sample.myApp" />


class="org.springframework.web.servlet.view.InternalResourceViewResolver">
name="prefix" value="/WEB-INF/jsp/" />
name="suffix" value=".jsp" />




Create index.jsp file under webapp folder.

index.jsp
taglib prefix="form" uri="http://www.springframework.org/tags/form"%>


Welcome Page



Click on below button to get welcome page
method="post" action="/springdemo/welcome"
id="form1">

type="submit" value="getWelcomeMessage"
style="font-size: 18px;" />



Total project structure looks like below.
Run the application on server, you can able to see below kind of screen.


Click on the button ‘getWelcomeMessage’. You can able to see below kind of screen.
As you see above image, it is displaying complete stack trace and server that is used by the application to run. It doesn’t look good and expose security concerns.


How to customize the exception handling process?
By using @ExceptionHandler annotation, we can redirect to some view page on error cases.

For example, I added below method in HelloWorldController class.

         @ExceptionHandler(RuntimeException.class)
         public String handleRuntimeException() {
                  return "runtimeException";
         }
        
Whenever any run time exception thrown, then runtimeException.jsp file is shown to the user.

Update HelloWorldController class like below.

HelloWorldController.java
package com.sample.myApp.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorldController {

@RequestMapping("/welcome")
public String getHelloMessage() {
boolean flag = true;

if (flag) {
throw new RuntimeException("Throwning blindly");
}

return "welcome";
}

@ExceptionHandler(RuntimeException.class)
public String handleRuntimeException() {
return "runtimeException";
}
}


Create 'runtimeException.jsp' file under WEB-INF/jsp folder.

runtimeException.jsp
 page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>



http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
Insert title here


Run time exception occured. Please contact the Administrator.





Run the application on server.
Click on ‘getWelcomeMessage’ button.

You will be redirected to runtimeException.jsp file.




Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Spring mvc: Exception handling at controller level

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×