Searching...
Friday, 5 April 2013

JSF 2 Ajax example

10:33


Create a jsf xhtml page with ajax tag



<?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:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <h2>JSF 2 Ajax Example</h2>
    <h:form id="vinod">
        <h:outputLabel id="nameLable" value="Name"/>
        <h:inputText id="name" value="#{ajaxBean.name}" />
        <h:commandButton action="#{ajaxBean.getDetails}" id="submit"
            value="Load Details">
            <f:ajax render="vinod"/> </h:commandButton>
        <h:outputLabel id="addressLable" value="address" />
        <h:inputText id="address" value="#{ajaxBean.address}" />
        <h:outputLabel id="phonenumberLable" value="phonenumber " />
        <h:inputText id="phonenumber" value="#{ajaxBean.phonenumber}" />
        <h:outputLabel id="designationLable" value="designation " />
        <h:inputText id="designation" value="#{ajaxBean.designation}" />
    </h:form>
</h:body>
</html>

Create a managed bean


package com.vinod.jsf;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class AjaxBean {
    public AjaxBean() {
    }
    private String name;
    private String designation;
    private String address;
    private String phonenumber;
    private String companyname;

    public String getDetails() {
        setName("Vinod");
        setDesignation("Software Engineer");
        setAddress("Bangalore");
        setPhonenumber("9090909090");
        setCompanyname("Sasi technologies");
        return "Success";
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDesignation() {
        return designation;
    }
    public void setDesignation(String designation) {
        this.designation = designation;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getPhonenumber() {
        return phonenumber;
    }
    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }
    public String getCompanyname() {
        return companyname;
    }
    public void setCompanyname(String companyname) {
        this.companyname = companyname;
    }
}


Deploy application and Runt it




Done!! download example and use mvn jetty:run to execute the program

https://github.com/kkvinodkumaran/myrepository/tree/master/vinod-jsf

0 comments: