Jsf 如何根据条件显示h:消息?
我正在制作多语言网站,其中一个字段使用验证程序 验证后,我收到的响应为Jsf 如何根据条件显示h:消息?,jsf,jsf-2,validation,multilingual,Jsf,Jsf 2,Validation,Multilingual,我正在制作多语言网站,其中一个字段使用验证程序 验证后,我收到的响应为err002、err003,基于此错误,我将以消息格式显示相应的错误。所以我的计划如下 我拥有的是 我想做的事情如下 if (message is err002) { show message of err002 from the properties file. #{msg['err002']} } if (message is err003) { show message of err003 fro
err002、err003
,基于此错误,我将以消息格式显示相应的错误。所以我的计划如下
我拥有的是
我想做的事情如下
if (message is err002) {
show message of err002 from the properties file.
#{msg['err002']}
}
if (message is err003) {
show message of err003 from the properties file.
#{msg['err003']}
}
你知道怎么做吗
实际上我想做的是用这两种语言显示错误消息。我拥有的是会话bean中的语言代码,但我不能在验证器中检查语言代码
任何关于如何做到这一点的想法/建议都将非常有用
编辑1 faces config.xml
<application>
<locale-config>
<default-locale>zh_CN</default-locale>
</locale-config>
<resource-bundle>
<base-name>resources.welcome</base-name>
<var>msg</var>
</resource-bundle>
</application>
我拥有的属性文件是
welcome.properties和welcome\u zh\u CN.properties您可以通过验证程序方法轻松实现它。像这样使用它
@FacesValidator("passwordValidator")
public class PasswordValidator implements Validator {
String err1, err2, err3;
public PasswordValidator() {
ResourceBundle bundle = ResourceBundle.getBundle("msg", FacesContext.getCurrentInstance().getViewRoot().getLocale());
err1 = bundle.getString("err1");
err2 = bundle.getString("err2");
err3 = bundle.getString("err3");
}
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
String pass = (String) value;
FacesMessage msg;
if(/*some condition*/) {
msg = new FacesMessage(err1);
} else if(/*other condition*/) {
msg = new FacesMessage(err2);
} else {
msg = new FacesMessage(err3);
}
if(msg != null) {
throw new ValidatorException(msg);
}
}
}
并在视图中使用它
<h:inputText id="password" validator="passwordValidator" .../>
<h:message for=password .../>
您可以访问该会话bean,也许您会发现一些想法。我忘记了抛出异常-添加它只是为了解决这个问题)太好了。。。如果有任何疑问,我会检查并回复您。:)我想你会救我的…可能有两个原因。1.没有msg_zh_CN.properties文件。2.捆绑包不在您搜索它的位置。看看加载器方法。这个问题已经解决了,但出现了新问题,如FacesContext.getCurrentInstance().getViewRoot().getLocale()
总是给我zh_CN
谢谢你的代码更正。在编写没有IDE的代码时总是会发生这种情况)。但请停止删除您的输入,被发送!
<h:inputText id="password" validator="passwordValidator" .../>
<h:message for=password .../>