Same bundle always loaded regardless of a accept-language header - jsf

I am currently working on a primefaces website and I want to have it with internationalization.
I created a directory for the language bundles in:
src/main/resources/org/test
The bundle contains these files:
language.properties
language_en.properties
The faces config looks like this:
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<application>
<locale-config>
<supported-locale>en</supported-locale>
<supported-locale>de</supported-locale>
</locale-config>
<resource-bundle>
<base-name>org.test.language</base-name>
<var>language</var>
</resource-bundle>
</application>
</faces-config>
I tried loading the bundle in my view scoped view:
<f:loadBundle var="lang" basename="org.test.language"></f:loadBundle>
But I always get the german locale even with Accept-Language headers en_US.
What is wrong with it?

Related

JSF Error: Can't find bundle for base name xxxx, locale xxxx

I'm trying to internationalize my application and been searching a lot of tutorials on that. I always get stuck at this point, for no matter what configuration I do, none of them works.
Earlier I made a post onto a parsing exception. After solving this parser exception, now the application does not find bundles.
I arranged and registrated the Core bundle through faces-config.xml, as well as supported locales.
Even though I've searched through existing solutions but none of them seem to apply to my case.
So, where's the thing? Does it have to be at root folder? Is it a misconfig? A typo in my faces-config (don't think so, I redid it many times).
The config is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<!-- <message-bundle>messages_en_US</message-bundle> -->
<!-- <message-bundle>messages_es</message-bundle> -->
<!-- <message-bundle>messages_pt_BR</message-bundle> -->
<locale-config>
<default-locale>en_US</default-locale>
<supported-locale>pt_BR</supported-locale>
<supported-locale>es</supported-locale>
</locale-config>
<resource-bundle>
<base-name>com.tfduque.fieldassist.interface</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
Folder/package organization:
Stack trace (FULL: https://pastebin.com/69iJrvuX):
Type Exception Report
Message Can't find bundle for base name com.tfduque.fieldassist.interface_es, locale pt_BR
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
javax.servlet.ServletException: Can't find bundle for base name com.tfduque.fieldassist.interface_es, locale pt_BR
javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Solved it by removing the full path to the file - just the name of the file seems to be enough:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<!-- <message-bundle>messages_en_US</message-bundle> -->
<!-- <message-bundle>messages_es</message-bundle> -->
<!-- <message-bundle>messages_pt_BR</message-bundle> -->
<locale-config>
<default-locale>en_US</default-locale>
<supported-locale>pt_BR</supported-locale>
<supported-locale>es</supported-locale>
</locale-config>
<!--- Change the path ("com.tfduque.fieldassist.interface") to the filename ("interface") here -->
<resource-bundle>
<base-name>interface</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>

Default resource bundle not picked up on production server

Here is my faces-config.xml
<?xml version="1.0"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<application>
<message-bundle>messages</message-bundle>
<locale-config>
<default-locale>fr</default-locale>
<supported-locale>en</supported-locale>
</locale-config>
<resource-bundle>
<base-name>messages</base-name>
<var>messages</var>
</resource-bundle>
</application>
</faces-config>
Here are the resource bundles
src/main/resources/messages.properties -> hello=bonjour
src/main/resources/messages_en.properties -> hello=hello
On the JSF page #{view.locale} displays fr_FR, however #{messages['hello']} displays hello where bonjour was expected.
When the JBoss7 is run on development machine, the internationalization works. bonjour is displayed properly. When I copy the whole JBoss server, including its deployements, on the production machine, internationalization does not work.
Both machines run Windows 8.
How is this caused and how can I solve it?

java.lang.ClassNotFoundException: org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory

I am trying to implement Primeface Exception Handler
My faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<!-- JSF and Spring are integrated -->
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
</faces-config>
when I add the primefaces EL Resolver and factory in config, as follow, my tomcat fail start.
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<!-- JSF and Spring are integrated -->
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
<el-resolver>
org.primefaces.application.exceptionhandler.PrimeExceptionHandlerELResolver
</el-resolver>
</application>
<factory>
<exception-handler-factory>
org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory
</exception-handler-factory>
</factory>
</faces-config>
any update ?
-----------EDIT----------
I changes the tomcat version from 7.0.42 to 7.0.64, it showing me the following exception.
SEVERE: Critical error during deployment:
com.sun.faces.config.ConfigurationException: Factory 'javax.faces.context.ExceptionHandlerFactory' was not configured properly.
Caused by: javax.faces.FacesException: org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory
Caused by: java.lang.ClassNotFoundException: org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory
for detail in exception , please click Exception Detail
So the solution was to include the right version of PrimeFaces (5.0), previous versions did not contain this class.

JSF page not load on TomEE 1.5.2

I'm trying to start a small JSF application with no success. Tried it on Glassfish but I had the following error (Trying to use CDI on Glassfish 4 results in javax.el.PropertyNotFoundException: Target Unreachable, identifier 'indexMB' resolved to null).
So I tried to migrate the application to the TomEE 1.5.2, but although display no error in the console, the page does not load the JSF components, as you can see in the image below:
Any help will be very useful.
Follow my setup and my files:
TomEE 1.5.2
Primefaces 3.5
commons-fileupload 1.3
commons-io 2.4
** index.xhtml
<html ...>
<f:loadBundle basename="i18n" var="bundle" />
<h:head>
<title>#{bundle['index_title']}</title>
</h:head>
<h:body>
#{bundle['index_appname']}
<br />
<h:form id="frmIndex">
<p:panelGrid columns="2">
<p:outputLabel for="user" value="#{bundle['lblUser']}" />
<p:inputText id="user" value="#{indexMB.user}" />
<p:outputLabel for="password" value="#{bundle['lblPassword']}" />
<p:password id="password" value="#{indexMB.password}" />
</p:panelGrid>
<p:commandButton action="#{indexMB.loginTest}" value="#{bundle['btn_login']}" />
</h:form>
</h:body>
** IndexMB.java
#ManagedBean ("indexMB")
#RequestScoped
public class IndexMB {
private String password;
private String user;
public IndexMB() {
}
public String loginTest(){
...
}
// getters and setters
}
** web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
** faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<application>
<locale-config>
<default-locale>pt_BR</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>fr</supported-locale>
</locale-config>
</application>
** beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
the page does not load the JSF components
The page is loading the components, otherwise there won't be a <input type="text" /> that comes from the <p:inputText id="user" value="#{indexMB.user}" /> in your Facelets code (along with other components). The problem seems to be in your #{bundle['<whatever>']} configuration. Note that this problem is not related to GlassFish nor TomEE at all, just about the internationalization configuration.
For JSF 2.x I followed the explanation from this Q/A: https://stackoverflow.com/a/4830669/1065197 and the link that provides more info: Internationalization in JSF with UTF-8 encoded properties files. Based in that tutorial I made a test application. In short:
Configure the bundle in your faces-config.xml
<application>
<locale-config>
<default-locale>pt_BR</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>fr</supported-locale>
</locale-config>
<resource-bundle>
<!--
Here should be the full name of the package and the
name of the properties files with the i18n text.
Example (from a personal project):
edu.home.store.view.idioma.tienda
Where the properties files are:
edu.home.store.view.idioma.tienda_es.properties
edu.home.store.view.idioma.tienda_en.properties
-->
<!-- <base-name>edu.home.store.view.idioma.tienda</base-name> -->
<!-- assumming your file is directly posted in class folder -->
<base-name>i18n</base-name>
<!--
Name of the variable to use in Facelets files.
-->
<var>bundle</var>
</resource-bundle>
</application>
Create a #SessionScoped bean that will handle the locale to use in your pages.
#ManagedBean
#SessionScoped
public class LocaleBean implements Serializable {
private static final long serialVersionUID = 89794215646544L;
private Locale locale;
public LocaleBean() {
}
#PostConstruct
public void init() {
//give the default value here
locale = new Locale("pt_BR");
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}
public Locale getLocale() {
return locale;
}
public String getLenguaje() {
return locale.getLanguage();
}
public void setLenguaje(String lenguaje) {
locale = new Locale(lenguaje);
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}
}
Wrap all the content of your view using <f:view> and define the language to use. Preferably, this should go in the main template. Example:
<f:view locale="#{localeBean.locale}">
<!-- Your Facelets code goes here... -->
</f:view>
Apart from this problem, I highly recommend you to change the obviously autogenerated Faces Servlet mapping configuration from /faces/* to *.xhtml:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
More info:
JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?

JSF 2.2 faces-config: Unknown Schema version: 2.2

I'm trying to use the Faces 2.2. I get an error.
SEVERE: Critical error during deployment:
com.sun.faces.config.ConfigurationException: java.util.concurrent.ExecutionException: com.sun.faces.config.ConfigurationException: Unable to parse document 'jndi:/server/contracts/WEB-INF/faces-config.xml': Unknown Schema version: 2.2
at com.sun.faces.config.ConfigManager.getConfigDocuments(ConfigManager.java:749)
at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:348)
at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:214)
at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:4750)
at com.sun.enterprise.web.WebModule.contextListenerStart(WebModule.java:550)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:5366)
at com.sun.enterprise.web.WebModule.start(WebModule.java:498)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:917)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:901)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:733)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2019)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1669)
at com.sun.enterprise.web.WebApplication.start(WebApplication.java:109)
at org.glassfish.internal.data.EngineRef.start(EngineRef.java:130)
at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:269)
at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:301)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:461)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:389)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:348)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:363)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1085)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:95)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1291)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1259)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:461)
at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:212)
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:179)
at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
at com.sun.enterprise.v3.services.impl.ContainerMapper$Hk2DispatcherCallable.call(ContainerMapper.java:354)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.util.concurrent.ExecutionException: com.sun.faces.config.ConfigurationException: Unable to parse document 'jndi:/server/contracts/WEB-INF/faces-config.xml': Unknown Schema version: 2.2
at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:252)
at java.util.concurrent.FutureTask.get(FutureTask.java:111)
at com.sun.faces.config.ConfigManager.getConfigDocuments(ConfigManager.java:747)
... 44 more
Caused by: com.sun.faces.config.ConfigurationException: Unable to parse document 'jndi:/server/contracts/WEB-INF/faces-config.xml': Unknown Schema version: 2.2
at com.sun.faces.config.ConfigManager$ParseTask.call(ConfigManager.java:998)
at com.sun.faces.config.ConfigManager$ParseTask.call(ConfigManager.java:942)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at com.sun.faces.config.ConfigManager.getConfigDocuments(ConfigManager.java:735)
... 44 more
Caused by: com.sun.faces.config.ConfigurationException: Unknown Schema version: 2.2
at com.sun.faces.config.ConfigManager$ParseTask.getDocument(ConfigManager.java:1101)
at com.sun.faces.config.ConfigManager$ParseTask.call(ConfigManager.java:989)
... 48 more
faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_2.xsd">
<application>
<resource-library-contracts>
<contract-mapping>
<url-pattern>/user/*</url-pattern>
<contracts>blue</contracts>
</contract-mapping>
<contract-mapping>
<url-pattern>*</url-pattern>
<contracts>red</contracts>
</contract-mapping>
</resource-library-contracts>
</application>
</faces-config>
The code of this example.
I copied latest JSF version over "glassfish3/glassfish/modules/javax.faces.jar" file.
I do not understand the problem.
The schema namespace for JSF 2.2 changed. The faces-config.xml has to look like this:
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
</faces-config>
I had the same problem with eclipse. I still get a warning ("No grammar costraints..)
But the application works.
this is my faces-config file:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<action-listener>org.primefaces.application.DialogActionListener</action- listener>
<navigation-handler>org.primefaces.application.DialogNavigationHandler</navigation-handler>
<view-handler>org.primefaces.application.DialogViewHandler</view-handler>
<locale-config>
<default-locale>de</default-locale>
<supported-locale>en</supported-locale>
</locale-config>
<resource-bundle>
<base-name>Messages</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
Messages is the base file names for the properties.The files are called Messages.properties (default) and Messages_de.properties.
The reason of this problem is that http://java.sun.com/xml/ns/javaee/web-facesconfig_2_2.xsd address is invalid. Therefore, I can advise you to use following faces-config.xml file
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-facesconfig_2_2.xsd">
<application>
<resource-library-contracts>
<contract-mapping>
<url-pattern>/user/*</url-pattern>
<contracts>blue</contracts>
</contract-mapping>
<contract-mapping>
<url-pattern>*</url-pattern>
<contracts>red</contracts>
</contract-mapping>
</resource-library-contracts>
</application>
</faces-config>

Resources