Skip to content

Commit c844ac1

Browse files
mukeshkphp-coder
authored andcommitted
refactor: combine Thymeleaf error pages into one.
Fix #1119
1 parent 253655a commit c844ac1

File tree

7 files changed

+28
-171
lines changed

7 files changed

+28
-171
lines changed

src/main/java/ru/mystamps/web/config/MvcConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void configureDefaultServletHandling(DefaultServletHandlerConfigurer conf
105105
@Override
106106
public void addViewControllers(ViewControllerRegistry registry) {
107107
registry.addViewController(AccountUrl.AUTHENTICATION_PAGE);
108-
registry.addViewController(SiteUrl.FORBIDDEN_PAGE);
108+
registry.addViewController(SiteUrl.FORBIDDEN_PAGE).setViewName("error/status-code");
109109
}
110110

111111
@Override

src/main/java/ru/mystamps/web/feature/site/ErrorController.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class ErrorController {
3737
private final SiteService siteService;
3838

3939
@RequestMapping(SiteUrl.NOT_FOUND_PAGE)
40-
public void notFound(
40+
public String notFound(
4141
HttpServletRequest request,
4242
@CurrentUser Integer currentUserId,
4343
// CheckStyle: ignore LineLength for next 1 line
@@ -50,10 +50,12 @@ public void notFound(
5050
String method = request.getMethod();
5151

5252
siteService.logAboutAbsentPage(page, method, currentUserId, ip, referer, agent);
53+
54+
return "error/status-code";
5355
}
5456

5557
@RequestMapping(SiteUrl.INTERNAL_ERROR_PAGE)
56-
public void internalError(
58+
public String internalError(
5759
// CheckStyle: ignore LineLength for next 3 lines
5860
@RequestAttribute(name = RequestDispatcher.ERROR_EXCEPTION_TYPE, required = false) Class<?> exceptionType,
5961
@RequestAttribute(name = RequestDispatcher.ERROR_EXCEPTION, required = false) Exception exception,
@@ -69,6 +71,7 @@ public void internalError(
6971
exception
7072
);
7173
}
74+
return "error/status-code";
7275
}
7376

7477
private static Object getNameOrAsIs(Class<?> clazz) {

src/main/resources/ru/mystamps/i18n/Messages.properties

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,11 @@ t_password_again = Retype password
9696
t_activation_key = Activation key
9797
t_register = Register
9898

99-
# error/403.html
99+
# error/status-code.html
100100
t_403_title = 403: forbidden
101101
t_403_description = Forbidden
102-
103-
# error/404.html
104102
t_404_title = 404: page not found
105103
t_404_description = Requested page{0}not found
106-
107-
# error/500.html
108104
t_500_title = 500: internal server error
109105
t_500_description = Internal{0}server error
110106

src/main/resources/ru/mystamps/i18n/Messages_ru.properties

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,11 @@ t_password_again = Пароль (ещё раз)
9696
t_activation_key = Код активации
9797
t_register = Зарегистрироваться
9898

99-
# error/403.html
99+
# error/status-code.html
100100
t_403_title = 403: доступ запрещён
101101
t_403_description = Доступ запрещён
102-
103-
# error/404.html
104102
t_404_title = 404: страница не найдена
105103
t_404_description = Запрашиваемая страница{0}не найдена
106-
107-
# error/500.html
108104
t_500_title = 500: внутренняя ошибка сервера
109105
t_500_description = Внутренняя{0}ошибка сервера
110106

src/main/webapp/WEB-INF/views/error/403.html

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/main/webapp/WEB-INF/views/error/404.html

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/main/webapp/WEB-INF/views/error/500.html renamed to src/main/webapp/WEB-INF/views/error/status-code.html

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
99
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
1010
<meta name="robots" content="noindex" />
11-
<title th:text="#{t_500_title}">500: internal server error</title>
11+
<!--/*/
12+
<th:block th:switch="${#response.status}">
13+
<title th:case="'403'" th:text="#{t_403_title}">403: forbidden</title>
14+
<title th:case="'404'" th:text="#{t_404_title}">404: page not found</title>
15+
/*/-->
16+
<title th:case="'500'" th:text="#{t_500_title}">500: internal server error</title>
17+
<!--/*/ </th:block> /*/-->
1218
<link rel="shortcut icon" type="image/x-icon" href="../../../favicon.ico" th:href="${FAVICON_ICO}" />
1319
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" th:href="${BOOTSTRAP_CSS}" />
1420
<link rel="stylesheet" href="../../static/styles/main.css" th:href="${MAIN_CSS}" />
@@ -53,11 +59,21 @@
5359

5460
<div class="row text-center">
5561
<div id="content" class="col-sm-12">
56-
<h1 id="error-code">
62+
<h1 id="error-code" th:text="${#response.status}">
5763
500
5864
</h1>
59-
<h4 id="error-msg" th:utext="#{t_500_description('&lt;br /&gt;')}">
60-
Internal<br />server error
65+
<h4 id="error-msg" th:switch="${#response.status}">
66+
<!--/*/
67+
<span th:case="'403'" th:text="#{t_403_description}" th:remove="tag">
68+
Forbidden
69+
</span>
70+
<span th:case="'404'" th:utext="#{t_404_description('&lt;br /&gt;')}" th:remove="tag">
71+
Requested page<br />not found
72+
</span>
73+
/*/-->
74+
<span th:case="*" th:utext="#{t_500_description('&lt;br /&gt;')}" th:remove="tag">
75+
Internal<br />server error
76+
</span>
6177
</h4>
6278
</div>
6379
</div>

0 commit comments

Comments
 (0)