java 如何在 Spring MVC 中定义和获取基于语言环境的消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4192796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:15:07  来源:igfitidea点击:

How to I define and get locale-based messages in Spring MVC?

javavalidationinternationalizationspring-mvc

提问by David Parks

I want to define a set of error messages so that when validation errors generate codes, those codes pick up the corresponding error messageand print them.

我想定义一组错误消息,以便在验证错误生成代码时,这些代码选取相应的错误消息并打印出来。

For the sake of learning, and to develop an extendable web app, I'd like to follow the proper i18n path, though I only need to define one (english) set of messages now.

为了学习和开发可扩展的网络应用程序,我想遵循正确的 i18n 路径,尽管我现在只需要定义一组(英文)消息。

So the locales should all default to english when they don't find their own resources (which I'm yet to define).

因此,当语言环境找不到自己的资源(我尚未定义)时,它们都应该默认为英语。

I've never used any of the i18n functionality of Java. And the spring docs assume that I have this knowledge.

我从未使用过 Java 的任何 i18n 功能。spring 文档假设我有这些知识。

Could someone just give me a gental nudge in the right direction?

有人可以给我一个正确的方向的轻推吗?

I've defined a messageSourcein my dispatcher-servlet.xmlwebapp context. I have a validator which produces a BindingResultobject with a rejected field "username", with code "username.taken". I can generate the default message.

我已经messageSource在我的dispatcher-servlet.xmlwebapp 上下文中定义了一个。我有一个验证器,它生成一个BindingResult带有拒绝字段的对象"username"和代码"username.taken"。我可以生成默认消息。

Now I just need to get the error message from the errormessages.propertiesfile in my view.

现在我只需要errormessages.properties在我的视图中从文件中获取错误消息。

How do I resolve an error code to a message?

如何将错误代码解析为消息?

<bean id="messageSource"
      class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basenames">
    <list>
      <value>errormessages</value>
    </list>
  </property>
</bean>

回答by scheffield

It depends on what you want to do with this text. The first possibility is to get the message programmatically:

这取决于你想用这个文本做什么。第一种可能性是以编程方式获取消息:

@Autowired
private MessageSource messageSource;

@RenderMapping(params = "render=details")
public String showDetails (Model model, Locale locale) {
    messageSource.getMessage(<your key goes here>, null, locale);
}

This way is very uncommon, cause you have to get the message keys form the Errors object by yourself.

这种方式非常不常见,因为您必须自己从 Errors 对象中获取消息键。

Another more common way is to use the build in view extensions shipped with spring mvc. You didn't wrote it but I guess your using JSPs. In that case you can simply write something like this in your JSP:

另一种更常见的方法是使用 spring mvc 附带的内置视图扩展。你没有写,但我猜你使用的是 JSP。在这种情况下,您可以简单地在 JSP 中编写如下内容:

<!-- showing errors -->
<div>
    <form:errors path="*" />
</div>

<!-- showing arbitrary messages -->
<div>
    <spring:message code="${success.messageKey}"/>
</div>

For further reading I suggest you http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html

如需进一步阅读,我建议您http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html