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

index.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">
	<h:head>
		<script name="jquery/jquery.js" library="primefaces"></script>
	</h:head>
	<f:view>
		<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="#{registrationBean.user.name}"/>
				<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}"/>
			</h:panelGrid>
			<p:commandButton value="Add" ajax="false">
				<p:collector value="#{registrationBean.user}" unique="false" addTo="#{registrationBean.users}"></p:collector>
			</p:commandButton>
			<br/>
			<br/>
			<br/>
			<p:dataTable id="dataTable" value="#{registrationBean.users}" var="user" border="1">
				<p:column>
					<f:facet name="header">
						<h:outputText value="Name"/>
					</f:facet>
					<h:outputText value="#{user.name}"/>
				</p:column>
				<p:column>
					<f:facet name="header">
						<h:outputText value="Status"/>
					</f:facet>
					<h:outputText value="#{user.status}"/>
				</p:column>
			</p:dataTable>
		</h:form>
	</f:view>
</html>

2. User Data Type

User.java

package net.javabeat.primefaces.data;

public class User {
	private String name;
	private String age;
	private String status;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.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

RegistrationBean.java

package net.javabeat.primefaces;

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

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

import net.javabeat.primefaces.data.User;
@ManagedBean
@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

Comments

comments

About Amr Mohammed

Speak Your Mind

*