0

I am new to Spring boot and I am trying to learn by coding a website. I have done a hibernate validation on my web, when I fill every field correctly then every thing goes well but when I leave for example any field null then thymeleaf gives me an error page and don't shows me the error fields on my HTML page.

Here is my code:

POM.xml

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE

<groupId>dentistry.iliriani</groupId>
<artifactId>Dentistry</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Dentistry</name>
<description>Demo project for Spring Boot</description>


<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>3.0.11.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>4.3.1</version>
    </dependency>

    <dependency>
        <groupId>org.webjars.bower</groupId>
        <artifactId>jquery</artifactId>
        <version>3.3.1</version>
    </dependency>

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>webjars-locator</artifactId>
        <version>0.36</version>
    </dependency>


    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <!-- <version>5.1.5.RELEASE</version> -->
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <!-- <version>1.3.0.RELEASE</version> -->
    </dependency>

</dependencies>

<properties>
    <java.version>1.8</java.version>
</properties>

Entity class

package dentistry.model;

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.Size;

@Entity public class User {

@Id
@NotEmpty
@Column(unique=true)
private String id;
@NotEmpty
private String name;
@NotEmpty
@Size(min=3)
private String pass;
private String role;

public User() {}

public User(String id, String name, String pass, String role) {
    super();
    this.id = id;
    this.name = name;
    this.pass = pass;
    this.role = role;
}


public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPass() {
    return pass;
}
public void setPass(String pass) {
    this.pass = pass;
}
public String getRole() {
    return role;
}
public void setRole(String role) {
    this.role = role;
}

}

Controller:

package dentistry.controllers;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import dentistry.login.LoginService; import dentistry.model.User;

@Controller public class RegisterController implements WebMvcConfigurer{

@Autowired
LoginService loginService;

@GetMapping("/register")
public String registerForm(Model model) {

    model.addAttribute("new_user", new User());
    return "newUser";
}

@PostMapping("/register")
public String registerUser(@Valid User user, BindingResult bindingResult, Model model) {

    if(bindingResult.hasErrors()) {return "newUser";}


    if(loginService.isUserPresent(user.getId())){
        model.addAttribute("exist", true);
        return "newUser";
    }

    loginService.addUser(user);
    return "intro";
}

}

HTML page:

Register

<div layout:fragment="content" class="container myspace">
<div class="alert alert-info" th:if="${exist}">
<p class="text text-center">
Ky perdorues eshte regjistruar me pare!
</p>
</div>
    <div class="jumbotron">
        <h2>Register</h2>
        <br>
        <form action="#" th:action="@{/register}" th:object="${new_user}" method="post">

            <div class="form-group">
                <label for="id">ID</label> <input type="text" th:field="*{id}"
                    id="id" class="form-control" />
                    <div class="text text-danger" th:if="${#fields.hasErrors('id')}" th:errors="*{id}">Id errorr</div>
            </div>

            <div class="form-group">
                <label for="name">Name</label> <input type="text"
                    th:field="*{name}" id="name" class="form-control" />
                    <div class="text text-danger" th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name error</div>
            </div>

            <div class="form-group">
                <label for="pass">Pass</label> <input type="password"
                    th:field="*{pass}" id="pass" class="form-control" />
                    <div class="text text-danger" th:if="${#fields.hasErrors('pass')}" th:errors="*{password}">Pass error</div>
            </div>
            <div class="form-group">
                <label for="role">Role</label> <input type="text"
                    th:field="*{role}" id="role" class="form-control" />
                    <div class="text text-danger" th:if="${#fields.hasErrors('role')}" th:errors="*{role}">Role error</div>
            </div>
            <input type="submit" value="Submit" class="btn-primary">
        </form>
    </div>
</div>

<div th:replace="footer::footer"></div>

And here is the error:

  • This has nothing to do with software recommendation. Why haven't you asked this question on Stack Overflow? – Nino Filiu Apr 13 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.