Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 2b46a050

Přidáno uživatelem Petr Urban před téměř 3 roky(ů)

#25 created CustomErrorController.java and own templates for http status codes such as 403, 404, 500 and general error template

Zobrazit rozdíly:

src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/exception/CustomErrorController.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.exception;
2

  
3
import org.slf4j.Logger;
4
import org.slf4j.LoggerFactory;
5
import org.springframework.boot.web.servlet.error.ErrorController;
6
import org.springframework.http.HttpStatus;
7
import org.springframework.stereotype.Controller;
8
import org.springframework.web.bind.annotation.RequestMapping;
9
import org.springframework.web.bind.annotation.ResponseBody;
10

  
11
import javax.servlet.RequestDispatcher;
12
import javax.servlet.http.HttpServletRequest;
13

  
14
@Controller
15
public class CustomErrorController implements ErrorController {
16

  
17
    private static final Logger LOGGER = LoggerFactory.getLogger(CustomErrorController.class);
18

  
19

  
20
    @RequestMapping("/error")
21
    public String handleError(HttpServletRequest request) {
22

  
23
        String errorPage = "error";
24

  
25
        Object statusCode = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
26

  
27
        if (statusCode != null) {
28
            int code = Integer.parseInt(statusCode.toString());
29

  
30
            if (code == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
31
                return "error/500";
32
            } else if (code == HttpStatus.NOT_FOUND.value()) {
33
                return "error/404";
34
            } else if (code == HttpStatus.UNAUTHORIZED.value()) {
35
                return "error/403";
36
            }
37
        }
38

  
39
        return errorPage;
40
    }
41

  
42
    @Override
43
    public String getErrorPath() {
44
        return "/error";
45
    }
46

  
47
}
src/main/resources/application.properties
12 12

  
13 13
# admin account password:
14 14
account.user.password=
15

  
16
# custom error pages
17
server.error.whitelabel.enabled=false
src/main/webapp/WEB-INF/templates/error.html
1
<!DOCTYPE HTML>
2
<html xmlns:th="http://www.thymeleaf.org">
3
<head>
4
    <title>SPAWn – SPADe Web Interface - An Uncaught Error</title>
5
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
6
    <meta name="viewport" content="width=device-width, initial-scale=1">
7
    <link rel="stylesheet" th:href="@{/resources/css/style.css}">
8
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
9
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
10
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
11

  
12
    <script th:src="@{/resources/js/core.js}"></script>
13
</head>
14
<body>
15

  
16
    <div th:replace="fragments/navbar :: navBar"></div>
17

  
18
    <h1>Uncaught error</h1>
19

  
20
</body>
21
</html>
src/main/webapp/WEB-INF/templates/error/403.html
1
<!DOCTYPE HTML>
2
<html xmlns:th="http://www.thymeleaf.org">
3
<head>
4
    <title>SPAWn – SPADe Web Interface - Not Authorized</title>
5
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
6
    <meta name="viewport" content="width=device-width, initial-scale=1">
7
    <link rel="stylesheet" th:href="@{/resources/css/style.css}">
8
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
9
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
10
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
11

  
12
    <script th:src="@{/resources/js/core.js}"></script>
13
</head>
14
<body>
15

  
16
    <div th:replace="fragments/navbar :: navBar"></div>
17

  
18
    <h1>Not accessible with your current user role.</h1>
19

  
20
</body>
21
</html>
src/main/webapp/WEB-INF/templates/error/404.html
1
<!DOCTYPE HTML>
2
<html xmlns:th="http://www.thymeleaf.org">
3
<head>
4
    <title>SPAWn – SPADe Web Interface - Not Found</title>
5
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
6
    <meta name="viewport" content="width=device-width, initial-scale=1">
7
    <link rel="stylesheet" th:href="@{/resources/css/style.css}">
8
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
9
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
10
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
11

  
12
    <script th:src="@{/resources/js/core.js}"></script>
13
</head>
14
<body>
15
    <div th:replace="fragments/navbar :: navBar"></div>
16

  
17
    <h1>The page you have requested could not be found</h1>
18

  
19
</body>
20
</html>
src/main/webapp/WEB-INF/templates/error/500.html
1
<!DOCTYPE HTML>
2
<html xmlns:th="http://www.thymeleaf.org">
3
<head>
4
    <title>SPAWn – SPADe Web Interface - Internal Server Error</title>
5
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
6
    <meta name="viewport" content="width=device-width, initial-scale=1">
7
    <link rel="stylesheet" th:href="@{/resources/css/style.css}">
8
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
9
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
10
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
11

  
12
    <script th:src="@{/resources/js/core.js}"></script>
13
</head>
14
<body>
15
    <div th:replace="fragments/navbar :: navBar"></div>
16

  
17
    <h1>We are sorry for the error</h1>
18

  
19
</body>
20
</html>
src/main/webapp/WEB-INF/templates/fragments/navbar.html
9 9
                <li class="nav-item active">
10 10
                    <a class="nav-link" th:href="@{/}">Detect</a>
11 11
                </li>
12
                <li th:if="${not #lists.isEmpty(query.antiPatterns)}" class="nav-item active">
13
                    <a class="nav-link" th:href="@{/configuration}">Configuration</a>
12
                <li th:switch="${query} == null OR ${#lists.isEmpty(query.antiPatterns)}" class="nav-item active">
13
                    <a th:case="false" class="nav-link" th:href="@{/configuration}">Configuration</a>
14
                    <a th:case="true" class="nav-link" href="#" style="color: red">Configuration</a>
14 15
                </li>
15 16
                <li class="nav-item active">
16 17
                    <a class="nav-link" th:href="@{/about}">About</a>

Také k dispozici: Unified diff