JSF Validation no wok in template - jsf

JSF validation are not working When i submit ,I under the myfaces(2.1.10) and mojarra(2.1.21) has tried, as same results.here is my code.Theoretically not execute onSubmit() and display error messages in m_name.But in fact is execute onSubmit() and not display error message.
template.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:rich="http://richfaces.org/rich">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Cache-Control" content="no-cache" />
<title>Title</title>
</h:head>
<h:body>
<ui:insert name="body"></ui:insert>
</h:body>
</html>
template client file templatevalidation.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/template.xhtml">
<ui:define name="body">
<h:form>
<div>
<h:inputText id="name" value="#{templateBean.name}">
<f:validateRequired />
<f:validateLength minimum="5" />
</h:inputText>
<h:message id="m_name" for="name" />
</div>
<div>
<h:commandButton action="#{templateBean.onSubmit}" value="submit">
<f:ajax execute="name" render="m_name" />
</h:commandButton>
</div>
</h:form>
</ui:define>
</ui:composition>
here is my bean TemplateBean.class
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class TemplateBean implements Serializable{
private static final long serialVersionUID = 9009393522101806766L;
private String name;
public void onSubmit(){
System.out.println("Name: " + name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

You need to update your <h:message> component after the AJAX call to show the error message using the render attribute in your <f:ajax> tag:
<h:message id="nameMsg" for="name" />
<h:commandButton action="#{templateBean.onSubmit}" value="submit">
<f:ajax execute="name" render="nameMsg" />
</h:commandButton>

Must use hibernate-validation
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.0.Final</version>
</dependency>

Related

p:remoteCommand run on page load instead of button's onclick event in javascript

I have to call a remote command through javascript. The remote command is working fine but it invokes on page load instead of the button's onclick event.
Following is my xhtml file code as well as the bean code.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Crowd Flow</title>
</h:head>
<f:event listener="#{task.loadTaskByTaskId(param['task_id'])}"
type="preRenderView" />
<h:body bgcolor="white">
<h:outputScript>
function getCurrentLocation() {
testingCommand();
}
</h:outputScript>
<h:panelGroup id="header" layout="block">
<h3>Welcome to the task.</h3>
</h:panelGroup>
<h:panelGroup id="taskLayout" layout="block">
<h:outputText value="Category is #{task.taskCategory}"></h:outputText>
<h:outputText value="Task id is is #{task.id}"></h:outputText>
<c:if test="#{param['task_id']=='9'}">
<h:form style="width:400px;" id="newTagTaskForm">
<p:remoteCommand name="testingCommand"
actionListener="#{task.testingCommand()}" autoRun="false" />
Task Name : <h:outputText id="taskName"
value="#{task.newPlaceTagTask.name}"></h:outputText>
<br />
Task Description : <h:outputText id="taskDescription"
value="#{task.newPlaceTagTask.description}"></h:outputText>
<br />
Address : <h:outputText id="taskAddress"
value="#{task.newPlaceTagTask.location.address}"></h:outputText>
<br />
Image : <h:graphicImage width="50" height="50"
url="#{task.newPlaceTagTask.location.image}" />
<br />
<h:inputHidden value="#{task.userId}" id="taskPlaceTagUserId"
name="taskPlaceTagUserId" />
<h:inputHidden value="#{task.newPlaceTagTask.location.id}"
id="taskPlaceTagLocationId" name="taskPlaceTagLocationId" />
<h:commandButton id="testing" value="submit" type="submit" onclick="getCurrentLocation();return false;" ></h:commandButton>
</h:form>
</c:if>
</h:panelGroup>
</h:body>
</html>
Here is the bean function, bean is view scoped.
package com.project.crowdsource.beans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean(name="task")
#ViewScoped
public class TaskBean implements Serializable {
public TaskBean() {
//initiating user's data access object.
taskDao = new TaskDAO();
}
public String testingCommand() {
System.out.println("in action testing command");
return "";
}
}
Any help would be highly appreciated.
Thanks,

primefaces inputext onblur event not working

Hello I have an inputText on blur event that will send a signal to the backing bean, this bean contains a Boolean variable that determines if another input text will be enabled or not ... but I can not make this work, this is my code:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
template="/Template.xhtml">
<ui:define name="content">
<h:form id="someForm">
<p:growl id="msg" showDetail="true" life="3000"/>
<p:panelGrid border="0" id="panel">
<p:row>
<p:column width="350">
Title
</p:column>
<p:column colspan="2">
<p:inputText id="someId" value="#{someBean.somePropertie}">
<p:ajax event="blur" update="anotherInput" listener="#{someBean.onEvent}" />
</p:inputText>
</p:column>
</p:row>
<p:row>
<p:column width="350">title 2</p:column>
<p:column colspan="2">
<p:inputText id="anotherInput" converter="toUpperCaseConverter" value="#{someBean.somePropertie2}"
disabled="#{someBean.bDisabled}"
/>
</p:column>
</p:row>
</p:panelGrid>
</h:form>
</ui:define>
</ui:composition>
Backing bean:
#ManagedBean
#SuppressWarnings("serial")
public class SomeBean implements Serializable{
private boolean bDisabled;
private String somePropertie;
private String somePropertie2;
public void onEvent(){
System.out.println("DO SOMETHING");
this.bDisabled = true;
}
... getters and setters of properties and boolean ....
}
this is the main tamplate:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<h:head>
<title>..:: XXXXX ::..</title>
<meta http-equiv="Pragma" content="no-cache" />
<link rel="stylesheet" type="text/css" href="#{request.contextPath}/css/default.css"/>
<style type="text/css">
.ui-growl{
position:absolute;
top:20%;
left:50%;
z-index:9999;
}
.ui-widget,.ui-widget .ui-widget {
font-family: Trebuchet MS;
}
</style>
</h:head>
<h:body style="background-color: #E1E1E1;">
<div id="page">
<div id="divHeader" style="height: 70px;">
<ui:insert name="header" >
<ui:include src="header.xhtml" />
</ui:insert>
</div>
<div id="divMenu" style="height: 50px;">
<ui:insert name="menu">
<ui:include src="menu.xhtml" />
</ui:insert>
</div>
<div id="divContent">
<ui:insert id="content" name="content" >
<ui:include src="content.xhtml" />
</ui:insert>
</div>
</div>
</h:body>
</html>
the converter:
#FacesConverter("toUpperCaseConverter")
public class ToUpperCaseConverter implements Converter {
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return (String) value;
}
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return (value != null) ? value.toUpperCase() : null;
}
}
any idea ???
Please help :(
Your question is not complete, since it does not provide all the elements to reproduce the bug. You haven't provided the page that calls your element, neither the converter, etc.
JSF's debugger best friend is firebug. Always check for javascript errors and enable the network tab to see each request response body, which may contain "silent" error messages.
I'll take my risk trying to guess your problem :-)
Here's your code, slightly changed to run in my primefaces 4.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>You NEED a header</title>
</h:head>
<h:body>
<h:form>
<ui:include src="index2.xhtml" />
</h:form>
</h:body>
</html>
and
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<p:growl
id="msg"
showDetail="true"
life="3000" />
<p:panelGrid
border="0"
id="panel">
<p:row>
<p:column width="350">
Title
</p:column>
<p:column colspan="2">
<p:inputText
id="someId"
value="#{someBean.somePropertie}">
<p:ajax
event="blur"
update="anotherInput"
listener="#{someBean.onEvent}" />
</p:inputText>
</p:column>
</p:row>
<p:row>
<p:column width="350">title 2</p:column>
<p:column colspan="2">
<p:inputText
id="anotherInput"
value="#{someBean.somePropertie2}"
disabled="#{someBean.bDisabled}" />
</p:column>
</p:row>
</p:panelGrid>
</ui:composition>
The managed bean is pretty much the same
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
#ManagedBean
#SuppressWarnings("serial")
public class SomeBean implements Serializable {
private boolean bDisabled;
private String somePropertie;
private String somePropertie2;
public void onEvent() {
System.out.println("DO SOMETHING");
this.bDisabled = true;
}
public boolean isbDisabled() {
return this.bDisabled;
}
public void setbDisabled(boolean bDisabled) {
this.bDisabled = bDisabled;
}
public String getSomePropertie() {
return this.somePropertie;
}
public void setSomePropertie(String somePropertie) {
this.somePropertie = somePropertie;
}
public String getSomePropertie2() {
return this.somePropertie2;
}
public void setSomePropertie2(String somePropertie2) {
this.somePropertie2 = somePropertie2;
}
}
I've noticed that if you don't omit the HEAD section from the caller page, it seems to render and work.
But if you forget the HEAD section (you must not, see Primefaces FAQ item #2), you'll render the page (quite) but you'll get some javascript errors and your page won't work. The HEAD section I am talking about is this
<h:head>
<title>You NEED a header</title>
</h:head>
This is how it looks without the HEAD section.
With the HEAD section, it seems to work. Also notice the subtle primefaces look and feel.

echo parameter with an #Named backing bean to a facelets template client

What should be the return type for getResponse and submit, and are both necessary?
When a guess is entered in either the firstForm or SecondForm, how do I echo that guess to the same webpage?
Either with ajax, and so not reloading the same page
or
loading a new page, guessResults.xhtml, for example, which echo's the guess.
backing bean, NextClient:
package dur.beans;
import dur.jpa.Client;
import dur.jpa.ClientFacadeLocal;
import java.util.concurrent.atomic.AtomicInteger;
import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
#Named("nextClient")
#ApplicationScoped
public class NextClient implements NextClientLocal {
#EJB
private ClientFacadeLocal clientFacade;
private AtomicInteger next = new AtomicInteger(1009);
private AtomicInteger guess = new AtomicInteger(0);
private final boolean correct = true;
#Override
public String getNext() {
next.addAndGet(1);
Client client = clientFacade.find(next.intValue());
return client.toString();
}
#Override
public void setGuess(int guessInt) {
guess = new AtomicInteger(guessInt);
}
#Override
public int getGuess() {
return guess.intValue();
}
//not sure what do with these methods
#Override
public String getResponse() {
return "the guess of " + guess.intValue() + " is " + correct;
}
#Override
public String submit() {
return "the guess of " + guess.intValue() + " is " + correct;
}
}
facelets template client, next.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
>
<h:head></h:head>
<h:body>
This and everything before will be ignored
<ui:composition template="template.xhtml">
<ui:define name="navigation">
<ui:include src="menu.xhtml"/>
</ui:define>
<ui:define name="main">
<h1>next bird</h1>
<p>
#{nextClient.next}
</p>
<p>
<h:panelGroup id="firstPanel">
<h:form id="firstForm">
<h:outputLabel for="input" value="First form input" />
<h:inputText id="input" value="#{nextClient.guess}" required="true" />
<h:commandButton value="Submit form" action="#{nextClient.submit}">
<f:ajax execute="#form" render="#form :secondPanel :secondForm :messages" />
</h:commandButton>
<h:message for="input" />
</h:form>
</h:panelGroup>
<h:panelGroup id="secondPanel">
<h:form id="secondForm">
<h:outputLabel for="input" value="Second form input" />
<h:inputText id="input" value="#{nextClient.guess}" required="true" />
<h:commandButton value="Submit other form" action="#{nextClient.submit}">
<f:ajax execute="#form" render="#form :firstPanel :firstForm :messages" />
</h:commandButton>
<h:message for="input" />
</h:form>
</h:panelGroup>
<h:messages id="messages" globalOnly="true" layout="table" />
</p>
</ui:define>
</ui:composition>
This and everything after will be ignored
</h:body>
</html>
see also:
http://balusc.blogspot.ca/2011/09/communication-in-jsf-20.html#AjaxRenderingOfContentWhichContainsAnotherForm
JSF 2.0 commandButton do nothing
https://javaserverfaces.java.net/nonav/docs/2.0/pdldocs/facelets/h/commandButton.html
http://docs.oracle.com/javaee/7/tutorial/doc/jsf-facelets003.htm
I'm running facelets on Glassfish, using CDI, so am using #Named and not #ManagedBean -- some of the documentation above is more geared for #ManagedBean, but I'm not sure how much that matters.
The goal is one step better than "hello world", "hello world, your guess is " would be a good result. If there's a specific manual, I don't mind a RTFM to that specific documentation. The Oracle docs are probably the best for facelets?
code:
https://github.com/THUFIR/EntAppWeb
This response.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>response</h:head>
<h:body>
This and everything before will be ignored
<ui:composition template="template.xhtml">
<ui:define name="navigation">
<ui:include src="menu.xhtml"/>
</ui:define>
<ui:define name="main">
<h1>submitted value</h1>
<p>
#{nextClient.guess}
</p>
<h2>for this bird</h2>
<p>
#{nextClient.client}
</p>
</ui:define>
</ui:composition>
This and everything after will be ignored
</h:body>
</html>
to next.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>next</h:head>
<h:body>
This and everything before will be ignored
<ui:composition template="template.xhtml">
<ui:define name="navigation">
<ui:include src="menu.xhtml"/>
</ui:define>
<ui:define name="main">
<h1>next bird</h1>
<p>
#{nextClient.next}
</p>
<p>
<h:panelGroup id="simpleGroup">
<h:form id="simpleForm">
<h:outputLabel for="input" value="First form input" />
<h:inputText id="input" value="#{nextClient.guess}" required="true" />
<h:commandButton value="submit" action="response">
</h:commandButton>
</h:form>
</h:panelGroup>
</p>
</ui:define>
</ui:composition>
This and everything after will be ignored
</h:body>
</html>
using the backing bean NextClient:
package dur.beans;
import dur.jpa.Client;
import dur.jpa.ClientFacadeLocal;
import java.util.concurrent.atomic.AtomicInteger;
import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
#Named("nextClient")
#ApplicationScoped
public class NextClient implements NextClientLocal {
#EJB
private ClientFacadeLocal clientFacade;
private AtomicInteger next = new AtomicInteger(1009);
private AtomicInteger guess = new AtomicInteger(0);
private final boolean correct = true;
private Client client = new Client();
#Override
public String getNext() {
next.addAndGet(1);
client = clientFacade.find(next.intValue());
return client.toString();
}
#Override
public void setGuess(int guessInt) {
guess = new AtomicInteger(guessInt);
}
#Override
public int getGuess() {
return guess.intValue();
}
#Override
public Client getClient() {
return client;
}
#Override
public void setClient(Client client) {
this.client = client;
}
}
outputs the submitted value to the response, along with the bird. It might make more sense to output the result to the same page, but this is sufficient.

Richfaces 4 Ajax:queue inputTextArea not queuing

I've been trying to put a queue onto a text area, and it is not cooperating. I'll list the relevant section of code below.
<h:form>
<a4j:queue requestDelay="1000" ignoreDupResponses="true"/>
<table>
<tr>
<td>
<h:outputText value="Notes:"/>
</td>
</tr>
<tr>
<td>
<h:inputTextarea value="#{MyActionBean.notes}">
<a4j:ajax event="keyup" listener="#{MyActionBean.updateNotes}"/>
</h:inputTextarea>
The notes get updated as expected, but there is no delay between requests. Is there some bug in my code, do textAreas not work for this? Any help would be appreciated.
EDIT: Just for good measure, tried the following code as well, but it didn't work either.
<h:panelGrid columns="1" width="100%">
<h:outputText value="Notes:"/>
<h:inputTextarea value="#{MyActionBean.notes}">
<a4j:ajax event="keyup" listener="#{MyActionBean.updateNotes}">
<a4j:attachQueue id="notesQueue" requestDelay="1000"/>
</a4j:ajax>
</h:inputTextArea>
</h:panelGrid>
For reference, the technology versions:
JBoss AS 7, Seam 2.3.0, Richfaces 4.2.2, JSF 2.1
For your case you need to nest a4j:attachQueue inside a4j. Try to run the code below and you'll notice that you'll get an output on the console after 15 seconds.
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<a4j:queue name='Y' requestDelay="3000" ignoreDupResponses="true"/>
<h:panelGrid columns="1" width="100%">
<h:outputText value="Notes:"/>
<h:inputTextarea value="#{MyActionBean.notes}">
<a4j:ajax event="keyup" listener="#{MyActionBean.updateNotes}">
<a4j:attachQueue requestDelay="3000"/>
</a4j:ajax>
</h:inputTextarea>
<a4j:status>
<f:facet name="start">
Please wait
</f:facet>
</a4j:status>
</h:panelGrid>
</h:form>
</h:body>
You can use this link if you need more information
UPDATE
I stripped down the code you sent to the bare minimum and I'm sorry to say that it is still working on my end with JBoss. Your issue could be somewhere else (e,g. your ajax might be failing for some reason since you told me "Please Wait" was not appearing). However, I should mention that I'm not familiar with JBoss' conversation scope so I changed it to javax.enterprise.context.SessionScoped (but I don't think that should matter because it was still working when it was requestscoped). I'm including all the code so you can test it yourself as a separate project. Also, since we are using a queue I changed the logging location from updateNotes to setReport so that I could be absolutely certain that the characters were actually being queued. When I typed "This is a test", the only output I saw was the entire String and not each character.
AssessmentCheckListAction.java
import java.io.Serializable;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
#Named(value="AssessmentChecklistAction")
#SessionScoped
public class AssessmentCheckListAction implements Serializable {
private static final long serialVersionUID = -4970638494437413924L;
private String report;
public AssessmentCheckListAction() {
}
public void updateNotes() {
//FacesContext.getCurrentInstance().getExternalContext().log(report);
//Logging the setter to make sure everything was queued
}
public String getReport() {
return report;
}
public void setReport(String report) {
FacesContext.getCurrentInstance().getExternalContext().log(report);
this.report = report;
//left blank
}
index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
template="template.xhtml">
<ui:define name="content">
<h:form>
<a4j:queue requestDelay="3000" ignoreDupResponses="true"/>
<h:panelGrid columns="1" width="100%">
<h:outputText value="Notes:"/>
<h:inputTextarea value="#{AssessmentChecklistAction.report}">
<a4j:ajax event="keyup" listener="#{AssessmentChecklistAction.updateNotes}">
<a4j:attachQueue requestDelay="3000"/>
</a4j:ajax>
</h:inputTextarea>
<a4j:status>
<f:facet name="start">
Please wait
</f:facet>
</a4j:status>
</h:panelGrid>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
template.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Cache-Control" content="no-store"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
</h:head>
<h:body>
<ui:insert name="content"/>
</h:body>
</html>

Changing language using commandLink

I am trying to change the language using p:commandLink.
I have a template (template.xhtml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:pm="http://primefaces.org/mobile"
xmlns:p="http://primefaces.org/ui">
<f:loadBundle
basename="labels.ClientLabels"
var="labels" />
<h:head>
<title><ui:insert name="windowTitle" /></title>
</h:head>
<h:body>
<f:view locale="#{localeChanger.locale}">
<pm:page title="aaaa" swatch="a">
<pm:view id="main">
<!-- HEADER -->
<ui:insert name="header">
<!-- the default header is used if no replacement for header is specified when the template is used -->
<pm:header fixed="true" title="Header" swatch="a">
<f:facet name="left">
<p:commandLink action="#{localeChanger.setEnglish}" onchange="submit()">
<h:graphicImage value="image/uk.png" />
</p:commandLink>
</f:facet>
<f:facet name="right">
<p:commandLink action="#{localeChanger.setGerman}" onchange="submit()">
<h:graphicImage value="image/de.png" />
</p:commandLink>
</f:facet>
</pm:header>
</ui:insert>
<!-- CONTENT -->
<ui:insert name="content">
<!-- default content -->
<pm:content>
<h:form></h:form>
</pm:content>
</ui:insert>
<!-- FOOTER -->
<ui:insert name="footer">
<!-- the default footer -->
<pm:footer fixed="true" swatch="a">
<pm:navBar>
<p:button outcome="info" value="#{labels.Info}" icon="info" />
<p:button outcome="userlogin" value="#{labels.User}" icon="star" />
</pm:navBar>
</pm:footer>
</ui:insert>
</pm:view>
</pm:page>
</f:view>
</h:body>
</html>
The userlogin.xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:pm="http://primefaces.org/mobile"
xmlns:p="http://primefaces.org/ui">
<head></head>
<body>
<ui:composition template="template.xhtml">
<ui:define name="content">
<pm:content>
<h:outputText value="#{labels.IntroductionText}" />
<h:form>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="username" value="#{labels.UserName}" />
<p:inputText value="#{login.username}" id="username"
required="true" />
<h:outputLabel for="password" value="#{labels.Password}" />
<h:inputSecret value="#{login.password}" id="password"
required="true" label="password" />
<f:facet name="footer">
<p:commandButton id="loginButton" value="#{labels.Login}" />
</f:facet>
</h:panelGrid>
</h:form>
</pm:content>
</ui:define>
</ui:composition>
</body>
</html>
The LocaleChanger.java:
#ManagedBean
#SessionScoped
public class LocaleChanger implements Serializable{
private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
public Locale getLocale() {
return locale;
}
public String getLanguage() {
System.out.println(locale.getLanguage());
return locale.getLanguage();
}
public void setLanguage(String language) {
locale = new Locale(language);
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}
public void setEnglish(){
System.out.println("english");
setLanguage("en");
}
public void setGerman(){
System.out.println("german");
setLanguage("de");
}
}
The faces-config.xml:
<managed-bean>
<managed-bean-name>localeChanger</managed-bean-name>
<managed-bean-class>test.LocaleChanger</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>de</supported-locale>
</locale-config>
<default-render-kit-id>PRIMEFACES_MOBILE</default-render-kit-id>
</application>
When I am clicking the commandLinks nothing happens.The two methods setEnglish and setGerman are invoked but no change in labels.
My property files are:
ClientLabels_en.properties and
ClientLabels_de.properties
I am using embedded Jetty, JSF 2.0, PrimeFaces 3.3 and PrimeFaces Mobile 0.9.3.
Thank you very much.
UPDATE
The file ClientLabels_en.properties:
Info = Info
User = User
IntroductionText = Please enter your username and password
UserName = Username:
Password = Password:
Login = Login
The file ClientLabels_de.properties is the same but with german text for the values.
I also changed the name of the file in ClientLabels_de_DE.properties but the problem still remains.

Resources