2013-07-28 21 views
0

我最近使用spring mvc时遇到问题。 我在我的项目中使用i18n消息。但我发现jsp是可以的,但在我的自定义java代码中工作。Spring MVC消息在JSP中正常,但不在我的Java代码中

我的配置为如下:

<bean id="messageSource" 
    class="org.springframework.context.support.ResourceBundleMessageSource" 
    abstract="false" scope="singleton" lazy-init="default"> 
    <property name="basename" value="messages/messages" /> 
</bean> 

我的JSP代码(我只是写这个测试代码,它得到的结果):

<spring:message code="error.user.none.registered" arguments="aaa"/> 

我的Java代码:

@Resource 
protected MessageSource messageSource; 
public Object login(String email, String password, String ip) 
      throws Exception { 
     User t = userDao.queryByEmail(email); 
     if (t == null) { // 账号不存在 
      return messageSource.getMessage(
        Constants.ERROR_USER_NONE_REGISTERED, 
        new Object[] { email }, Locale.SIMPLIFIED_CHINESE); 
     } 
..... 
} 

这段代码是在我的UserService.java登录方法中,我想检查电子邮件是否被注册。但也会得到错误

org.springframework.context.NoSuchMessageException: No message found under code 'error.user.none.registered' for locale 'zh_CN'. 
+0

从您的代码中,我猜测您的消息源未正确检索。我以前没有使用@Resource ...我认为你可以尝试使用Autowire保护的MessageSource messageSource而不是 –

回答

0

您是否在您的dispatcher-servlet.xml中定义了“messageSource”?

我猜你的java代码(我假设的服务)是在applicationContext.xml中定义的,因此它没有注入预期的messageSource。

+0

你的意思是我需要导入其他配置文件,我定义了服务接口? –

+0

我将两个文件合并成一个文件然后测试,没关系。谢谢 –

+0

@风一样的男子请参考这里以了解详情http://stackoverflow.com/questions/17909598/defining-messagesource-and-localeresolver-beans-in-spring-mvc-for-adding-i18n-su/17915856 #17915856。顺便说一句好用户名。 – Hippoom

相关问题