Skip to content

Commit 1c13d2c

Browse files
committed
formatted according to eugen's formatter.
1 parent d100f84 commit 1c13d2c

3 files changed

Lines changed: 43 additions & 45 deletions

File tree

‎spring-mvc-xml/src/main/java/com/baeldung/spring/controller/ErrorController.java‎

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,52 @@
22

33
import javax.servlet.http.HttpServletRequest;
44

5+
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
56
import org.springframework.stereotype.Controller;
67
import org.springframework.web.bind.annotation.RequestMapping;
78
import org.springframework.web.bind.annotation.RequestMethod;
89
import org.springframework.web.servlet.ModelAndView;
910

1011
@Controller
1112
public class ErrorController {
13+
14+
@RequestMapping(value = "500Error", method = RequestMethod.GET)
15+
public void throwRuntimeException() {
16+
throw new NullPointerException("Throwing a null pointer exception");
17+
}
1218

13-
@RequestMapping(value = "500Error", method = RequestMethod.GET)
14-
public void throwRuntimeException() {
15-
throw new NullPointerException("Throwing a null pointer exception");
16-
}
19+
@RequestMapping(value = "errors", method = RequestMethod.GET)
20+
public ModelAndView renderErrorPage(HttpServletRequest httpRequest) {
21+
ModelAndView errorPage = new ModelAndView("errorPage");
22+
String errorMsg = "";
23+
int httpErrorCode = getErrorCode(httpRequest);
1724

18-
@RequestMapping(value = "errors", method = RequestMethod.GET)
19-
public ModelAndView renderErrorPages(HttpServletRequest httpRequest) {
20-
ModelAndView errorPage = new ModelAndView("errorPage");
21-
String errorMsg = "";
22-
int httpErrorCode = getErrorCode(httpRequest);
25+
switch (httpErrorCode) {
26+
case 400: {
27+
errorMsg = "Http Error Code : 400 . Bad Request";
28+
break;
29+
}
30+
case 401: {
31+
errorMsg = "Http Error Code : 401. Unauthorized";
32+
break;
33+
}
34+
case 404: {
35+
errorMsg = "Http Error Code : 404. Resource not found";
36+
break;
37+
}
38+
// Handle other 4xx error codes.
39+
case 500: {
40+
errorMsg = "Http Error Code : 500. Internal Server Error";
41+
break;
42+
}
43+
// Handle other 5xx error codes.
44+
}
45+
errorPage.addObject("errorMsg", errorMsg);
46+
return errorPage;
47+
}
2348

24-
switch (httpErrorCode) {
25-
case 400: {
26-
errorMsg = "Http Error Code : 400 . Bad Request";
27-
break;
28-
}
29-
case 401: {
30-
errorMsg = "Http Error Code : 401. Unauthorized";
31-
break;
32-
}
33-
case 404: {
34-
errorMsg = "Http Error Code : 404. Resource not found";
35-
break;
36-
}
37-
// Handle other 4xx error codes.
38-
case 500: {
39-
errorMsg = "Http Error Code : 500. Internal Server Error";
40-
break;
41-
}
42-
// Handle other 5xx error codes.
43-
}
44-
errorPage.addObject("errorMsg", errorMsg);
45-
return errorPage;
46-
}
47-
48-
private int getErrorCode(HttpServletRequest httpRequest) {
49-
return (Integer) httpRequest.getAttribute("javax.servlet.error.status_code");
50-
}
49+
private int getErrorCode(HttpServletRequest httpRequest) {
50+
return (Integer) httpRequest
51+
.getAttribute("javax.servlet.error.status_code");
52+
}
5153
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
2-
<%@ page session="false" %>
1+
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
2+
<%@ page session="false"%>
33
<html>
44
<head>
5-
<title>Home</title>
5+
<title>Home</title>
66
</head>
77
<body>
8-
<h1>
9-
${errorMsg}
10-
</h1>
8+
<h1>${errorMsg}</h1>
119

1210
</body>
1311
</html>

‎spring-mvc-xml/src/main/webapp/WEB-INF/web.xml‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
</welcome-file-list>
4545

4646
<error-page>
47-
<location>
48-
/errors
49-
</location>
47+
<location>/errors</location>
5048
</error-page>
5149
</web-app>

0 commit comments

Comments
 (0)