PrimeFaces Collector Example

Collector is a simple utility to manage collections declaratively, collector requires a collection and a value to work with. It’s important to override equals and hashCode methods of the value object to make collector work.

Even you would be adding a certain object into list of the same type of that object, however, you have the ability of adding a certain object into list from the same kind by using the Collector component, so no need for defining a method for doing the add operation.

1. The View


<html xmlns=""
		<script name="jquery/jquery.js" library="primefaces"></script>
		<h:form prependId="false">
			<h1>JavaBeat Primefaces Example</h1>
			<h2>Primefaces - Collector </h2>
			<h:panelGrid columns="2">
				<h:outputText value="Enter Name:"/>
				<p:inputText value="#{}"/>
				<h:outputText value="Enter Age:"/>
				<p:inputText value="#{registrationBean.user.age}"/>
				<h:outputText value="Enter Status [Example A, B, C or D]:"/>
				<p:inputText value="#{registrationBean.user.status}"/>
			<p:commandButton value="Add" ajax="false">
				<p:collector value="#{registrationBean.user}" unique="false" addTo="#{registrationBean.users}"></p:collector>
			<p:dataTable id="dataTable" value="#{registrationBean.users}" var="user" border="1">
					<f:facet name="header">
						<h:outputText value="Name"/>
					<h:outputText value="#{}"/>
					<f:facet name="header">
						<h:outputText value="Status"/>
					<h:outputText value="#{user.status}"/>

2. User Data Type


public class User {
	private String name;
	private String age;
	private String status;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public String getAge() {
		return age;
	public void setAge(String age) {
		this.age = age;
	public String getStatus() {
		return status;
	public void setStatus(String status) {
		this.status = status;

3. The Managed Bean

package net.javabeat.primefaces;

import java.util.ArrayList;
import java.util.List;

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

public class RegistrationBean {
	private List<User> users = new ArrayList<User>();
	private User user = new User();
	public List<User> getUsers() {
		return users;
	public void setUsers(List<User> users) {
		this.users = users;
	public User getUser() {
		return user;
	public void setUser(User user) {
		this.user = user;

4. Primefaces Collector Example Demo

The below snapshot show you the using of Collector for adding a certain object into defined list.

Primefaces - Collector Example

Leave a Reply

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

Pin It on Pinterest

Share This

Share this post with your friends!