Spring MVC - 获取错误的消息包
我在使用 Spring MVC 消息包时遇到了一个奇怪的问题:获取了错误的消息包文件。我已经仔细检查过,在我的 Java 控制器类中,我有 fr_FR 语言环境,但 Spring 标签 (appContext.getMessage(code, null, locale);
在类中以及)给我回复英文信息!
到底是怎么回事?
我正在为 Liferay Portal 开发 portlet。让我向您展示我的部分代码:
在 applicationContext.xml 中:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
</bean>
在我的 JSP 中,我的代码如下所示:
...
<spring:message code="button.help"/>
...
我的消息的路径如下所示:
development:
- /src/main/webapp/WEB-INF/classes/messages.properties (英文,默认)
- /src/main/webapp/WEB-INF/classes/messages_fr.properties
部署在Tomcat
- /webapps/MY_APP/WEB-INF/classes/messages.properties
- /webapps/MY_APP/WEB-INF/classes/messages_fr.properties
I have a strange problem using Spring MVC message bundles: the wrong message bundle file is being fetched. I have double-checked, and in my Java controller class I have the fr_FR locale, but Spring tags (appContext.getMessage(code, null, locale);
in the class as well) return me English messages!
What is going on?
I am developing portlets for Liferay Portal. Let me show you parts of my code:
in applicationContext.xml:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
</bean>
in my JSPs I have code looking like this:
...
<spring:message code="button.help"/>
...
and the paths to my messages look like this:
development:
- /src/main/webapp/WEB-INF/classes/messages.properties (English, default)
- /src/main/webapp/WEB-INF/classes/messages_fr.properties
deployed in Tomcat
- /webapps/MY_APP/WEB-INF/classes/messages.properties
- /webapps/MY_APP/WEB-INF/classes/messages_fr.properties
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试在浏览器中设置语言(为 fr_FR )。在我使用的 Firefox 版本中,它位于
Edit -> Preferences ->内容->语言
并使用上移或下移按钮将 fr_FR 作为首选。
这使得浏览器发送带有首选区域设置的请求。
Try setting the language (to fr_FR ) in your browser. In firefox version that I use, its at
Edit ->Preferences -> Content -> Langauges
And use the move-up or move-down buttons to have fr_FR as the top preference.
This makes the browser send requests with the prefered locale set.
当我的本地化消息文件有错误的 unicode 字符(
\uXXX 形式)。
Java、Spring和Tomcat都没有写过相关的信息,根本没有任何反馈。
修复文件后,一切突然开始工作。
问候
I had a similar issue when my localized message file had a bad unicode character (the
\uXXX form).
Neither Java, Spring nor Tomcat did write an information about it, no feedback at all.
After fixing the file, everything suddenly started to work.
Regards
首先,您必须确保 liferay 支持您想要的区域设置。此外,您还必须在每个 portlet 的 portlet.xml 中添加支持的区域设置。
portlet.xml
:如果您错过了两者之一,它将无法工作。检查您是否也为此 portlet 添加了受支持的区域设置。
Frist you have to make sure that liferay supports the locale you want. In addition you have to add your supported locales in the portlet.xml for each portlet.
portlet.xml
:If you miss one of both it just won't work. Check if you added the supported-locale for this portlet as well.
您必须在 applicationContext 中有一个拦截器,就像
您还需要
在我的项目的第一个 JSP 页面上,我给出了这样的语言选择:
请告诉我这是否对您有帮助。
PS 我的 messages_*.properties 文件位于源文件夹 src/main/resources 中,而不是在 webapp 中,我不知道这是否重要。
PPS 有用的教程在这里:
http://springbyexample.org/examples/basic-webapp-internationalization.html
我还想补充一点,在你的 xml 的开头你应该有这样的东西:
它是必要的,以便识别诸如 mvc 之类的前缀。确保你拥有它。
You must have an interceptor in applicationContext like
You also need
On the first JSP page of my project I gave a choice for languages like that:
Please tell me if this helped you.
P.S. My messages_*.properties files are in a source folder src/main/resources, not in webapp, I don't know if this matters.
P.P.S. Useful tutorial here:
http://springbyexample.org/examples/basic-webapp-internationalization.html
I would like to add also that at the beginning of your xml you should have stuff like:
It is necessary in order to recognize the prefixes such as mvc. Make sure you have it.