Skip to content

Commit 245ee16

Browse files
committed
combine themeleaf error pages
Fix #1119
1 parent 2b7bd1b commit 245ee16

File tree

8 files changed

+98
-240
lines changed

8 files changed

+98
-240
lines changed

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 SiteUrl.COMMON_ERROR_PAGE;
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 SiteUrl.COMMON_ERROR_PAGE;
7275
}
7376

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ public final class SiteUrl {
3939
public static final String SITE_EVENTS_PAGE = "/site/events";
4040
public static final String CSP_REPORTS_HANDLER = "/site/csp/reports";
4141

42-
public static final String FORBIDDEN_PAGE = "/error/403";
42+
public static final String FORBIDDEN_PAGE = "/error/status-code";
4343
public static final String NOT_FOUND_PAGE = "/error/404";
4444
public static final String INTERNAL_ERROR_PAGE = "/error/500";
45+
public static final String COMMON_ERROR_PAGE = "/error/status-code";
4546

4647
static final String ROBOTS_TXT = "/robots.txt";
4748
static final String SITEMAP_XML = "/sitemap.xml";

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,9 @@ t_register = Register
100100
t_403_title = 403: forbidden
101101
t_403_description = Forbidden
102102

103-
# error/404.html
103+
# error/status-code.html
104104
t_404_title = 404: page not found
105105
t_404_description = Requested page{0}not found
106-
107-
# error/500.html
108106
t_500_title = 500: internal server error
109107
t_500_description = Internal{0}server error
110108

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,9 @@ t_register = Зарегистрироваться
100100
t_403_title = 403: доступ запрещён
101101
t_403_description = Доступ запрещён
102102

103-
# error/404.html
103+
# error/status-code.html
104104
t_404_title = 404: страница не найдена
105105
t_404_description = Запрашиваемая страница{0}не найдена
106-
107-
# error/500.html
108106
t_500_title = 500: внутренняя ошибка сервера
109107
t_500_description = Внутренняя{0}ошибка сервера
110108

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

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!DOCTYPE html>
2+
<html lang="en" th:lang="${#locale.language == 'ru' ? 'ru' : 'en'}"
3+
xmlns="http://www.w3.org/1999/xhtml"
4+
xmlns:th="http://www.thymeleaf.org"
5+
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
6+
<head th:with="httpStatus=${T(org.springframework.http.HttpStatus).valueOf(#response.status)}">
7+
<meta charset="utf-8" />
8+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
9+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
10+
<meta name="robots" content="noindex" />
11+
<th:block th:switch="${#response.status}">
12+
<title th:case="'403'" th:text="#{t_403_title}">403: forbidden</title>
13+
<title th:case="'404'" th:text="#{t_404_title}">404: forbidden</title>
14+
<title th:case="'500'" th:text="#{t_500_title}">500: forbidden</title>
15+
</th:block>
16+
<link rel="shortcut icon" type="image/x-icon" href="../../../favicon.ico" th:href="${FAVICON_ICO}" />
17+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" th:href="${BOOTSTRAP_CSS}" />
18+
<link rel="stylesheet" href="../../static/styles/main.css" th:href="${MAIN_CSS}" />
19+
</head>
20+
<body th:with="httpStatus=${T(org.springframework.http.HttpStatus).valueOf(#response.status)}">
21+
<div class="container-fluid">
22+
<div class="row" id="header">
23+
<div id="logo" class="col-sm-10">
24+
<a href="../site/index.html" th:href="'/'" th:text="#{t_my_stamps}">My stamps</a>
25+
</div>
26+
27+
<div id="user_bar" class="col-sm-2">
28+
<ul class="list-unstyled">
29+
<!--/*/
30+
<li sec:authorize="isAuthenticated()">
31+
<i class="glyphicon glyphicon-user"></i>
32+
<a sec:authentication="principal.userName"
33+
href="../collection/info.html"
34+
title="Open my collection"
35+
th:title="#{t_open_my_collection}"
36+
th:href="@{${INFO_COLLECTION_PAGE}(slug=${#authentication.principal.userCollectionSlug})}">
37+
John Doe
38+
</a>
39+
</li>
40+
/*/-->
41+
<li sec:authorize="isAnonymous()">
42+
<a href="../account/auth.html" th:href="@{${AUTHENTICATION_PAGE}}" th:text="#{t_enter}">Sign in</a>
43+
</li>
44+
<!--/*/
45+
<li sec:authorize="isAuthenticated()">
46+
<form id="logout-form" method="get" action="../site/index.html" class="no-margin" th:method="post" th:action="@{${LOGOUT_PAGE}}">
47+
<i class="glyphicon glyphicon-share"></i>&nbsp;<input type="submit" value="Sign out" class="btn btn-link no-padding" th:value="#{t_logout}" />
48+
</form>
49+
</li>
50+
/*/-->
51+
<li sec:authorize="isAnonymous()">
52+
<a href="../account/register.html" th:href="@{${REGISTRATION_PAGE}}" th:text="#{t_register}">Register</a>
53+
</li>
54+
</ul>
55+
</div>
56+
</div>
57+
58+
<div class="row text-center">
59+
<div id="content" class="col-sm-12">
60+
<h1 id="error-code" th:text="${#response.status}">
61+
403
62+
</h1>
63+
<th:block th:switch="${#response.status}">
64+
<h4 id="error-msg" th:case="'403'" th:text="#{t_403_description}">
65+
Forbidden
66+
</h4>
67+
<h4 id="error-msg" th:case="'404'" th:utext="#{t_404_description('&lt;br /&gt;')}">
68+
Requested page<br />not found
69+
</h4>
70+
<h4 id="error-msg" th:case="'500'" th:utext="#{t_500_description('&lt;br /&gt;')}">
71+
Internal<br />server error
72+
</h4>
73+
</th:block>
74+
</div>
75+
</div>
76+
77+
<div class="row">
78+
<footer class="col-sm-12 text-right">
79+
<i class="glyphicon glyphicon-envelope"></i>
80+
<a href="mailto:slava.semushin@gmail.com" title="Write e-mail" th:href="|mailto:#{t_site_author_email}|" th:title="#{t_write_email}" th:text="#{t_site_author_name}">Slava Semushin</a>, 2009-2019
81+
</footer>
82+
</div>
83+
</div>
84+
85+
<!-- Placed at the end of the document so the pages load faster -->
86+
<script src="http://yandex.st/jquery/1.9.1/jquery.min.js" th:src="${JQUERY_JS}"></script>
87+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" th:src="${BOOTSTRAP_JS}"></script>
88+
</body>
89+
</html>

0 commit comments

Comments
 (0)