当前位置: 首页 > 知识库问答 >
问题:

Hibernate验证。显示为其标识符的消息

贝凯
2023-03-14

我正在努力进行Hibernate验证和自定义错误消息。在我的例子中,我有注册表单和消息包的简单验证。我试图使用这些消息,但每当出现错误时,它们都会在页面上显示为{error.identifier}。我调试了代码,发现此消息已传递给描述下面的Errors对象。请帮忙。我到处找。

页面截图

以上信息由Thymeleaf编写

@Bean
public LocaleResolver localeResolver() {
    return new CookieLocaleResolver();
}



@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
    lci.setParamName("lang");
    return lci;
}


@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
}
@Bean(name = "messageSource")
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new 
    ReloadableResourceBundleMessageSource();
    messageSource.setBasename("/i18n/messages");
    messageSource.setDefaultEncoding("UTF-8");
    messageSource.setFallbackToSystemLocale(false);
    messageSource.setCacheSeconds(0);
    return messageSource;
}

@Bean
public ValidatorFactory validator() {
    LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean();
    factory.setValidationMessageSource(messageSource());
    return factory;
}

注册表格。java:

import org.hibernate.validator.constraints.*;
import org.z1key.projects.entity.Role;
import org.z1key.projects.entity.User;

import javax.validation.constraints.Pattern;

@FieldMatch(first = "password", second = "verifyPassword", message = 
"Passwords are different.")
public class SignupForm {

private static final String NOT_BLANK_MESSAGE = "{notBlank.message}";
private static final String EMAIL_MESSAGE = "{email.constraint}";
private static final String PASSWORD_MESSAGE = "{password.constraint}";

@NotBlank
@Length(min = 5, max = 25)
@Pattern(regexp = "^[A-Za-z\\d]+$", message = "{login.constraint}")
private String login;

@NotBlank(message = NOT_BLANK_MESSAGE)
@Email(message = EMAIL_MESSAGE)
private String email;

@NotBlank(message = SignupForm.NOT_BLANK_MESSAGE)
@Length(min = 6, max = 40)
@Pattern(regexp = "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]+$", message = PASSWORD_MESSAGE)
private String password;

private String verifyPassword;

...Getters & Setters

项目结构:项目结构截图pom。xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.z1key.projects</groupId>
<artifactId>library</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent>

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

    <!-- Thymeleaf -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>nz.net.ultraq.thymeleaf</groupId>
        <artifactId>thymeleaf-layout-dialect</artifactId>
        <version>2.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>

    <!-- Security-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- JPA -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- Session -->
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
        <version>1.3.0.RELEASE</version>
    </dependency>
    <!-- Redis -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <!-- JDBC -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <!-- TEST -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- jUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- Connection Pool -->
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
    </dependency>

    <!-- MySQL driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.5</version>
    </dependency>

    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.3</version>
    </dependency>
</dependencies>

<name>library</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>org.z1key.projects.config.Application</start-class>
    <thymeleaf.version>3.0.5.RELEASE</thymeleaf.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
    <finalName>${project.artifactId}</finalName>
    <outputDirectory>${CATALINA_HOME}/webapps/${project.artifactId}</outputDirectory>
    <plugins>
        <!-- Tomcat plugin -->
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <url>http://localhost:8080/manager/text</url>
                <server>TomcatServer</server>
                <username>z1key</username>
                <password>z1key</password>
                <warFile>target/${project.artifactId}.war</warFile>
                <path>/${project.artifactId}</path>
                <update>true</update>
            </configuration>
        </plugin>
        <!-- Boot Maven plugin-->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <!-- Move resources for SpringBoot-->
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/classes/static</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/webapp</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

此外,我还添加了ValidationMessages。页面上捕获并显示了具有相同内容和消息的资源文件夹的属性。但问题是如何在本地化的messageSource包中调用它。因为@Bean validator()不会产生任何效果。也许需要把这个豆子注射到什么地方?

共有1个答案

杭英杰
2023-03-14

我是一个新手,在混合来自不同来源的解决方案时犯了错误。解决方案非常简单和愚蠢。需要扩展的超级类WebMvcConfigrerAdapter@Overder方法getValidator()而不是在那里定义@Bean validator():

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
...
@Override
public Validator getValidator() {
    LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean();
    factory.setValidationMessageSource(messageSource());
    return factory;
}
...

我不会删除问题。也许这对其他人会有帮助。

 类似资料:
  • 我试图让我的javascript验证电话号码输入,并在用户输入信息不正确时显示消息。我还设置了另外两个输入,所以我复制了其中一个,并用phoneNumber替换了名称,但没有结果。没有HTML的替代品,请它是我必须使用我的评估。 这里我只包含了电话号码输入的代码。http://jsfiddle.net/29ZNV/ 超文本标记语言 JAVASCRIPT

  • 我试图覆盖默认的Hibernate验证器消息,但没有成功。我已经为几个文本使用了一个资源包,它工作得很好。问题在于验证器的消息。 详细内容: 我创建了以下文件:ValidationMessages\u de.properties并将其放置在\src\main\resources\translations\ValidationMessages\u de.properties下。该文件具有以下资源:o

  • 因此,很明显,它识别出输入的名称太短,但我在视图中没有得到任何错误消息,正如这里所预期的:。

  • Swagger UI 不显示请求参数的验证信息。我想显示参数的最小值,最大值,格式,模式(正则表达式)。有谁知道我在Swagger UI上表达参数验证信息的方式吗?

  • 我想对我的Vue使用Firebase身份验证。js应用程序考虑以下限制: 我无法使用任何受支持的提供商(Facebook、Google等) 我不能使用电子邮件-该应用程序是为孩子们设计的,因此,我想使用他们在注册时选择的唯一昵称,而不是电子邮件,该昵称必须使用本地rtl语言(非英语) 我想使用firebase云函数作为唯一的服务器端代码 期望的结果是:a)使用昵称、密码注册 如何使用firebas