2013-08-19 93 views
0

在問這裏之前,我在這裏搜索了很多內容,找到一個有用的內容,但是我無法解決我的問題。Hibernate @Valid和Spring MVC

我正在學習使用Spring MVC和Hibernate Validator以及i18n支持。當表單需要驗證時,我的問題是相關的,下面是我生成的代碼。

豆用戶

@Entity(name="users") 
public class User implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long id; 

    @NotEmpty 
    private String firstName; 
    private String lastName; 

} 

UserController中

@Secured("ROLE_USER") 
@RequestMapping(value = "/save", method = RequestMethod.POST) 
public String doSave(@ModelAttribute("formUsuario") @Valid br.com.rodrigoferra.domain.User user, BindingResult results, Model model) { 

     UserValidator userValidator = new UserValidator(); 

     userValidator.validate(user, results); 

     if(results.hasErrors()) { 
      return "users/edit"; 
     } 

     user = userService.create(user); 

     return "redirect:/users/"; 

    } 

UserValidator

public class UserValidator implements Validator { 

    @Autowired private MessageSource messageSource; 

    protected static Logger logger = Logger.getLogger("controller"); 

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

    public void validate(Object target, Errors errors) { 
     logger.info("Entrou em validate"); 

    User user = (User) target; 

    if (user.getFirstName() == null) { 
     errors.rejectValue("firstName", "NotEmpty.user.firstName"); 
    } 

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "NotEmpty.user.firstName"); 

    } 

} 

其中messages.properties所在的位置: enter image description here 最後,applicationContext.xml中

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
     <property name="basenames" value="classpath:/resources/i18n/messages, classpath:/resources/i18n/errors" /> 
     <property name="defaultEncoding" value="UTF-8" /> 
    </bean> 

    <bean name="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> 
     <property name="validationMessageSource"> 
      <ref bean="messageSource" /> 
     </property> 
    </bean> 

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver"> 
     <property name="defaultLocale" value="pt_BR" /> 
    </bean> 

報告該錯誤是在執行方法:

GRAVE: Servlet.service() for servlet [spring] in context with path [/simpla-spring-version] threw exception [Request processing failed; nested exception is org.apache.tiles.request.render.CannotRenderException: ServletException including path '/WEB-INF/layouts/simpleLayout.jsp'.] with root cause 
org.springframework.context.NoSuchMessageException: No message found under code 'NotEmpty.user.firstName' for locale 'pt_BR'. 

messages_pt_BR.properties,同樣以messages.properties:

NotEmpty.user.firstName = Name is required! 

好,I'm越來越瘋狂吧,已經改變了位置,嘗試了很多在網絡和春季論壇上創建的示例......我感謝任何幫助。

對不起,我的英語不好。

+0

有錯誤,它試圖顯示消息NotEmpty.user.firstname,但message.properties不包含該屬性 – orangegoat

+0

對不起,我忘了添加messages.properties! –

回答

0

消息文件路徑錯誤,資源目錄的內容直接添加到類路徑(就像java目錄)。更多信息可在maven網站

所以不是classpath:/resources/i18n/messages使用classpath:/i18n/messages和相同GOED上發現錯誤ofcourse文件。

+0

我會測試,該消息需要NotEmpty作爲前綴? NotEmpty.user.firstName或只是user.firstName? –