2017-04-10 9 views
0

私はHibernateの検証とエラーメッセージのカスタマイズに苦労しています。 私の場合は、サインアップフォームとメッセージバンドルの簡単な検証があります。これらのメッセージを使用しようとしていますが、エラーが発生するたびに{error.identifier}のように表示されます。私はコードをデバッグし、このメッセージがすでにErrorsオブジェクトに渡されていることがわかりました 助けてください。私はどこでも検索しました。メッセージの上Hibernate検証。メッセージの識別子として表示されるメッセージ

Page screenshot

だけThymeleaf <p th:text="#{password.constraint}"></p> MvcConfig.javaによって書かれている:

@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; 
} 

SignupForm.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 

プロジェクト構造:Project Structure screenshot の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> 

また、私は同じ内容とメッセージcatched、ページ上に表示されたとのリソースフォルダにValidationMessages.propertiesを追加しました。しかし、ローカライズされたmessageSourceバンドルでこれを呼び出す方法について質問してください。 @Beanバリデータ()は何もしません。この豆をどこかに注入する必要がありますか?

答えて

0

私は初心者ですし、さまざまなソースからのソリューションを混ぜて間違えました。解決策は非常に単純で愚かでした。そこ)(拡張スーパークラスWebMvcConfigurerAdapter@Override方法getValidator()する必要はなく、@Beanバリデータを定義します。

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

私は質問は削除されません。たぶん誰かに助けになるでしょう。

関連する問題