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 ¿?
Related
I try to understand why I get a javax ViewExpiredException on my simple webapp - but I don't seem to understand what is causing the view to expire.
This is the register.jsf:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:b="http://bootsfaces.net/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
template="pageTemplate.xhtml">
<ui:define name="content">
<b:panel title="Registrierung" look="primary">
<h:form>
<h:panelGrid>
<h:outputText value="Name:"/>
<b:inputText value="#{registerController.user.name}" placeholder="Robina Kuh"/>
<h:outputText value="E-Mail:"/>
<b:inputText value="#{registerController.user.email}" placeholder="robina.kuh#oc.com" size="32">
<f:facet name="prepend">
<h:outputText value="#" />
</f:facet>
</b:inputText>
<b:commandButton value="Registrieren" icon="envelope" action="#{registerController.registerUser}"/>
</h:panelGrid>
</h:form>
</b:panel>
</ui:define>
</ui:composition>
The template:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:b="http://bootsfaces.net/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Lunch</title>
</h:head>
<h:body style="padding: 60px;">
<ui:include src="topMenu.xhtml" />
<ui:insert name="content">
<b:container>
<b:jumbotron>
<h1>Da ist wohl etwas schiefgelaufen... Sorry!</h1>
</b:jumbotron>
</b:container>
</ui:insert>
</h:body>
</html>
This is the controller:
#Named("registerController")
#SessionScoped
public class RegisterController implements Serializable {
#Inject
private UserManager userManager;
private User user;
private Logger logger = Logger.getLogger(RegisterController.class);
public RegisterController() {
logger.debug("Created RegisterController");
user = new User();
if(user != null)
logger.debug("Name: " + user.getName()
+"\nEmail: " + user.getEmail());
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String registerUser() {
logger.debug("registerUser called"
+ "\n Name: " + user.getName()
+"\nEmail: " + user.getEmail());
userManager.addUser(user);
logger.debug("registerUser end");
return "benutzer.jsf";
}
}
It even is not entering the action method of my commandButton when I try to debug it (I am using the Bootsfaces Framework but I don't think this has anything to do with that Framework).
Setting the save state to client works but I would like to understand what is the problem? From what I am understanding of JSF this should work without pushing the state to the client side.
Am I missing something fundamental?
I deployed the webapp to a widlfly 9 server.
It was a bug in the used wildfly version - just used the new widlfly 10 final release and it is working as it should.
I´m trying to upload some files with the richfaces fileupload.
This is my .xhtml:
<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:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head></h:head>
<h:body>
<ui:composition
template="/WEB-INF/templates/application/main-template.xhtml">
<ui:define name="contentheader">
<h:outputText value="#{messages.incoming_malfunction_header}" />
</ui:define>
<h:form id="controls_form">
<ui:define name="contentbody">
<rich:fileUpload maxFilesQuantity="2" immediateUpload="true"
fileUploadListener="#{isiDataTransferPM.uploadListener}"
listWidth="280px">
<a4j:ajax event="uploadcomplete" render="controls_form" />
</rich:fileUpload>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
This is my uploadeListener:
public void uploadListener(FileUploadEvent event) throws Exception {
final UploadedFile item = event.getUploadedFile();
if (selectedEntity.equals(ENTITY_WORK)) {
//addToList
}
This is my web.xml config:
<context-param>
<param-name>org.richfaces.fileUpload.maxRequestSize</param-name>
<param-value>10000000</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.fileUpload.createTempFiles</param-name>
<param-value>true</param-value>
</context-param>
I use the following libraries:
jboss-jsf-api_2.1_spec-2.0.7.Final-redhat-1.jar
richfaces-components-ui-4.2.3.Final.jar
richfaces-components-api-4.2.3.Final.jar
richfaces-core-api-4.2.3.Final.jar
richfaces-core-impl-4.2.3.Final.jar
Debbuging the code shows, that the listener is called. But after calling this listener,the progress bar of the fileupload shows still 0 %. Nothing happens anymore.
Any Ideas.
Thanks
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>
I'm trying to understand why a viewscoped view is recreating on every ajax call.
I have a test case, and it's working fine without composition templating.
I find questions that seem similar to my problem but the solutions don't work for me, like setting javax.faces.PARTIAL_STATE_SAVING to false or javax.faces.STATE_SAVING_METHOD to client.
Here's a sample:
page.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:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<h:body>
<h:form id="pageForm">
<p:commandButton
value="Choose an option..."
actionListener="#{pageController.handleOptionChoose}"
update="selectedOption"
/>
<br/>
<h:panelGrid columns="2" cellpadding="3">
<p:outputLabel for="selectedOption" value="Selected option: "/>
<p:outputLabel id="selectedOption" value="#{pageController.selectedOption}"/>
</h:panelGrid>
<br/><br/>
<p:commandButton
value="Edit option"
actionListener="#{pageController.doAnythingWithTheOption}"
update="editedOption"
/>
<br/>
<h:panelGrid columns="2" cellpadding="3">
<p:outputLabel for="editedOption" value="Edited option: "/>
<p:outputLabel id="editedOption" value="#{pageController.editedOption}"/>
</h:panelGrid>
</h:form>
<p:dialog
id="dialogOption_Dialog"
header="Choose an option"
widgetVar="dialogOption_Widget"
modal="true"
appendTo="#(body)"
>
<h:form id="dialogOption_Form">
<h:panelGrid columns="1">
<p:selectOneButton id="dialogOptionChoose" value="#{pageController.selectedOption}" >
<f:selectItem itemLabel="Option 1" itemValue="Option 1" />
<f:selectItem itemLabel="Option 2" itemValue="Option 2" />
<f:selectItem itemLabel="Option 3" itemValue="Option 3" />
</p:selectOneButton>
<p:spacer width="0" height="20" />
<p:commandButton
value="Done"
action="#{pageController.showOption}"
update=":pageForm:selectedOption"
oncomplete="dialogOption_Widget.hide();"
/>
</h:panelGrid>
</h:form>
</p:dialog>
</h:body>
</html>
content.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:c="http://java.sun.com/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:mj="http://mojarra.dev.java.net/mojarra_ext"
xmlns:o="http://omnifaces.org/ui"
xmlns:of="http://omnifaces.org/functions">
<h:body>
<ui:composition template="/layout.xhtml">
<ui:define name="content">
<ui:include src="page.xhtml" />
</ui:define>
</ui:composition>
</h:body>
</html>
layout.xhtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.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:o="http://omnifaces.org/ui"
xmlns:of="http://omnifaces.org/functions">
<f:view contentType="text/html" transient="true">
<h:head>
<h:outputStylesheet name="theme.css" library="css" />
<f:facet name="first">
<f:metadata>
<meta http-equiv="X-UA-Compatible" content="IE=8" />
</f:metadata>
<title>Teste</title>
<link rel="icon" type="image/png" href="resources/css/icons/favicon.ico" />
</f:facet>
</h:head>
<h:body>
<p:layout fullPage="true" >
<p:layoutUnit id="top" position="north" size="50" style="border: 2px solid Black !important;">
<!-- <ui:insert name="menubar" /> -->
</p:layoutUnit>
<p:layoutUnit id="center" position="center" style="border: 2px solid Black !important;">
<ui:insert name="content" />
</p:layoutUnit>
<p:layoutUnit id="bottom" position="south" size="60" resizable="true" collapsible="true" style="border: 2px solid Black !important;">
<!-- <ui:insert name="footer" /> -->
</p:layoutUnit>
</p:layout>
</h:body>
</f:view>
</html>
PageController.java
package com.ericsantanna.grendel.mBeans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.context.RequestContext;
/**
* #author Eric Sant'Anna
*
*/
#ManagedBean
#ViewScoped
public class PageController {
private String selectedOption;
private String editedOption;
public PageController() {
System.out.println("RECREATING VIEW");
}
public void showOption() {
System.out.println("Selected option number: " + selectedOption.substring(7));
}
public void doAnythingWithTheOption() {
System.out.print("Trying to a substring in the selected option: ");
editedOption = selectedOption.substring(7);
System.out.println(editedOption);
}
public void handleOptionChoose() {
RequestContext.getCurrentInstance().execute("dialogOption_Widget.show();");
}
public String getSelectedOption() {
return selectedOption;
}
public void setSelectedOption(String selectedOption) {
this.selectedOption = selectedOption;
}
public String getEditedOption() {
return editedOption;
}
public void setEditedOption(String editedOption) {
this.editedOption = editedOption;
}
}
JBoss log (setup):
17:45:29,494 INFO [org.jboss.ejb.client] (MSC service thread 1-7) JBoss EJB Client version 1.0.5.Final
17:45:29,550 INFO [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-8) Inicializando Mojarra 2.2.4 ( 20131003-1354 https://svn.java.net/svn/mojarra~svn/tags/2.2.4#12574) para o contexto '/JobSchedulerWeb'
17:45:30,344 INFO [org.hibernate.validator.util.Version] (MSC service thread 1-8) Hibernate Validator 4.2.0.Final
17:45:30,367 INFO [org.primefaces.webapp.PostConstructApplicationEventListener] (MSC service thread 1-8) Running on PrimeFaces 4.0
17:45:30,368 INFO [org.omnifaces.eventlistener.VersionLoggerEventListener] (MSC service thread 1-8) Using OmniFaces version 1.5
To reproduce this code, simply choose an option and click "Edit option" to do a substring in the backing bean, only to get a NullPointerException when using facelets templating. (Look at the console in each step)
Anyone can help?
This,
<f:view ... transient="true">
turns off JSF state saving for the current view. Essentially, this JSF view is stateless. As there's no view state, there's no means of a view scope and the logical consequence is that the view scoped bean instance can't be saved anywhere. It'll disappear in nowhere by end of the request and thus behave like a request scoped bean.
The observed symptoms are thus fully expected. If that wasn't your intent, then you need to remove the transient="true" attribute. It defaults to false.
This has completely nothing to do with templating. The <ui:composition> plays no significant role here. You'd have had exactly the same problem when merging the templates into a single template without <ui:composition>, <ui:define>, etc while keeping the same markup.
See also:
Why JSF saves the state of UI components on server?
javax.faces.application.ViewExpiredException: View could not be restored
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.