简体   繁体   中英

Implement the custom error in spring boot

I want to implement the custom error page in spring boot. In the app the id is the primary key so when the id is not given it transfers the request to a page but i want that the app will display the index page with the error message on the index page. the entity class is

@Entity
public class Employee  {
    @Id
    //@GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    //@Id
    public void setName(String name) {
        this.name = name;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }


    private String name;
    private String phone;
}

the index page is

<!DOCTYPE html>
<html lang="en">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>index page</title>
</head>
<body>
<form action="#" th:action="@{/result}" th:object="${employee}" method="post">
    <p>Id: <input type="text" th:field="*{id}" /></p>
    <p>name: <input type="text" th:field="*{name}" /></p>
    <p>phone: <input type="text" th:field="*{phone}" /></p>
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>


</body>
</html>

I want to show the error on the index page like the id field change the color or there is a message right besides the id column.

You can do this via javascript/ jQuery, just writing a small function that it is listening on the DOM of the page. So on click of the submit you can show an error message, show a modal, or get redirected to another generic error page.

If you would like the server to make this control, spring provides amazing apis for validation, search on google for BindingResult Spring.

The idea behind it is very simple. You controller will get an object from the form and you can validate it via using some annotation on it.

I found this example: https://www.journaldev.com/2668/spring-validation-example-mvc-validator

Hope it helps

I suggest to develop a customError controller like this example: and affect your error html page on it ( you can do it differently)

@Controller
public class CustomErrorController implements  ErrorController{

private static final String PATH = "/error";
private static final org.slf4j.Logger log = 
LoggerFactory.getLogger(CustomErrorController.class);
 String script_error_page = "<html lang='en' app='HubMonitor'><head><meta 
   charset='UTF-8'><meta name='viewport' content='width=device-width, initial- 
  scale=1'><title>Hub Monitor</title><head>......your html code ....</head></html>";

@RequestMapping("/error")
@ResponseBody
public String handleError(HttpServletRequest request) {

    return script_error_page;
}

and you can add to your basic controller the test condition, it means when you want to be redirected to the error content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM