【发布时间】:2013-08-31 23:39:00
【问题描述】:
我在 JSF 页面上有一个 <f:viewParam> 标记,它在转换和验证后将 GET 参数设置为相应的托管 bean。
如果发生转换或验证错误,则会从资源包中获取相应的错误消息并显示在<p:messages>(也可能是<p:growl> 或<h:messages>)上。
该应用程序是多语言的。 Therefore when a different language is selected, a message should be displayed in that language but it always displays the message according to the default locale en (for English).
Test.xhtml:
<!DOCTYPE html>
<html lang="#{localeBean.language}"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view locale="#{localeBean.locale}">
<f:metadata>
<f:viewParam name="id" converter="#{myConverter}" />
</f:metadata>
<h:head>
<title>Test</title>
</h:head>
<h:body>
<h:messages />
</h:body>
</f:view>
</html>
转换器:
@FacesConverter("myConverter")
public final class MyConverter implements Converter
{
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
ResourceBundle bundle = context.getApplication()
.evaluateExpressionGet(context, "#{messages}", ResourceBundle.class);
String message = bundle.getString("id.conversion.error");
throw new ConverterException(
new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value)
{
throw new UnsupportedOperationException(); // Not relevant in this problem.
}
}
除了<f:viewParam>的消息外,没有问题。所有其他类型的消息都以用户选择的语言显示。
<f:viewParam> 有什么特别之处吗?
【问题讨论】:
标签: validation jsf jsf-2 localization viewparams