2

I have a simple JSF application with 2 .xhtml files. When I run the application, the 1st page displayed is welcome.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">
<h:head>
<title>Final Project</title>
</h:head>
<h:body bgcolor="white">
<div align="center" style="border:5px outset blue;">Welcome to the Product         Inventory     Application</div>
<br></br>
<br></br>
    <h:commandButton value="View All Products" action="allProducts"/>
</h:body>

It displays fine, but when I press the View All Products button, I expect it to display the allProducts.xhtml facelet. But when I click the button, nothing happens at all, no exception or anything. The allProducts.xhtml page is just:

<?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:head>
<title>All Products</title>
</h:head>
<h:body bgcolor="white">
<h3>Test</h3>
</h:body>
</html>
user1154644
  • 4,491
  • 16
  • 59
  • 102

2 Answers2

4

The problem is that an UICommand (<h:commandButton>, <h:commandLink> and similars) must be inside a form i.e. <h:form>. Change your welcome.xhtml page to:

<!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">
<h:head>
<title>Final Project</title>
</h:head>
<h:body bgcolor="white">
<div align="center" style="border:5px outset blue;">Welcome to the Product         Inventory     Application</div>
<br></br>
<br></br>
    <h:form>
        <h:commandButton value="View All Products" action="allProducts"/>
    </h:form>
</h:body>

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
1

I don't know the precise detail by heart, but here is the general idea. The action attribute of the button on the welcome page is referring to an 'allProducts' method of the backing bean. That method would have to return the string 'allProducts.xhtml' in order to have JSF present the products page. So you have to introduce a backing bean for the welcome page and endow that class with a method 'allProducts'.

Rudi Angela
  • 1,463
  • 1
  • 12
  • 20
  • 2
    Not necessarily. You can just write the name of the view you want to forward or calling a method from a backing bean that returns a `String` containing (check it) *the name of the view you want to forward*. – Luiggi Mendoza Dec 13 '12 at 00:45