JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

JSF 2 Dynamic Target View ID (to-view-id) Example

April 5, 2014 by Amr Mohammed Leave a Comment

The to-view-id element can be a value expression, in which case it is evaluated. The result is used as the view ID. This type of providing of to-view-id isn’t implemented before JSF 2.0. This

Also Read:

  • JSF 2 Tutorials
  • JSF Tutorials
  • Introduction to JSF

1. Managed Bean

IndexBean.java

[code lang=”java”]
package net.javabeat.jsf;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class IndexBean {
private String username;
private String password;
private String nextView;

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 getNextView() {
return nextView;
}
public void setNextView(String nextView) {
this.nextView = nextView;
}

public String login(){
if(this.username.equalsIgnoreCase("javabeat") &&
this.password.equals("javabeat")){
return "welcome";
}
else {
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("You’re username or password isn’t valid"));
return "login";
}

}

public String logout(){
this.nextView = "login";
return "logout";
}

public String expensesView(){
this.nextView = "expensesView";
return "expenses";
}
}
[/code]

2. The views

login.xhtml

[code lang=”xml”]
<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:outputScript library="javax.faces" name="jsf.js"/>
</h:head>
<h:body>
<f:view>
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 Dynamic Navigation – Redirect Example</h2>
<h:form prependId="false">
<h:messages globalOnly="true" style="color:red"></h:messages>
<h:panelGrid columns="2">
<h:outputText value="Enter Username: "/>
<h:inputText value="#{indexBean.username}"/>
<h:outputText value="Enter Password: "/>
<h:inputText value="#{indexBean.password}"/>
</h:panelGrid>
<h:commandButton value="Login" action="#{indexBean.login}"></h:commandButton>
</h:form>
</f:view>
</h:body>
</html>
[/code]

welcome.xhtml

[code lang=”xml”]
<!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">
<f:view>
<h:form>
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 Dynamic Navigation – Redirect Example</h2>
<h:outputText value="Welcome Mr.#{indexBean.username}"/>
<br/>
<h:commandLink value="Logout" action="#{indexBean.logout}"/>
#{‘ ‘}
<h:commandLink value="Expenses" action="#{indexBean.expensesView}"/>
</h:form>
</f:view>
</html>
[/code]

expensesView.xhtml

[code lang=”xml”]
<!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">
<f:view>
<h:form>
<h1>JavaBeat JSF 2.2 Examples</h1>
<h2>JSF2 Dynamic Navigation – Redirect Example</h2>
<h:panelGrid columns="2">
<h:outputText value="Oil"/>
<h:outputText value="20 USD"/>
<h:outputText value="House"/>
<h:outputText value="50 USD"/>
<h:outputText value="Schools"/>
<h:outputText value="200 USD"/>
</h:panelGrid>
<br/>
<h:commandButton value="logout" action="login?faces-redirect=true"/>
</h:form>
</f:view>
</html>
[/code]

3. Faces Configuration File

faces-config.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<faces-config 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-facesconfig_2_2.xsd"
version="2.2">
<application>
<resource-bundle>
<base-name>net.javabeat.jsf.application</base-name>
<var>msg</var>
</resource-bundle>
</application>
<navigation-rule>
<from-view-id>/welcome.xhtml</from-view-id>
<navigation-case>
<to-view-id>#{indexBean.nextView}</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
</faces-config>
[/code]

4. The Deployment Descriptor (web.xml)

web.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_2_5.xsd"
id="WebApp_ID" version="2.5" metadata-complete="true">
<context-param>
<description>State saving method: ‘client’ or ‘server’
(=default). See JSF Specification 2.5.2
</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.faces.application.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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
[/code]

5. JSF 2 Dynamic Target View Demo

The below snapshots will show you the impact of using dynamic target view in the navigation.

JSF 2 Dyanmic View Example 1

JSF 2 Dynamic View Example 2

JSF 2 Dynamic View Example 3

  • If you’ve noted that the faces-config.xml doesn’t provide neither navigation rule and nor string outcome for navigating from welcome.xhtml into expensesView or into login view via expenses and logout actions respectively.
  • The navigation has been done through by means of providing a dynamic target view expression #{indexBean.nextView}

[wpdm_file id=43]

Filed Under: JSF Tagged With: JSF 2

About Amr Mohammed

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved