Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Spring Spring3.2 Customizing the Default Error Page

Related Tutorials


 
 

Share on Google+Share on Google+

Customizing the Default Error Page

Advertisement
In this section, you will learn about Customizing the Default Error Page in Spring MVC.

Customizing the Default  Error Page

In this section, you will learn about Customizing the Default  Error Page in Spring MVC.

When the response body is empty and the response status have error code, generally an well formatted error page in HTML is provide by the Servlet containers. You can customize the default error page of the container by defining an <error-page> element in web.xml.

Prior to Servlet 3.0, you need to provide specific status code or exception type to map with each <error-page> element. After Servlet 3.0, you don' need to map it with specific status code or exception type. Means the page at defined location can be customized.

<error-page>
	<location>/error</location>
</error-page>

Note that the actual location for the error page can be a JSP page or some other URL within the container including one handled through an @Controller method.

The error page can be customize by setting the status code and reason on the error page as follows :

@Controller
public class CustomErrorController {

	@RequestMapping(value="/error", produces="text/html")
	@ResponseBody
	public Map<String, Object> errorHandler(HttpServletRequest request) {
		
		Map<String, Object> errorMap = new HashMap<String, Object>();
		errorMap.put("status", request.getAttribute("javax.servlet.error.status_code"));
		errorMap.put("reason", request.getAttribute("javax.servlet.error.message"));
		
		return errorMap;
	}

}

or in a JSP:

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
{
	status:<%=request.getAttribute("javax.servlet.error.status_code") %>,
	reason:<%=request.getAttribute("javax.servlet.error.message") %>
}


Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 12, 2013

Related Tutorials

Discuss: Customizing the Default Error Page  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com