formatted according to eugen's formatter.
This commit is contained in:
parent
d100f84ea9
commit
1c13d2ccf9
|
@ -2,6 +2,7 @@ package com.baeldung.spring.controller;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
@ -9,43 +10,44 @@ import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class ErrorController {
|
public class ErrorController {
|
||||||
|
|
||||||
|
@RequestMapping(value = "500Error", method = RequestMethod.GET)
|
||||||
|
public void throwRuntimeException() {
|
||||||
|
throw new NullPointerException("Throwing a null pointer exception");
|
||||||
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "500Error", method = RequestMethod.GET)
|
@RequestMapping(value = "errors", method = RequestMethod.GET)
|
||||||
public void throwRuntimeException() {
|
public ModelAndView renderErrorPage(HttpServletRequest httpRequest) {
|
||||||
throw new NullPointerException("Throwing a null pointer exception");
|
ModelAndView errorPage = new ModelAndView("errorPage");
|
||||||
}
|
String errorMsg = "";
|
||||||
|
int httpErrorCode = getErrorCode(httpRequest);
|
||||||
|
|
||||||
@RequestMapping(value = "errors", method = RequestMethod.GET)
|
switch (httpErrorCode) {
|
||||||
public ModelAndView renderErrorPages(HttpServletRequest httpRequest) {
|
case 400: {
|
||||||
ModelAndView errorPage = new ModelAndView("errorPage");
|
errorMsg = "Http Error Code : 400 . Bad Request";
|
||||||
String errorMsg = "";
|
break;
|
||||||
int httpErrorCode = getErrorCode(httpRequest);
|
}
|
||||||
|
case 401: {
|
||||||
|
errorMsg = "Http Error Code : 401. Unauthorized";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 404: {
|
||||||
|
errorMsg = "Http Error Code : 404. Resource not found";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Handle other 4xx error codes.
|
||||||
|
case 500: {
|
||||||
|
errorMsg = "Http Error Code : 500. Internal Server Error";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Handle other 5xx error codes.
|
||||||
|
}
|
||||||
|
errorPage.addObject("errorMsg", errorMsg);
|
||||||
|
return errorPage;
|
||||||
|
}
|
||||||
|
|
||||||
switch (httpErrorCode) {
|
private int getErrorCode(HttpServletRequest httpRequest) {
|
||||||
case 400: {
|
return (Integer) httpRequest
|
||||||
errorMsg = "Http Error Code : 400 . Bad Request";
|
.getAttribute("javax.servlet.error.status_code");
|
||||||
break;
|
}
|
||||||
}
|
|
||||||
case 401: {
|
|
||||||
errorMsg = "Http Error Code : 401. Unauthorized";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 404: {
|
|
||||||
errorMsg = "Http Error Code : 404. Resource not found";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// Handle other 4xx error codes.
|
|
||||||
case 500: {
|
|
||||||
errorMsg = "Http Error Code : 500. Internal Server Error";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// Handle other 5xx error codes.
|
|
||||||
}
|
|
||||||
errorPage.addObject("errorMsg", errorMsg);
|
|
||||||
return errorPage;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int getErrorCode(HttpServletRequest httpRequest) {
|
|
||||||
return (Integer) httpRequest.getAttribute("javax.servlet.error.status_code");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
|
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
|
||||||
<%@ page session="false" %>
|
<%@ page session="false"%>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Home</title>
|
<title>Home</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>
|
<h1>${errorMsg}</h1>
|
||||||
${errorMsg}
|
|
||||||
</h1>
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -44,8 +44,6 @@
|
||||||
</welcome-file-list>
|
</welcome-file-list>
|
||||||
|
|
||||||
<error-page>
|
<error-page>
|
||||||
<location>
|
<location>/errors</location>
|
||||||
/errors
|
|
||||||
</location>
|
|
||||||
</error-page>
|
</error-page>
|
||||||
</web-app>
|
</web-app>
|
Loading…
Reference in New Issue