Satellite Internet QuickBooks Advice international calling cards calling cards
JavaBeat Certifications Certifications Kits Articles Tips QNA Interview Questions SCJP 1.5 SCBCD 5.0 Java/J2EE Feeds
Feedback Request New Tips Print Email

f:attribute Tag in Java Server Faces (JSF)

Author : JavaBeat
Date : Sat Apr 12th, 2008
Topic : jsf
Add to: Digg Add to: Del.icio.us Add to: Reddit Add to: StumbleUpon Add to: Slashdot Add to: Yahoo Add to: Google Add to: Blinklist Add to: Technorati Information

  • Topic : Java Server Faces (JSF)

  • Environment : J2EE 5.0, MyFaces 1.1.5

  • Discuss Here

Introduction to f:attribute

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

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 differenet components. In this example we will be looking into three componenets : 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 paramter 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 reteriving 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
-->
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>

<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
-->
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>

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

Feedback Request New Tips Print Email

Favorites
C# problem error
Free Newsgroups
Latest QnA
SCJD Tips
When we start a thread by applying start() method on it ,how does it knows that to execute run()method?
About Wrapper class in Java
How to configure weblogic 7.0 in MyEclipse?
Static Block and Static Initializer in Java

JavaBeat Website (2004-2009), India
javabeat | about us | useful resources
Copyright (2004 - 2009), JavaBeat