Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 在验证消息中删除引号_Spring_Validation_Thymeleaf_Properties File - Fatal编程技术网

Spring 在验证消息中删除引号

Spring 在验证消息中删除引号,spring,validation,thymeleaf,properties-file,Spring,Validation,Thymeleaf,Properties File,我正在使用Spring Thymeleaf,并且在external messages.properties文件中有表单验证消息 当我这样引用消息时: th:attr="data-error=#{field.error.required.field}" 如以下输入字段声明所示: <input class="form-control input-lg" type="text" th:field="*{firstName}" th:attr="data-error=#{field

我正在使用Spring Thymeleaf,并且在external messages.properties文件中有表单验证消息

当我这样引用消息时:

th:attr="data-error=#{field.error.required.field}"
如以下输入字段声明所示:

<input class="form-control input-lg" type="text" th:field="*{firstName}"
       th:attr="data-error=#{field.error.required.field}" required="true"
       data-delay="100" placeholder="First name"/>
如何显示没有引号的消息

下面是一些更详细的代码。模型对象看起来像:

public class UserSession {

    @Email
    @NotEmpty
    @Size(min = 2, max = 255)
    private String email;
    ...
}
<div class="form-group" th:classappend="${#fields.hasErrors('email')} ? has-error">
    <div class="input-group">
        <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
        <input class="form-control input-lg" type="email" id="email"
               th:field="*{email}" th:attr="data-error=#{field.error.invalid.email}"
               required="true" data-delay="100" placeholder="Email"/>
    </div>
    <div class="help-block with-errors" th:errors="*{email}"></div>
    <div class="help-block with-errors"></div>
</div>
表单输入如下所示:

public class UserSession {

    @Email
    @NotEmpty
    @Size(min = 2, max = 255)
    private String email;
    ...
}
<div class="form-group" th:classappend="${#fields.hasErrors('email')} ? has-error">
    <div class="input-group">
        <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
        <input class="form-control input-lg" type="email" id="email"
               th:field="*{email}" th:attr="data-error=#{field.error.invalid.email}"
               required="true" data-delay="100" placeholder="Email"/>
    </div>
    <div class="help-block with-errors" th:errors="*{email}"></div>
    <div class="help-block with-errors"></div>
</div>

当我删除
数据错误
属性并将消息直接绑定到模型对象属性时,我得到的是默认错误,而不是消息

您需要将@NotBlank/@NotNull与表单类绑定

您可以直接绑定message=“error.message”//properties变量

您的HTML代码:

<form role="form" th:action="@{/signup}" method="post" th:object="${userForm}">
    <div class="row">
        <div class="col-lg-12">
            <th:block th:if="${#fields.hasErrors('${userForm.*}')}">
                <div th:utext="Common error message">Alert</div>
            </th:block>

            <div class="form-group input-group" th:classappend="${#fields.hasErrors('firstName')}? 'has-error'">
                <input type="text" th:field="*{firstName}" class="form-control" placeholder="firstName" />
                <span class="help-block" th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}">Incorrect title</span>  
            </div>

            // your other filed with submit button          
        </div>
    </div>
</form> 

警觉的
标题不正确
//你的另一个提交按钮
注:

  • 如果表单验证失败,则在标头中附加公共消息
  • 表单字段错误消息根据表单类中设置的消息追加

这对我不起作用。当我删除“数据错误”属性时,它默认为默认错误消息。我将更新问题的更多细节。是的,因为您需要在notEmpty注释中添加带有@notEmpty(message=“key”)的消息,请尝试此操作。否则将显示默认消息