2

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.

2
  • Seems this is not related to Spring Boot but only front end form validation. Is that correct? Commented Nov 3, 2018 at 14:48
  • Possible duplicate of Spring Boot and custom 404 error page Commented Nov 3, 2018 at 14:59

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.