Validation / conversion errors <f: viewParam> are not localized in <f: view locale>, but by default

I have a <f:viewParam> in JSF pages that sets the GET parameters to the corresponding managed bean after conversion and validation.

If conversion or verification errors occur, the corresponding error message is displayed from the resource set and displayed on <p:messages> (it can also be <p:growl> or <h:messages> ).

The app is multilingual. Therefore, when a different language is selected, a message should be displayed in that language, but it always displays the message in accordance with 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> 

Converter:

 @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. } } 

With the exception of messages from <f:viewParam> , there are no problems. All other message types are displayed in the language selected by the user.

Is there anything special with <f:viewParam> ?

+6
source share
1 answer

I can reproduce your problem. Both Mojarra 2.1.25 and MyFaces 2.1.12 pose the same problem. So I'm not sure if this is a bug in the JSF impl or an oversight in the JSF specification. It is still becoming clear that the language standard for the view is not set for the GET request before the render response phase is entered. The converter runs during the validation phase, well before the rendering response, which explains why it got the standard locale. I should study it later and, if necessary, report the Moharre problem.

At the same time, it is best to solve this problem: get the package as follows instead of the EL evaluation <resource-bundle><var> :

 String basename = "com.example.i18n.message"; // Exactly the same as <resource-bundle><base-name> Locale locale = context.getApplication().evaluateExpressionGet(context, "#{localeBean.locale}", Locale.class); ResourceBundle bundle = ResourceBundle.getBundle(basename, locale); // ... 

Update . I reported question 3021 in accordance with this problem. I still can’t plunge into what the spec says at this point, but I find the implementation behavior unintuitive.


Update 2 : the Mojarra guys and MyFaces agreed to this. For Mojarra this was fixed in accordance with 2.2.5 (there is no back.backback 2.1.x yet?), And for MyFaces it was fixed in accordance with 2.0.19, 2.1.13 and 2.2.0.

+5
source

Source: https://habr.com/ru/post/952679/


All Articles