July 29 2015

Jsf rendered attribute example

Jsf rendered attribute example

The rendered attribute which uses Boolean EL expression indicates whether a component is currently visible or not. The property is useful when you want to hide components for specific users or based on condition. The default value of rendered attribute is true.

Example:

<h:outputText value=”mapping” rendered=”Boolean EL expression” />

rendered Display the component
true Yes
false No

 Example

In the below example, rendered attribute takes boolean value or boolean expression and displays the component accordingly. Approve-1 and Approve-3 button’s rendered attribute resolves to true whereas Approve-2 button’s rendered attribute resolves to false.

LeaveManagement.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">

<head>
<title>Jsf rendered attribute example</title>
</head>

<h:body>

<h2>Jsf rendered attribute example</h2>

<h:form>
<h:messages />
<h:panelGrid columns="2">

<h:outputText value="Name" />

<h:outputText value="#{leaveManagedBean.fullName}" />
<h:commandButton value="Approve-1" rendered="true"
action="#{leaveManagedBean.approveLeave}" />

<h:commandButton value="Approve-2"
rendered="#{leaveManagedBean.displayApprove}"
action="#{leaveManagedBean.approveLeave}" />

<h:commandButton value="Approve-3"
rendered="#{leaveManagedBean.loggedInUserRole=='MANAGER'}"
action="#{leaveManagedBean.approveLeave}" />

</h:panelGrid>
</h:form>
</h:body>
</html>

LeaveManagedBean.java


package com.answersz;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "leaveManagedBean")
@SessionScoped
public class LeaveManagedBean {

public String fullName = "Michelle";

public String loggedInUserRole = "MANAGER";

public Boolean displayApprove = false;

public String approveLeave() {
// code goes here
return null;
}

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {
this.fullName = fullName;
}

public String getLoggedInUserRole() {
return loggedInUserRole;
}

public void setLoggedInUserRole(String loggedInUserRole) {
this.loggedInUserRole = loggedInUserRole;
}

public Boolean getDisplayApprove() {
return displayApprove;
}

public void setDisplayApprove(Boolean displayApprove) {
this.displayApprove = displayApprove;
}

}

jsfrendered attribute


Copyright © 2014. All rights reserved.

Posted July 29, 2015 by answersz in category Jsf

Leave a Comment

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

*