Command button not visible in JSF page - jsf

Im trying to test a sample JSF project but for some reason the command button are not being displayed that is given in the below page,
<?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">
<h:body>
<h2>Implicit Navigation</h2>
<hr />
<h:form>
<h3>Using Managed Bean</h3>
<h:commandButton action="#{navigationController.moveToPage1}"
value="Page1" />
<h3>Using JSF outcome</h3>
<h:commandButton action="page2" value="Page2" />
</h:form>
<br/>
<h2>Conditional Navigation</h2>
<hr />
<h:form>
<h:commandLink action="#{navigationController.showPage}"
value="Page1">
<f:param name="pageId" value="1" />
</h:commandLink>
<h:commandLink action="#{navigationController.showPage}"
value="Page2">
<f:param name="pageId" value="2" />
</h:commandLink>
<h:commandLink action="#{navigationController.showPage}"
value="Home">
<f:param name="pageId" value="3" />
</h:commandLink>
</h:form>
<br/>
<h2>"From Action" Navigation</h2>
<hr />
<h:form>
<h:commandLink action="#{navigationController.processPage1}"
value="Page1" />
<h:commandLink action="#{navigationController.processPage2}"
value="Page2" />
</h:form>
<br/>
<h2>Forward vs Redirection Navigation</h2>
<hr />
<h:form>
<h3>Forward</h3>
<h:commandButton action="page1" value="Page1" />
<h3>Redirect</h3>
<h:commandButton action="page1?faces-redirect=true"
value="Page1" />
</h:form>
</h:body>
</html>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtm</url-pattern>
</servlet-mapping>
</web-app>
faces.config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
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_0.xsd"
version="2.0">
<navigation-rule>
<from-view-id>home.xhtml</from-view-id>
<navigation-case>
<from-action>#{navigationController.processPage1}</from-action>
<from-outcome>page</from-outcome>
<to-view-id>page1.jsf</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{navigationController.processPage2}</from-action>
<from-outcome>page</from-outcome>
<to-view-id>page2.jsf</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
view source html file
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<h2>Implicit Navigation</h2>
<hr />
<h:form>
<h3>Using Managed Bean</h3>
<h:commandButton action="#{navigationController.moveToPage1}"
value="Page1" />
<h3>Using JSF outcome</h3>
<h:commandButton action="page2" value="Page2" />
</h:form>
<br/>
<h2>Conditional Navigation</h2>
<hr />
<h:form>
<h:commandLink action="#{navigationController.showPage}"
value="Page1">
<f:param name="pageId" value="1" />
</h:commandLink>
<h:commandLink action="#{navigationController.showPage}"
value="Page2">
<f:param name="pageId" value="2" />
</h:commandLink>
<h:commandLink action="#{navigationController.showPage}"
value="Home">
<f:param name="pageId" value="3" />
</h:commandLink>
</h:form>
<br/>
<h2>"From Action" Navigation</h2>
<hr />
<h:form>
<h:commandLink action="#{navigationController.processPage1}"
value="Page1" />
<h:commandLink action="#{navigationController.processPage2}"
value="Page2" />
</h:form>
<br/>
<h2>Forward vs Redirection Navigation</h2>
<hr />
<h:form>
<h3>Forward</h3>
<h:commandButton action="page1" value="Page1" />
<h3>Redirect</h3>
<h:commandButton action="page1?faces-redirect=true"
value="Page1" />
</h:form>
</h:body>
</html>
Navigationcontroller
package com.tutorialspoint.test;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
#ManagedBean(name = "navigationController", eager = true)
#RequestScoped
public class NavigationController implements Serializable {
private static final long serialVersionUID = 1L;
#ManagedProperty(value="#{param.pageId}")
private String pageId;
public String moveToPage1(){
return "page1";
}
public String moveToPage2(){
return "page2";
}
public String moveToHomePage(){
return "home";
}
public String processPage1(){
return "page";
}
public String processPage2(){
return "page";
}
public String showPage(){
if(pageId == null){
return "home";
}
if(pageId.equals("1")){
return "page1";
}else if(pageId.equals("2")){
return "page2";
}else{
return "home";
}
}
public String getPageId() {
return pageId;
}
public void setPageId(String pageId) {
this.pageId = pageId;
}
}
page1.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:body>
<h2>This is Page1</h2>
<h:form>
<h:commandButton action="home?faces-redirect=true"
value="Back To Home Page" />
</h:form>
</h:body>
</html>
page2.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:body>
<h2>This is Page2</h2>
<h:form>
<h:commandButton action="home?faces-redirect=true"
value="Back To Home Page" />
</h:form>
</h:body>
</html>

