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.

h:commandLink

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

h:commandButton

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

h:outputText

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.

attributeTag.jsp

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

AttributeBean.java


/**
 * 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().
                getAttributes().get("attribute1");
        this.value1 = value1;
    }
}

faces-config.xml


   <managed-bean>
        <managed-bean-name>
            attributeBean
        </managed-bean-name>
        <managed-bean-class>
            net.javabeat.myfaces.tags.AttributeBean
        </managed-bean-class>
        <managed-bean-scope>
            request
        </managed-bean-scope>
    </managed-bean>
    <navigation-rule>
        <navigation-case>
            <from-outcome>
                attribute
            </from-outcome>
            <to-view-id>
                /pages/tags/attributeTagResult.jsp
            </to-view-id>
        </navigation-case>
    </navigation-rule>

attributeTagResult.jsp


<!--
   Source : www.javabeat.net
-->

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

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*