f:attribute Tag in Java Server Faces (JSF)

  • Topic : Java Server Faces (JSF)
  • Environment : J2EE 5.0, MyFaces 1.1.5

Introduction to f:attribute

also read:

This section demonstrates how to use the f:attribute tag in Java Server Faces(JSF). This tag is part fo the core tag library.

How to use?

f:attribute tag will be more useful when we could not use f:param tag for passing the parameters to the backing bean. One more advantage with this tag is, it can be used with all the component.

The following files are used in this example :

  • attributeTag.jsp
  • attributeBean.java
  • faces-config.xml
  • attributeTagResult.jsp

In the following sections we will see in more detail about how to use this tag with different components. In this example we will be looking into three components : h:commandLink, h:commandButton, h:outputText.


f:attribute tag can be more powerful to pass the parameters to the backing bean when we are not able to pass using the f:param. Unlike f:param, this tag pass the parameter by invoking the action listener in the backing bean. In the following example
actionListener=”#{attributeBean.action}” is invoked when user clicks on the command link. The method in the backing bean will get the attribute passed from the jsp page.(String)event.getComponent().getAttributes().get(“attribute1”); is used for retrieving the value. You can pass many parameters by adding more tags inside the h:commandLink


f:attribute tag can be used with h:commandButton in the same way how we can use with h:commandLink.


Apart from just passing the parameters to backing bean, another salient feature of this tag is to bind the componenets atrributes to the backing bean. This example shows how to bind a attribute of h:outputText to the backing bean.


   Source : www.javabeat.net
            <h:form id="commandLink">
                <h:panelGrid columns="1">
                        <h:commandLink value="Test Link" action="#{attributeBean.submit}" actionListener="#{attributeBean.action}">
                            <f:attribute name="attribute1" value="Command Link"/>
                        <h:outputText id="test" binding="#{attributeBean.output}" value="#{attributeBean.value}">
                            <f:attribute name="textOutput" value="OutputText"/>
                        <h:commandButton value="Submit" action="#{attributeBean.submit}" actionListener="#{attributeBean.action}">
                            <f:attribute name="attribute1" value="Command Button"/>


 * Source : www.javabeat.net
 * */
package net.javabeat.myfaces.tags;

import javax.faces.component.html.HtmlOutputText;
import javax.faces.event.ActionEvent;

public class AttributeBean {
    private HtmlOutputText output;
    private String value1;

    public String getValue() {
        return (String)output.getAttributes().get("textOutput");

    public HtmlOutputText getOutput() {
        return output;

    public void setOutput(HtmlOutputText output) {
        this.output = output;

    public String getValue1() {
        return value1;

    public void setValue1(String value1) {
        this.value1 = value1;
    public String submit(){
        return "attribute";
    public void action(ActionEvent event){
        String value1 = (String)event.getComponent().
        this.value1 = value1;




   Source : www.javabeat.net

              <h:form id="attribute">
                  <h:outputText value="#{attributeBean.value1}"/>

Leave a Reply

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

Pin It on Pinterest

Share This

Share this post with your friends!