JSR303中的视图页面中未显示错误消息,在jsp

时间:2016-03-16 07:54:13

标签: spring twitter-bootstrap jpa bean-validation

我在项目中添加了服务器端验证。并且不知何故来自控制器的错误消息未到达视图页面。 我正在使用:

弹簧 JPA, JSR303用于验证,和 自举

以下是add.jsp的代码

<!DOCTYPE html>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet"
    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script
    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<style>
.error {
    color: #ff0000;
}

.errorblock {
    color: #000;
    background-color: #ffEEEE;
    border: 3px solid #ff0000;
    padding: 8px;
    margin: 16px;
}
</style>

<title>Add User</title>
</head>
<body>

    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="col-md-4">
                    <h2>Add User</h2>
                    <form:form action="user" method="post" role="form"
                        commandName="user">
                        <form:errors path="*" cssClass="errorblock" />

                        <div class="form-group ">
                            <label for="user">User Name:</label>

                            <form:input type="text" class="form-control" path="name"
                                id="name" placeholder="Enter User Name" required="requried"/>

                        </div>


                        <div class="form-group">
                            <label for="email">Email Address:</label>
                            <form:input type="email" path="emailAddress" class="form-control"
                                id="email" placeholder="Enter Email Address" required="requried"/>

                        </div>

                        <div class="form-group">
                            <label for="role">Role:</label>
                            <form:select path="role" class="form-control" id="role" required="requried">
                                <option disabled selected></option>
                                <c:forEach var="role" items="${roles}">
                                    <option value="${role.value}">${role.name}</option>
                                </c:forEach>
                            </form:select>

                        </div>

                        <button type="submit" value="Submit" name="add"
                            class="btn btn-info active">Submit</button>
                        <a href="adminHome" class="btn btn-info" role="button">Back</a>

                    </form:form>


                </div>
                <div class="col-md-4">
                    <div class="col-md-4"></div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

以下是Controller.java的代码

@RequestMapping(value = "/user", params = "add", method = RequestMethod.GET)
    public String getAddUser(Model model) {
        ArrayList<Role> roles = new ArrayList<Role>();

        // From user Add only admin and head of department can be added
        roles.add(Role.ADMIN);
        roles.add(Role.HEAD_OF_DEPARTMENT);
        model.addAttribute("roles", roles);
        model.addAttribute("user", new User());
        return "user/add";
    }

    @RequestMapping(value = "/user", params = "add", method = RequestMethod.POST)
    public String postAddUser(@ModelAttribute @Valid User user,
            BindingResult result) {

        if (result.hasErrors()) {
            return "redirect:user?add";
        } else {
            System.out.println("Inside postAddUser");
            user = userRepository.save(user);
            return "redirect:user?id=" + user.getId();
        }

    }

以下是User.java的代码

package in.ac.jmi.entities;

import in.ac.jmi.constants.Role;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name="USER")
public class User extends UrlEntity{

    @Column(name="NAME", nullable = false)
    @NotEmpty(message="Name can not be empty")
    private String name;

    @Column(name="ROLE", nullable = false)
    @NotNull(message="Role can not be left blank")
    private Role role;

    @Column(name = "EMAIL_ADDRESS", nullable = false)
    @NotEmpty(message="email address can not be empty")
    private String emailAddress;

    public User(){}

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Role getRole() {
        return role;
    }
    public void setRole(Role role) {
        this.role = role;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }
@Override
public String toString() {
    return "\nUser [name=" + name + ", role=" + role + ", emailAddress="
            + emailAddress + "]";
}

}

1 个答案:

答案 0 :(得分:1)

原因是您使用的是alert,它清除了return "redirect:user?add";中的错误消息,请尝试将其更改为model