a4j:support – How to use action attribute?

SHARE & COMMENT :

a4j:support and action attribute

This example program demonstrates how to use action attribute to update the server values and display in the screen. In this example user inputs are passed to update method and processed. The values are updated in the h:panelGrid component.

also read:

The advantage of using a4j:support tag is it can be updated the whole region by specifying the reRender attribute.

richfaces.jsp


<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@ taglib prefix="rich" uri="http://richfaces.org/rich" %>
<%@ taglib prefix="a4j" uri="http://richfaces.org/a4j"%>
<html>
    <body>
        <f:view>
            <h:form id="jbForm1">
                <h:outputText value="Employee id : "/>
                <h:inputText value="#{richBean.empId}">
                    <a4j:support event="onkeyup" reRender="output" action="#{richBean.update}" />
                </h:inputText>
            </h:form>
            <h:panelGrid id="output">
                <h:column>
                    <h:outputText value="Updated Values : "/>
                    <h:outputText value="#{richBean.name}"/>
                    <h:outputText value="#{richBean.status}"/>
                </h:column>
            </h:panelGrid>

        </f:view>
    </body>
</html>

JavaBeatRichfacesBean.java


package javabeat.net.richfaces;

import java.util.HashMap;
import java.util.Map;

public class JavaBeatRichfacesBean {
    private String name;
    private Integer empId;
    private String status;

    public Integer getEmpId() {
        return empId;
    }

    public void setEmpId(Integer empId) {
        this.empId = empId;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void update(){
        Map<integer,String> map = new HashMap<integer,String>();
        map.put(1, "Employee One");
        map.put(2, "Employee Two");
        map.put(3, "Employee Three");
        map.put(4, "Employee Four");
        this.name = map.get(this.empId);
        if (this.name == null || this.name.trim().length() == 0)
        {
            this.status = "No Matching Found";
        }
    }

}

faces-config.xml


<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="1.2"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
    <managed-bean>
        <managed-bean-name>richBean</managed-bean-name>
        <managed-bean-class>javabeat.net.richfaces.JavaBeatRichfacesBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
</faces-config>

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

*

Close
Please support the site
By clicking any of these buttons you help our site to get better