简体   繁体   English

Spring Validator不显示错误消息

[英]Spring Validator doesn't show error message

I have a problem with my UserValidator class. 我的UserValidator类有问题。 I'm trying to validate 2 fields of form: username and email and only in case of username the messahe is showing. 我正在尝试验证表单的2个字段:用户名和电子邮件,并且仅在使用用户名的情况下显示消息。 It's weird for me, because i'm doing it the same way in both cases. 这对我来说很奇怪,因为在两种情况下我都以相同的方式进行操作。

I have globalmessages.properties file with my error messages: 我有带有错误消息的globalmessages.properties文件:

same.mail=Email exists, type other email!
equal.user=Username exists, type other username!

UserValidator class is validated in Controller in that way: UserValidator类通过以下方式在Controller中验证:

@Autowired
private UserValidator userValidator;

@RequestMapping(value = "/adduser", method = RequestMethod.GET)
    public String addUser(@Valid @ModelAttribute(value = "user") User user, BindingResult result) {
userValidator.validate(user, result);
return "signin";
}

In validate method i'm checking if user exists in db and if so error is registered. 在验证方法中,我正在检查用户是否存在于db中,如果存在则记录错误。 Then i'm doing the same with email. 然后我用电子邮件做同样的事情。 That's UserValidator's validate method: 那是UserValidator的validate方法:

@Override
    public void validate(Object target, Errors errors) {

        User user = (User) target;

        //check if user exists
        Users users = usersDAOImpl.checkByUsername(user.getUsername());
        //check if email exists
        Emails email = usersDAOImpl.checkByEmail(user.getEmail());

        String emailAddresse = email == null ? "null" : "not null";

        System.out.println("From UserValidator class, before rejecting values- email: "
                + emailAddresse);

        if (users != null)
        {
            errors.rejectValue("username", "equal.user");
            System.out.println("From UserValidator class - user not null: "
                    + users.getUsername());
        }

        if (email != null) 
        {
            errors.reject("email", "same.mail");
            System.out.println("From UserValidator class - email not null: "
                    + email.getEmail());
        }
    }

There's my signin form: 有我的登录表格:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page session="false" %>
<html>
<head>
    <title>Home</title>
</head>
<body>

<form:form modelAttribute="user" method="get" action="${pageContext.request.contextPath}/adduser" >
    <table align="left">
        <tr>
            <td>Username</td>
            <td><form:input path="username" /></td>
            <td><font color="red"><form:errors path="username"></form:errors></font></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><form:input type="password" path="password" /></td>
            <td><font color="red"><form:errors path="password"></form:errors></font></td>
        </tr>
        <tr>
            <td>Confirm password</td>
            <td><form:input type="password" path="confpassword" /></td>
            <td><font color="red"><form:errors path="confpassword"></form:errors></font></td>
        </tr>
        <tr>
            <td>E-mail</td>
            <td><form:input type="email" path="email" /></td>
            <td><font color="red"><form:errors path="email"></form:errors></font></td>
        </tr>
        <tr>
            <td><input type="submit" value="Create user"></td>
        </tr>
    </table>
</form:form>

</body>
</html>

When i type existing username and submit the form, the error for username from globalmessages.properties is displaying and when i type existing email, error message isn't displaying, although usersDAOImpl.checkByEmail method works fine and i can see "From UserValidator class - email not null: " + email.getEmail() on console. 当我键入现有的用户名并提交表单时,显示来自globalmessages.properties的用户名错误,并且当我键入现有的电子邮件时,未显示错误消息,尽管usersDAOImpl.checkByEmail方法可以正常工作,并且我可以看到“来自UserValidator类- email不为null:“ +在控制台上的email.getEmail()。

If e-mail validation should be a field error 如果电子邮件验证应为字段错误

The method invocation should be rejectValue for the email field too. email字段的方法调用也应为rejectValue As reject sets a global error instead of a field error . 由于reject设置全局错误而不是字段错误

If it really is a global error 如果确实是全局错误

Or if you really want a global error, you need to consult the API docs, where you could find out that the signature of reject is: 或者,如果您确实想要全局错误,则需要查阅API文档,在该文档中您可以发现reject的签名是:

void reject(String errorCode, String defaultMessage);

so, in your case it would look like this: 因此,在您的情况下,它看起来像这样:

errors.reject("same.mail", "default message for same email address");

This would register a global error with your string in the property file, you just need to create a field in your markup, where the string should appear to the user. 这将在属性文件中用字符串注册一个全局错误,您只需要在标记中创建一个字段,该字符串应在用户面前显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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