JSF by default performs a server page forward while navigating from one page to another and the URL of the application does not change.
To enable the page redirection, append faces-redirect=true at the end of the view name.
The following code shows how to add redirect to JSF page.
Page1 is Forward while Page2 is Redirect.
<h:form> <h3>Forward</h3> <h:commandButton action="page1" value="Page1" /> <h3>Redirect</h3> <h:commandButton action="page2?faces-redirect=true" value="Page2" /> </h:form>
The following code is from demo.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 start.xhtml</h2> <h:form> <!--<h:commandButton action="page1?faces-redirect=true" value="Page1" />--> <h:commandButton action="page1" value="Page1" /> </h:form> </h:body> </html>
The following code is from 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.xhtml</h2> </h:body> </html>
The following code is from UserBean.java.
package com.java2s.common; import java.io.Serializable; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean @SessionScoped public class UserBean implements Serializable { private static final long serialVersionUID = 1L; }
Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.
After Tomcat finish starting, type the following URL in the browser address bar.
http://localhost:8080/simple-webapp/demo.xhtml