It's already been answered above, but I'll be specific. In your web.xml the <url-pattern> is specified as *.xhtm and your jsf files have a .xhtml extension. Note the missing letter in the pattern specification. Because of that your files aren't recognized and routed through the faces servlet. Just add the 'l' to the url-pattern in the web.xml and it should start working.

Related

Value not loaded into JSF form for editing

index.xhtml
This page lists the product information where user can edit or delete products. When I click on edit link, the page gets redirected to editProduct.xhtml but the values are not loaded on the form. An empty form gets loaded. Values for selected productId gets printed in netbeans console but not in the jsf form.
<?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://xmlns.jcp.org/jsf/html"
xmlns:fcore="http://java.sun.com/jsf/core">
<h:head>
<title>Product </title>
</h:head>
<h:body>
<br/>
<center>
<h2> Product Lists</h2>
<fcore:view>
<h:form>
<h:dataTable var="lists" value="#{productController.getAllProduct()}" border="1" cellpadding="10">
<h:column>
<fcore:facet name="header">Product Id</fcore:facet>
<h:outputText value="#{lists.productId}" />
</h:column>
<h:column>
<fcore:facet name="header">Product Name</fcore:facet>
<h:outputText value="#{lists.productName}" />
</h:column>
<h:column>
<fcore:facet name="header">Product Price</fcore:facet>
<h:outputText value="#{lists.productPrice}" />
</h:column>
<h:column>
<fcore:facet name="header">Product Quantity</fcore:facet>
<h:outputText value="#{lists.productQty}" />
</h:column>
<h:column>
<fcore:facet name="header">Product Description</fcore:facet>
<h:outputText value="#{lists.productDesc}" />
</h:column>
<h:column>
<fcore:facet name="header">Purchase Date</fcore:facet>
<h:outputText value="#{lists.purchaseDate}" />
</h:column>
<h:column>
<fcore:facet name="header">Edit</fcore:facet>
<h:commandLink action="editProduct" actionListener="#{productController.findProductById(lists.productId)}" value="Edit">
<fcore:param name="id" value="#{lists.productId}"/>
</h:commandLink>
</h:column>
<h:column>
<fcore:facet name="header">Delete</fcore:facet>
<h:commandLink action="index" actionListener="#{productController.deleteCategory(lists.productId)}" onclick="return confirm('Are You Sure ?')" value="Delete">
<fcore:param name="id" value="#{lists.productId}"/>
</h:commandLink>
</h:column>
</h:dataTable>
<h:commandLink value="Add New Product" action="addProduct" />
</h:form>
</fcore:view>
</center>
</h:body>
</html>
here is the java bean along with getters and setters.
ProductManagedBean.java
#ManagedBean
#RequestScoped
public class ProductManagedBean {
private int productId;
private String productName;
private double productPrice;
private int productQty;
private String productDesc;
private Date purchaseDate;
//getters and setter here ...
}
here is the controller for fetching the values. The selected productId's value gets printed in console but not in the jsf form.
#ManagedBean(name = "productController")
public class ProductController {
public ProductManagedBean findProductById(int productId) {
ProductManagedBean productManagedBean = new ProductManagedBean();
String query = "SELECT * FROM product WHERE proId = ?";
try {
DbConnection.DbDriver();
pstat = DbConnection.con.prepareStatement(query);
pstat.setInt(1, productId);
resultSet = pstat.executeQuery();
System.out.println("\n--- Product Id = " + productId);
while (resultSet.next()) {
productManagedBean.setProductId(resultSet.getInt("proId"));
productManagedBean.setProductName(resultSet.getString("proName"));
productManagedBean.setProductPrice(resultSet.getDouble("proPrice"));
productManagedBean.setProductQty(resultSet.getInt("proQty"));
productManagedBean.setProductDesc(resultSet.getString("proDesc"));
productManagedBean.setPurchaseDate(resultSet.getDate("proDate"));
System.out.println(productManagedBean.getProductId() + "\t" + productManagedBean.getProductName()
+ "\t" + productManagedBean.getProductPrice() + "\t" + productManagedBean.getProductQty()
+ "\t" + productManagedBean.getProductDesc() + "\t" + productManagedBean.getPurchaseDate());
}
} catch (SQLException ex) {
Logger.getLogger(ProductController.class.getName()).log(Level.SEVERE, null, ex);
}
public void update(ProductManagedBean productManagedBean) {
String query = "UPDATE product SET proName = ?, proPrice = ?, proQty = ?, proDesc = ?, proDate = ? "
+ "WHERE proId = ?";
try {
DbConnection.DbDriver();
pstat = DbConnection.con.prepareStatement(query);
pstat.setString(1, productManagedBean.getProductName());
pstat.setDouble(2, productManagedBean.getProductPrice());
pstat.setInt(3, productManagedBean.getProductQty());
pstat.setString(4, productManagedBean.getProductDesc());
pstat.setDate(5, new Date(productManagedBean.getPurchaseDate().getTime()));
pstat.setInt(6, productManagedBean.getProductId());
int save = pstat.executeUpdate();
if (save > 0) {
System.out.println("\n\n---- New Product Saved ---- " + productManagedBean.getProductName() + " --- \n");
}
} catch (SQLException ex) {
Logger.getLogger(ProductController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
here is the editProduct.xhtml page
<?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://xmlns.jcp.org/jsf/html"
xmlns:f = "http://java.sun.com/jsf/core"
xmlns:fcore="http://java.sun.com/jsf/core">
<h:head>
<title>Update Product</title>
</h:head>
<h:body>
<center>
<fcore:view>
<h:form>
<h:panelGrid border="1" cellpadding="10" columns="2">
<h:outputLabel value="Product Id"/>
<h:inputText value="#{productManagedBean.productId}" >
</h:inputText>
<h:outputLabel value="Product Name"/>
<h:inputText value="#{productManagedBean.productName}" required="true">
</h:inputText>
<h:outputLabel value="Product Price"/>
<h:inputText value="#{productManagedBean.productPrice}" required="true">
</h:inputText>
<h:outputLabel value="Product Qty"/>
<h:inputText value="#{productManagedBean.productQty}" required="true">
</h:inputText>
<h:outputLabel value="Product Desc"/>
<h:inputText value="#{productManagedBean.productDesc}" required="true">
</h:inputText>
<h:outputLabel value="Product Date"/>
<h:inputText value="#{productManagedBean.purchaseDate}" required="true">
<f:convertDateTime pattern="yyyy-MM-dd"/>
</h:inputText>
<h:commandLink value="Update" action="index" actionListener="#{productController.update(productManagedBean)}" />
</h:panelGrid>
</h:form>
</fcore:view>
</center>
</h:body>
here is web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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-app_3_1.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>
The ProductManagedBean must be SessionScop to load the values on the editProduct.xhtml.

PrimeFaces Push Faces Message Notify not working

I have problem with the Push Technology, the project is to:
Primefaces 3.5
WebLogic 12c
Atmosphere Runtime 2.2
The configuration is:
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>cupertino</param-value>
</context-param>
<servlet>
<servlet-name>Push Servlet</servlet-name>
<servlet-class>org.primefaces.push.PushServlet</servlet-class>
</servlet>
The View:
#Named
#SessionScoped
public class FrmNotificacion implements Serializable
...
public void send() {
...
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Usuario: "+getUser(), getMensajeDetalle());
PushContext pushContext = PushContextFactory.getDefault().getPushContext();
pushContext.push("/notificacion", msg);
System.out.println("......");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
The XHTML:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:p="http://primefaces.org/ui" >
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Enivar Notificación</title>
</h:head>
<h:body>
<h:form id="frmPrincipal" >
<p:growl id="message" showDetail="true" />
<p:panel header="Notificación" >
<p:panelGrid columns="2" columnClasses="ui-widget-header,,ui-widget-header,,ui-widget-header,," >
<p:outputLabel id="lblNombre" value="Ingrese Usuario a Notificar:" />
<p:inputText id="txtNombre" value="#{frmNotificacion.user}" />
<p:outputLabel id="lblMensaje" value="Ingresar Mensaje Notificación" />
<p:inputTextarea id="txtMensaje" autoResize="true" value="#{frmNotificacion.mensajeDetalle}"/>
</p:panelGrid>
<p:commandButton id="btnNotifica"
value="Notifica"
update="message"
actionListener="#{frmNotificacion.send()}"
icon="ui-icon-arrowthickstop-1-e" />
</p:panel>
</h:form>
<p:socket onMessage="handleMessage" channel="/notificacion" />
<script type="text/javascript">
function handleMessage(msg) {
msg.severity = 'info';
grow.show([msg]);
}
</script>
</h:body>
</html>
But the action notify push Faces Message not working
Maybe the configuration is wrong
It also shows me this console:
INFO: Installed AtmosphereInterceptor Atmosphere LifeCycle with priority AFTER_DEFAULT
03-feb-2015 9:09:50 org.atmosphere.cpr.AsynchronousProcessor action
ADVERTENCIA: Websocket protocol not supported
03-feb-2015 9:09:50 org.atmosphere.cpr.AsynchronousProcessor action
GRAVE: Invalid request state. AsyncContext#startAsync not supported. Make sure async-supported is set to true in web.xml
03-feb-2015 9:09:52 org.atmosphere.cpr.AtmosphereFramework$4 run
INFO: Latest version of Atmosphere's JavaScript Client 2.2.6
Change JavaScript:
Last:
<script type="text/javascript">
function handleMessage(msg) {
msg.severity = 'info';
grow.show([msg]);
}
</script>
For This:
<script type="text/javascript">
function handleMessage(data) {
alert("Hola");
}
</script>
as I do that I work at the FacesMessage ¿?

JSF Backing Bean (EJB) not called from JSF (using glassfish)

I'm trying to create a questionnaire website using GlassFish, JSF/Primefaces and for database, I'm using the embedded derby DB of Glassfish.
I followed this video tutorial (http://www.youtube.com/watch?v=aBjlR9HoR50) which is really good but I haven't managed to get my project to work properly.
My project deploys successfully on Glassfish but when I see the webpage on the browser, it is obvious that the backing bean is not called from the xhtml page.
Here is the backing bean code:
package ac.hw.questionnaire;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;
import ac.hw.questionnaire.model.Question;
/**
* Session Bean implementation class QuestionsEJB
*/
#Stateless
#LocalBean
#Named
public class QuestionsEJB {
private Logger logging = Logger.getLogger(this.getClass().getName());
#PersistenceContext (unitName="QuestionnaireEmbeddedDB")
EntityManager em;
private List<Question> questions;
private String name;
/**
* Default constructor.
*/
private EntityManager getEntityManager() {
if (em==null){
logging.info("EntityManager is null!");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("QuestionnaireEmbeddedDB");
em = emf.createEntityManager();
if (em==null){
logging.info("EntityManager is STILL null!");
}
}
return em;
}
public QuestionsEJB() {
// TODO Auto-generated constructor stub
logging.info("Logger: Constructor of QuestionsEJB called");
if (getEntityManager()==null){
return;
}
List resultList = em.createQuery("select q from Question q").getResultList();
if (resultList==null){
logging.info("ResultList is null");
}else{
logging.info("Constructor/Retrieved "+resultList.size()+" questions");
}
}
public List<Question> getQuestions() {
System.out.println("getQuestions method called");
if (questions==null){
this.questions = em.createQuery("select q from questions q").getResultList();
System.out.println("Retrieved "+questions.size()+" questions");
}
return questions;
}
public String getMyText(){
return "myTextFromEJB";
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
public String getName() {
this.logging.info("Get name!");
return name;
}
public void setName(String name) {
this.logging.info("Set name!");
this.name = name;
}
}
Here is my xhtml page:
<!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"
xmlns:p="http://primefaces.org/ui">
<ui:composition template="WEB-INF/template.xhtml">
<ui:define name="content">
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="myText" value="MyText is:"></h:outputLabel>
<h:outputText id="myText" value="#{QuestionsEJB.myText}">
</h:outputText>
</h:panelGrid>
<h:panelGrid columns="4" cellpadding="5">
<h:outputLabel for="name" value="Name:" style="font-weight:bold" />
<p:inputText id="name" value="#{QuestionsEJB.name}" />
<p:commandButton value="Submit" update="display" />
<h:outputText value="#{QuestionsEJB.name}" id="display" />
</h:panelGrid>
<br />
<p:dataTable var="qVar" value="#{QuestionsEJB.questions}">
<h:column>#{qVar.questionText}</h:column>
</p:dataTable>
</ui:define>
</ui:composition>
</html>
When I visit the xhtml page on the browser, none of the values are displayed and the Submit button doesn't do anything which tells me that the facelet doesn't bind to the EJB.
My web.xml (inside WebContent/WEB-INF/) has the following content:
<web-app version="3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" >
<display-name>Questionnaire</display-name>
<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>
</web-app>
My beans.xml file (also inside WebContent/WEB-INF) is empty (nothing at all).
My glassfish-web.xml file (also inside WebContent/WEB-INF) has the following content:
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
<context-root>/QuestionnaireEmbeddedDB</context-root>
<parameter-encoding default-charset="UTF-8"/>
</glassfish-web-app>
I have no exceptions on the server.log and the only unorthodox thing is that the EntityManager is null until I explicitly call the getEntityManager() method on the QuestionEJB.
Why can't I get the xhtml to connect to the backing bean?
Also, the template.xhtml has the following content:
<!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>
<title><ui:insert name="title">Questionnaire</ui:insert></title>
</h:head>
<body>
<ui:debug hotkey="x"
rendered="#{initParam['javax.faces.FACELETS_DEVELOPMENT']}" />
<div id="header">
<ui:insert name="header">
<h1>Welcome to the privacy questionnaire... (header section)</h1>
<!-- include your header file or uncomment the include below and create header.xhtml in this directory -->
<!-- <ui:include src="header.xhtml"/> -->
</ui:insert>
</div>
<h:body>
<h:form id="mainForm" enctype="multipart/form-data">
<div id="content">
<ui:insert name="content">
Content area. See comments below this line in the source.
<!-- include your content file or uncomment the include below and create content.xhtml in this directory -->
<!-- <div> -->
<!-- <ui:include src="content.xhtml"/> -->
<!-- </div> -->
</ui:insert>
</div>
</h:form>
</h:body>
<div id="footer">
<ui:insert name="footer">
<br />
<br />Powered by Glassfish & Eclipse.
<br />
<!-- include your header file or uncomment the include below and create footer.xhtml in this directory -->
<!--<ui:include src="footer.xhtml"/> -->
</ui:insert>
</div>
</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.

URL should change in JSF navigation

I have made a Login.xhtml by using Apache Myfaces 2.1 (using JSF 1.x)
When I have given my username and password on login.xhtml and one submit button is there.
When I click on goodbye button, the control goes to wsListing.xhtml but the URL in the browser remain as
http://hostid:8080/TestClient/faces/login.xhtml
I want it to be changed as
http://hostid:8080/TestClient/faces/wsListing.xhtml
I have attached the code for the same, please suggest me some solution, I am new to JSF.
Login.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:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui" xmlns:lang="en">
<f:view>
<head>
<title>Test Client Login</title>
</head>
<h:form id="loginForm">
<table>
<tr>
<h:outputLabel for="username">
<h:outputText id="usernameLabel" value="Enter Username:" />
</h:outputLabel>
<h:inputText id="username" value="#{loginBean.username}"
required="true">
<f:validateLongRange minimum="1" maximum="500" />
</h:inputText>
</tr>
<tr>
<h:outputLabel for="password">
<h:outputText id="passwordLabel" value="Enter Password:" />
</h:outputLabel>
<h:inputText id="password" value="#{loginBean.password}"
required="true">
<f:validateLongRange minimum="1" maximum="500" />
</h:inputText>
</tr>
<tr>
<h:commandButton id="goodbyeCommand" type="submit" value="Goodbye"
action="#{loginBean.goodbye}" immediate="true" />
</tr>
</table>
</h:form>
</f:view>
</html>
LoginBean.java
package com.example;
/**
* Managed Bean for Login
*
*/
public class LoginBean {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String goodbye() {
return "success";
}
}
faces-config.xml
<?xml version="1.0"?>
<faces-config version="1.2" 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_1_2.xsd">
<managed-bean>
<description>Login Bean</description>
<managed-bean-name>loginBean</managed-bean-name>
<managed-bean-class>com.example.LoginBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<description>Navigation from the hello page.</description>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/wsListing.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
You need to send a redirect. Add <redirect /> to the <navigation-case />.
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/wsListing.xhtml</to-view-id>
<redirect />
</navigation-case>
Unrelated to the concrete problem, MyFaces 2.1 is a JSF 2.1 implementation. Why are you forcing the webapp to run in JSF 1.2 modus? You miss so many JSF 2.x advantages.

Resources