October 19 2015

Jsf setpropertyactionlistener example

Jsf setpropertyactionlistener example

Jsf ‘<f:setPropertyActionListener>’ tag creates a special action listener and registers with the associated action. It sets value specified by ‘value’ attribute to ‘target’ attribute.It is generally used in datatable to get the record selected by the user.

Attribute          Value expression
value                store the value of target attribute
target               defines the destination of value attribute

In the below example, the ‘stud’ property is set to student property of the studentBean. This will take place before editStudent method is executed.

<h:commandLink value=”Edit” action=”#{studentBean.editStudent}”>
<f:setPropertyActionListener value=”#{stud}” target=”#{studentBean.student}” />
</h:commandLink>

Example

In the below datatable, the f:setPropertyActionListener of the h:commandLink sets the ‘stud’ object on which user clicked to ‘studentBean.student’.

Student.xhtml

<?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:f="http://java.sun.com/jsf/core"
 xmlns:h="http://java.sun.com/jsf/html">
<h:head>
 <title>Jsf setpropertyactionlistener</title>
</h:head>

<h:body>
 <h2>Jsf setpropertyactionlistener example</h2>
 
 <h:form>


 <h:dataTable value="#{studentBean.studList}" var="stud" border="2">


 <h:column>
 <f:facet name="header"> Student ID </f:facet>

 <h:outputText value="#{stud.studId}" />
 </h:column>

 <h:column>
 <f:facet name="header">Name</f:facet>
 <h:outputText value="#{stud.name}" />
 </h:column>

 <h:column>
 <f:facet name="header">Age</f:facet>
 <h:outputText value="#{stud.age}" />
 </h:column>


 <h:column>
 <f:facet name="header">Eit</f:facet>

 <h:commandLink value="Edit" action="#{studentBean.editStudent}">
 <f:setPropertyActionListener value="#{stud}"
 target="#{studentBean.student}" />
 </h:commandLink>
 </h:column>
 </h:dataTable>


 <h:panelGrid id="panel" columns="2" rendered="#{studentBean.editable}">

 <h:outputLabel value="Name" />
 <h:inputText size="20" value="#{studentBean.student.studId}" />

 <h:outputLabel value="Name" />
 <h:inputText size="20" value="#{studentBean.student.name}" />

 <h:outputLabel value="Age" />
 <h:inputText size="20" value="#{studentBean.student.age}" />


 </h:panelGrid>

 </h:form>
</h:body>
</html>

StudentBean.java

package com.answersz;

import java.util.ArrayList;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "studentBean")
@SessionScoped
public class StudentBean {

 Student student = new Student();

 ArrayList<Student> studList = new ArrayList<Student>();

 boolean editable = false;

 @PostConstruct
 public void init() {
 studList.add(new Student("2015-01", "ERIC", 20));
 studList.add(new Student("2015-02", "ANDREW", 18));
 studList.add(new Student("2015-03", "HARRY", 17));
 }

 public String editStudent() {
 editable = true;
 return null;
 }

 public Student getStudent() {
 return student;
 }

 public void setStudent(Student student) {
 this.student = student;
 }

 public ArrayList<Student> getStudList() {
 return studList;
 }

 public void setStudList(ArrayList<Student> studList) {
 this.studList = studList;
 }

 public boolean isEditable() {
 return editable;
 }

 public void setEditable(boolean editable) {
 this.editable = editable;
 }

}

Student.java

package com.answersz;

public class Student {

 private String studId;
 private String name;
 private Integer age;

 public Student(String studId, String name, Integer age) {
 super();
 this.studId = studId;
 this.name = name;
 this.age = age;
 }

 public Student() {

 }

 public String getStudId() {
 return studId;
 }

 public void setStudId(String studId) {
 this.studId = studId;
 }

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public Integer getAge() {
 return age;
 }

 public void setAge(Integer age) {
 this.age = age;
 }


}


Copyright © 2014. All rights reserved.

Posted October 19, 2015 by answersz in category Jsf

Leave a Comment

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

*