2017-06-09 19 views
1

ウェブサイトに登録する際にエラーメッセージを表示しようとしています。私は春、春の検証、春のセキュリティとmysqlデータベースを使用しています。私は春には新しく、これを理解することはできません。ここでorg.springframework.context.NoSuchMessageException:ロケール 'en_US'の 'Size.userForm.username'コードのメッセージが見つかりません

は私のhtmlファイルです:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 

<c:set var="contextPath" value="${pageContext.request.contextPath}"/> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 
    <meta name="description" content=""> 
    <meta name="author" content=""> 

    <title>Create an account</title> 

    <link href="${contextPath}/resources/css/bootstrap.min.css" rel="stylesheet"> 
    <link href="${contextPath}/resources/css/common.css" rel="stylesheet"> 

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> 
    <!--[if lt IE 9]> 
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 
    <![endif]--> 
</head> 

<body> 

<div class="container"> 

    <form:form method="POST" modelAttribute="userForm" class="form-signin"> 
     <h2 class="form-signin-heading">Create your account</h2> 
     <spring:bind path="username"> 
      <div class="form-group ${status.error ? 'has-error' : ''}"> 
       <form:input type="text" path="username" class="form-control" placeholder="Username" 
          autofocus="true"></form:input> 
       <form:errors path="username"></form:errors> 
      </div> 
     </spring:bind> 

     <spring:bind path="password"> 
      <div class="form-group ${status.error ? 'has-error' : ''}"> 
       <form:input type="password" path="password" class="form-control" placeholder="Password"></form:input> 
       <form:errors path="password"></form:errors> 
      </div> 
     </spring:bind> 

     <spring:bind path="passwordConfirm"> 
      <div class="form-group ${status.error ? 'has-error' : ''}"> 
       <form:input type="password" path="passwordConfirm" class="form-control" 
          placeholder="Confirm your password"></form:input> 
       <form:errors path="passwordConfirm"></form:errors> 
      </div> 
     </spring:bind> 

     <button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button> 
    </form:form> 

</div> 
<!-- /container --> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
<script src="${contextPath}/resources/js/bootstrap.min.js"></script> 
</body> 
</html> 

私のディスパッチャサーブレットは次のようになります。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

    <context:component-scan base-package="com.web.shop"/> 

    <mvc:annotation-driven/> 

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/view/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 

    <bean id="messageSource" 
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource" 
    p:basename="WEB-INF/messages" /> 
</beans> 

マイUserValidationファイル:

@Component 
public class UserValidator implements Validator { 

    @Autowired 
    private UserService userService; 

    @Override 
    public boolean supports(Class<?> aClass) { 
     return User.class.equals(aClass); 
    } 

    @Override 
    public void validate(Object o, Errors errors) { 
     User user = (User) o; 

     ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "NotEmpty"); 

     if (user.getUsername().length() < 6 || user.getUsername().length() > 32) { 
      errors.rejectValue("username", "Size.userForm.username"); 
     } 

     if (userService.findByUsername(user.getUsername()) != null) { 
      errors.rejectValue("username", "Duplicate.userForm.username"); 
     } 

     ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "NotEmpty"); 
     if (user.getPassword().length() < 8 || user.getPassword().length() > 32) { 
      errors.rejectValue("password", "Size.userForm.password"); 
     } 

     if (!user.getPasswordConfirm().equals(user.getPassword())) { 
      errors.rejectValue("passwordConfirm", "Diff.userForm.passwordConfirm"); 
     } 
    } 
} 

メッセージと私のファイルが入っていますWEB-INFをメッセージフォルダに入れると、それはvalidation.propertiesと呼ばれ、次のようになります:

NotEmpty=This field is required. 
Size.userForm.username=Please use between 6 and 32 characters. 
Duplicate.userForm.username=Someone already has that username. 
Size.userForm.password=Try one with at least 8 characters. 
Diff.userForm.passwordConfirm=These passwords don't match. 

なぜメッセージが見つからないのかわかりません。これは私が得るエラーです:

There was an unexpected error (type=Internal Server Error, status=500). 
org.springframework.context.NoSuchMessageException: No message found under code 'Size.userForm.username' for locale 'en_US'. 

ありがとうございました。

+0

デフォルトロケールは 'en_US'で、' en'ではないと思います。同じメッセージで 'validation_en_US.properties'を追加すると問題が解決されます –

+0

私はあなたが言ったことをしましたが、同じエラーが発生します: org.springframework.context.NoSuchMessageException: 'Size.userForm.username'ロケール 'en_US'。 – P3P5

答えて

1

ファイル名のような命名規則リソースファイルが

「messages_en_us.properties」

<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
<property name="basename"> 
    <value>classpath*:messages</value> 
</property> 

すべてのプロパティファイルは、Javaのクラスパスにあるので、あなたが定義する必要があるべきフォロー接頭辞classpathを持つパス*:それ以外の場合はアプリケーションのWebディレクトリを調べます

+0

はい、ありがとうございます。 – P3P5

+0

@ P3P5ようこそ。乾杯.. –

関連する問題