I want to get some information about contacts and display them in a table. I used two managed beans named by PersonalContact and BusinessContact and one abstract class named by Contact including firstName, lastName, ID, email and mphone attributes which derives from.Also, one generic class to add beans to an arraylist. I put here just BusinessContact as a bean, business as XHTML and tableBusiness as a table and generic class AddressBook, files of personal is parallel of them.
Inputs getting from screen are assigned to attributes of beans but when the beans go to Arraylist to be added, its content becomes null. Why is that? Is that about scopes?
Here is my bean:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class BusinessContact extends Contact{
private String companyName,workPhone;
public BusinessContact() {}
public BusinessContact(String companyName, String workPhone, String ID, String firstName, String lastName, String email, String mphone) {
super(ID, firstName, lastName, email, mphone);
this.companyName = companyName;
this.workPhone = workPhone;
}
/**
* Creates a new instance of BusinessContact
*/
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getWorkPhone() {
return workPhone;
}
public void setWorkPhone(String workPhone) {
this.workPhone = workPhone;
}
}
Here is my business.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:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Contact</title>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="2">
First Name:
<h:inputText id="firstName" required="true"
requiredMessage="Can not be blank" value="#{businessContact.firstName}">
</h:inputText>
ID:
<h:inputText id="id" required="true"
requiredMessage="Can not be blank" value="#{businessContact.ID}">
</h:inputText>
Last Name:
<h:inputText id="lastName" required="true"
requiredMessage="Can not be blank" value="#{businessContact.lastName}">
</h:inputText>
E-mail:
<h:inputText id="email" required="true"
requiredMessage="Can not be blank" value="#{businessContact.email}">
</h:inputText>
Mobile Phone:
<h:inputText id="mphone" required="true"
requiredMessage="Can not be blank" value="#{businessContact.mphone}">
</h:inputText>
Company Name:
<h:inputText id="companyName" required="true"
requiredMessage="Can not be blank" value="#{businessContact.companyName}">
</h:inputText>
Work Phone:
<h:inputText id="workPhone" required="true"
requiredMessage="Can not be blank" value="#{businessContact.workPhone}">
</h:inputText>
</h:panelGrid>
<h:commandButton value="Add" id="add" action="#{addressBook.add(BusinessContact)}"></h:commandButton>
<h:commandButton value="Delete" id="delete" action="#{addressBook.remove(BusinessContact)}"></h:commandButton>
<h:outputLink id="t" value="tableBusiness.xhtml"> Click see the table</h:outputLink>
<ui:debug hotkey="k" rendered="true"/>
</h:form>
</h:body>
This is my generic class for putting beans to an arraylist:
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class AddressBook <T extends Contact> {
private ArrayList<T> list = new ArrayList<T>();
T clas;
public ArrayList<T> getList() {
return list;
}
public void setList(ArrayList<T> list) {
this.list = list;
}
public void add(T obj) {
this.clas=obj;
//System.out.println(obj.getFirstName()+" ");
list.add(obj);
}
public void remove(T obj)
{
list.remove(obj);
}
/**
* Creates a new instance of AddressBook
*/
public AddressBook() {}
}
And lastly, this xhmtl for displaying arraylist in table:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
</h:head>
<h:body>
<h:form>
"#{businessContact.firstName}"
<h:dataTable value="#{addressBook.list}" var="BusinessContact">
<h:column>
<f:facet name="header">
First Name
</f:facet>
<h:outputText value="#{businessContact.firstName}">
</h:outputText>
</h:column>
<h:column>
<f:facet name="header">
Last Name
</f:facet>
#{businessContact.lastName}
</h:column>
<h:column>
<f:facet name="header">
Mobile Phone
</f:facet>
#{businessContact.mphone}
</h:column>
<h:column>
<f:facet name="header">
E-mail
</f:facet>
#{businessContact.email}
</h:column>
<h:column>
<f:facet name="header">
Company Name
</f:facet>
#{businessContact.companyName}
</h:column>
<h:column>
<f:facet name="header">
Work Phone
</f:facet>
#{businessContact.workPhone}
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
You are naming the var="BusinessContact" but trying to use it as "businessContact"
Change this line
<h:dataTable value="#{addressBook.list}" var="BusinessContact">
to this
<h:dataTable value="#{addressBook.list}" var="businessContact">
Related
I have problem with InputText. When I try to get the InputText value after clicking the button, the value is null.
When I create the login page, there isn't a problem but using a DataTable does.
package controller;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import model.Groups;
import model.Student;
import util.DataHolder;
#ManagedBean(name = "edit", eager = true)
#SessionScoped
#RequestScoped
public class EditController implements Serializable {
private static final long serialVersionUID = 1L;
private List<Student> studentList;
private Groups group;
#PostConstruct
public void init() {
this.group = DataHolder.getGroup();
}
public List<Student> getStudentList() {
studentList = group.getStudents();
return studentList;
}
public void saveGrade(Student student) {
System.out.println(student.getName() + " " + student.getSurname() + "grade =" + student.getGrade());
}
public Groups getGroup() {
return group;
}
public void setGroup(Groups group) {
this.group = group;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
}
<?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>Edit Page</title>
<link rel="stylesheet"
href="https://cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css"></link>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script
src="https://cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></link>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css"></link>
<script>
$(document).ready(function() {
$('#example').DataTable();
});
</script>
<h:body>
<div class="container">
<h:dataTable value="#{edit.studentList}" var="student"
styleClass="table table-striped table-bordered" id="example">
<h:column headerText="Student">
<f:facet name="header">
<h:outputLabel>Student</h:outputLabel>
</f:facet>
<h:outputText value="#{edit.studentList.indexOf(student) + 1}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel>Number</h:outputLabel>
</f:facet>
<h:outputText value="#{student.no}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel>Name</h:outputLabel>
</f:facet>
<h:outputText value="#{student.name}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel>Surname</h:outputLabel>
</f:facet>
<h:outputText value="#{student.surname}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel>Grade</h:outputLabel>
</f:facet>
<h:column>
<h:form>
<h:inputText value="#{student.grade}" class="form-control"></h:inputText>
</h:form>
</h:column>
</h:column>
<h:column>
<h:form>
<h:commandButton class="btn btn-danger btn-sm" value="Save"
action="#{edit.saveGrade(student)}" />
</h:form>
</h:column>
<h:column>
<h:form>
<h:commandButton class="btn btn-danger btn-sm" value="Delete"
action="#{edit.deleteStudent(student)}" />
</h:form>
</h:column>
</h:dataTable>
</div>
</h:body>
</html>
This question already has answers here:
Creating master-detail pages for entities, how to link them and which bean scope to choose
(2 answers)
Closed 7 years ago.
I have a very strange situation here. I have made my application in JSF and it looks just great, however I am not quite sure about following: I want to have a hyperlink so that once I click on my book title I get to a page with all the details regarding that book. My code looks so far:
My class looks like this:
package com.century.rental;
import javax.ejb.Stateless;
import javax.persistence.*;
import java.util.List;
#Stateless
public class GameEJB {
#PersistenceContext(unitName = "PerUni")
private EntityManager em;
public List<Game> findGames() {
TypedQuery<Game> query = em.createNamedQuery("Game.findAll", Game.class);
return query.getResultList();
}
public List<Game> findGamesByTitle(String title) {
TypedQuery<Game> query = em.createNamedQuery("Game.findByTitle", Game.class);
query.setParameter("title", title);
return query.getResultList();
}
public Game find(Long id) {
return em.find(Game.class, id);
}
public Game createGame(Game game) {
em.persist(game);
System.out.print("game stored");
return game;
}
}
My controller class looks like this:
package com.century.rental;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class GameController {
#EJB
private GameEJB gameEJB;
private Game game = new Game();
private String title = new String();
private List<Game> gameList = new ArrayList<Game>();
private List<Game> sgameList = new ArrayList<Game>();
public GameController() {
}
public String doCreateGame() {
gameEJB.createGame(game);
gameList = gameEJB.findGames();
game = new Game();
return "listGames.xhtml";
}
public Game getGame() {
return this.game;
}
public void setGame(Game game) {
this.game = game;
}
public List<Game> getGameList() {
gameList = gameEJB.findGames();
return gameList;
}
public void setGameList(List<Game> gameList) {
this.gameList = gameList;
}
public String searchGames() {
sgameList = gameEJB.findGamesByTitle(title);
return "resultsGames.xhtml";
}
public List<Game> getSgameList() {
return sgameList;
}
public void setSbookList(List<Game> sgameList) {
this.sgameList = sgameList;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Entity:
package com.century.rental;
import java.util.Date;
import javax.persistence.*;
#Entity
#NamedQueries({
#NamedQuery(name = "Game.findAll", query = "SELECT g FROM Game g"),
#NamedQuery(name = "Game.findByTitle", query = "SELECT g FROM Game g WHERE g.title = :title")
})
public class Game extends Product {
#Basic(optional = false)
#Column(name = "DEVELOPER_STUDIO", nullable = false, length = 100)
private String developerStudio;
#Basic(optional = false)
#Column(name = "PLATFORM", nullable = false, length = 100)
private String platform;
public Game() {
}
public Game(String title, String description, String rating, Date releaseDate, String developerStudio, String platform) {
super(title, description, rating, releaseDate);
this.developerStudio = developerStudio;
this.platform = platform;
}
public String getDeveloperStudio() {
return developerStudio;
}
public void setDeveloperStudio(String developerStudio) {
this.developerStudio = developerStudio;
}
public String getPlatform() {
return platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
}
My HTML code looks like this:
<?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>List of All Available Games in Database</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</h:head>
<h:body>
<f:view>
<h:form>
<h1><h:outputText value="List of All Available Games in Database"/></h1>
<h:dataTable value="#{gameController.gameList}" var="item" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Id"/>
</f:facet>
<h:outputText value="#{item.id}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Title"/>
</f:facet>
<h:outputText value="#{item.title}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Description"/>
</f:facet>
<h:outputText value="#{item.description}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Rating"/>
</f:facet>
<h:outputText value="#{item.rating}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Release Date"/>
</f:facet>
<h:outputText value="#{item.releaseDate}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Developer Studio"/>
</f:facet>
<h:outputText value="#{item.developerStudio}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Platform"/>
</f:facet>
<h:outputText value="#{item.platform}"/>
</h:column>
</h:dataTable>
</h:form>
</f:view>
<br /><br />
Add New -OR- Go to Main Page
</h:body>
</html>
Please, help me I am not quite sure how can I make a hyperlink to a specific book from a list of books( as shown in the code )
Thanks.
P.S.
My specific page specificGame, which is supposed to get values from a game that was clicked in listGames page.
<?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>List specific Game in Database</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</h:head>
<h:body>
<f:view>
<h:form>
<h1><h:outputText value="List a specific Game in Database"/></h1>
<h:dataTable value="#{GameController.sGameList}" var="item" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Name"/>
</f:facet>
<h:outputText value="#{item.name}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Title"/>
</f:facet>
<h:outputText value="#{item.title}"/>
</h:column
</h:dataTable>
</h:form>
</f:view>
<br /><br />
Add New -OR- Go to Main Page
</h:body>
</html>
Now when I click on a title from the list of games on page list Games I would be redirected to specific Game page which is supposed to look like this: http://i.stack.imgur.com/OHmdp.png
populated by all the values from the particular game.
Try to change the column title in listGames.xhtml to :
<h:column>
<f:facet name="header">
<h:outputText value="Title"/>
</f:facet>
<h:commandLink value="#{item.title}" action="#{gameController.searchGames(item.title)}" />
</h:column>
Then, adapt the action method in the backing bean to :
public String searchGames(String tit) {
sgameList = gameEJB.findGamesByTitle(tit);
return "resultsGames.xhtml";
}
When I run the JSF with the items listed, I get the first part to come up where I can enter in details successfully, but they won't show up on the screen like I want them to! Can you guys please help:
Here's my index.xhtml code:
<?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://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>JSF Tester</title>
</h:head>
<h:body>
<h:form>
First name: <h:inputText value="#{PersonController.current.firstName}"/>
Last name: <h:inputText value="#{PersonController.current.lastName}"/>
Birth date: (mm-dd-yyyy) <h:inputText value="#{PersonController.current.date}">
<f:convertDateTime dateStyle="full" type="date" timeZone="CST" pattern="MM-dd-yyyy"/>
</h:inputText>
<h:commandButton value="Submit Person" action="#{PersonController.savePerson()}" />
<h:dataTable var="persons" value="#{PersonController.current}">
<h:column>
<f:facet name="header">First Name</f:facet>
#{PersonController.current.firstName}
</h:column>
<h:column>
<f:facet name="header">Last Name</f:facet>
#{PersonController.current.lastName}
</h:column>
<h:column>
<f:facet name="header">Date</f:facet>
#{PersonController.current.date}
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
And here is my class containing the ArrayList:
/**
$ Id: PersonController.java v1.0, 7/16/2014
$ Name: $
*/
package org.usd.csci.person.jsf;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
/**
*
* #author
*/
#ManagedBean(name="PersonController")
#SessionScoped
public class PersonController implements Serializable {
Person current = new Person();
List<Person> persons = new ArrayList<Person>();
/**
* Creates a new instance of PersonController
*/
public PersonController() {
}
public Person getCurrent() {
return current;
}
public List<Person> getPersons() {
return persons;
}
public void savePerson() {
persons.add(current);
current = new Person();
}
}
You are setting the datatable in a wrong way. It should be like the below.
value parameter should address the ArrayList. In your case PersonController.persons.
var parameter is used to address the Person objects in persons list. You can name it whatever you want.
To access a property of Person object You will use this var parameter. For example if you set var as
var = "variableName", You can access the properties of a Person object using variableName.firstName variableName.lastName etc.
<h:dataTable var="person" value="#{PersonController.persons}">
<h:column>
<f:facet name="header">First Name</f:facet>
#{person.firstName}
</h:column>
<h:column>
<f:facet name="header">Last Name</f:facet>
#{person.lastName}
</h:column>
<h:column>
<f:facet name="header">Date</f:facet>
#{person.date}
</h:column>
</h:dataTable>
i am using rich notify messages and added all related jars namely
richfaces-components-ui-4.1.0.Final ,
richfaces-core-impl-4.0.0.Final,
standard-1.1.2 ,
jstl-1.2 ,
validation-api-1.0.0.GA ,
richfaces-core-api-4.0.0.Final ,
richfaces-components-api-4.0.0.Final
jsf-impl-2.0.2,
cssparser-0.9.5,
guava-r08,
jhighlight-1.0.
but i am getting class not found exception:org.richfaces.component.NotifyAttributes.
sample.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<rich:panel>
<f:facet name="header">
<h:outputText value="Validation Form" />
</f:facet>
<h:form>
<h:panelGrid columns="3">
<rich:validator event="change">
<h:outputText value="Name:" />
<h:inputText label="Name" id="name" value="#{userBean.name}"
required="true">
<f:validateLength minimum="3" />
</h:inputText>
<rich:message for="name" ajaxRendered="true" />
<h:outputText value="Job:" />
<h:inputText label="Job" id="job" value="#{userBean.job}"
requiredMessage="Job is required">
<f:validateLength minimum="3" maximum="50" />
</h:inputText>
<rich:message for="job" ajaxRendered="true" />
<h:outputText value="Address:" />
<h:inputText label="Address" id="address"
value="#{userBean.address}" requiredMessage="Address is required">
<f:validateLength minimum="10" />
</h:inputText>
<rich:message for="address" ajaxRendered="true" />
<h:outputText value="Zip:" />
<h:inputText label="Zip" id="zip" value="#{userBean.zip}"
requiredMessage="Zip is required">
<f:validateLength minimum="4" maximum="9" />
</h:inputText>
<rich:message for="zip" ajaxRendered="true" />
</rich:validator>
<rich:notifyMessages stayTime="2000" nonblocking="true" escape="false"/>
<f:facet name="footer">
<a4j:commandButton value="Ajax Validate" />
</f:facet>
</h:panelGrid>
</h:form>
</rich:panel>
</ui:composition>
bean class used to set and get bean vlaues.let me know whats wrong with existing code ,along with jars to be added
userBean.java:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class userBean {
private String name;
private String job;
private String address;
private String zip;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}
error:
Caused by: java.lang.ClassNotFoundException: org.richfaces.component.NotifyAttributes
i am not getting why this is getting although i added all required jars of richfaces4.0 and above.
notify component is not part of RichFaces 4.0. See Richfaces 4.0 VDL documentation. You have to use version 4.1 or up (for example current version RichFaces 4.3).Note: all richfaces-???.jar(s) should be for the same version of RichFaces.
I'm new in Richfaces framework which I'm going to use in the nearest project in my job.
I've created some simple example in which I have rich:dataTable and I'm trying to make column sorted. I've read in the tutorial ( http://docs.jboss.org/richfaces/latest_3_3_X/en/devguide/html/rich_column.html ) that everything what I need is to type "sortBy" parameter to the "rich:column" tag . Unfortunately columns can't be sort. Can anybody help me? Below source code of my simple app :
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<f:view>
<h:head></h:head>
<h:body>
<rich:panel header="Richfaces dataTable">
<rich:dataTable value="#{TaskListBean.dataList}" var="dataItem"
rows="4" columnClasses="50,100,100,100"
onRowMouseOver="this.style.backgroundColor='#B5F3FB'"
onRowMouseOut="this.style.backgroundColor='#{a4jSkin.rowBackgroundColor}'"
width="350">
<f:facet name="caption">
<h:outputText value="This is the caption" />
</f:facet>
<f:facet name="header">
<h:outputText value="Trouble Ticket Systems" />
</f:facet>
<rich:column colspan="4">
<h:outputText value="Trouble Tickets opened" />
</rich:column>
<rich:column sortBy="#{dataItem.taskInstanceId}">
<f:facet name="header">Ticket Id</f:facet>
<h:outputText value="#{dataItem.taskInstanceId}" />
</rich:column>
<rich:column sortBy="#{dataItem.taskNode}">
<f:facet name="header">Status</f:facet>
<h:outputText value="#{dataItem.taskNode}" />
</rich:column>
<rich:column sortBy="#{dataItem.actorId}">
<f:facet name="header">Actor</f:facet>
<h:outputText value="#{dataItem.actorId}" />
</rich:column>
<rich:column>
<f:facet name="header">Description</f:facet>
<h:outputText value="#{dataItem.description}" />
</rich:column>
<f:facet name="footer">
<h:outputText value="This is the footer" />
</f:facet>
</rich:dataTable>
</rich:panel>
</h:body>
</f:view>
Menaged Bean of my app:
package richfacesr;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
#ManagedBean(name = "manager")
public class TaskListBean implements Serializable {
private ArrayList dataList;
public void loadDataList() {
dataList = new ArrayList();
TaskListData data1 = new TaskListData();
data1.setTaskInstanceId(1000l);
data1.setActorId("Willy smith");
data1.setTaskNode("In Charge");
data1.setDescription("CR 123456");
TaskListData data2 = new TaskListData();
data2.setTaskInstanceId(1001l);
data2.setActorId("Frank Sinatra");
data2.setTaskNode("Rejected");
data2.setDescription("CR 654321");
dataList.add(data1);
dataList.add(data2);
}
public List<TaskListData> getDataList() {
loadDataList();
return dataList;
}
}
and the object:
package richfacesr;
import java.util.ArrayList;
import java.util.List;
public class TaskListData {
private String taskNode;
private long taskInstanceId;
private String actorId;
private String description;
public String getActorId() {
return actorId;
}
public void setActorId(String actorId) {
this.actorId = actorId;
}
public String getTaskNode() {
return taskNode;
}
public void setTaskNode(String currentNode) {
this.taskNode = currentNode;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public long getTaskInstanceId() {
return taskInstanceId;
}
public void setTaskInstanceId(long taskInstanceId) {
this.taskInstanceId = taskInstanceId;
}
}
Taken from the Richfaces 4 documentation, built in sorting for dataTables does not work.
The built-in sort controls are only available with the extendedDataTable component. Support for built-in sort controls in the dataTable component will be added in a subsequent release.
It did work in Richfaces 3, but it seems that they have changed a bit, so that this feature is currently not working in Richfaces 4, out of the box.
Although you say that you use Richfaces 5, which is Alpha1, I doubt that this feature is already in there. So you will need to write some code for yourself.
Looking a the Richfaces 4 showcase about table sorting you need to
put your managed bean into ViewScope - otherwise the unsorted list will be created again and again
move your loadDataList out of getDataList() into the constuctor or annotate it with #PostConstruct - otherwise the unsorted list will be created again and again
make your TaskListData Serializable
add a sort method to your TaskListBean - do the sorting there - maybe as described in Sort ArrayList of custom Objects by property
check the name of your ManagedBean TaskListBean - in the code you call it manager, but in your xhtml you call it TaskListBean
Here is my tuned version of your xhtml
<f:view>
<h:head />
<h:body>
<h:form>
<rich:dataTable id="table" var="dataItem"
value="#{manager.dataList}" width="350">
<rich:column id="ticketId" sortBy="#{dataItem.taskInstanceId}">
<f:facet name="header">
<a4j:commandLink value="Ticket Id" render="table"
action="#{manager.sortBy('Ticket Id')}" />
</f:facet>
<h:outputText value="#{dataItem.taskInstanceId}" />
</rich:column>
<rich:column sortBy="#{dataItem.taskNode}">
<f:facet name="header">
<a4j:commandLink value="Status" render="table"
action="#{manager.sortBy('Status')}" />
</f:facet>
<h:outputText value="#{dataItem.taskNode}" />
</rich:column>
<rich:column sortBy="#{dataItem.actorId}">
<f:facet name="header">
<a4j:commandLink value="Actor" render="table"
action="#{manager.sortBy('Actor')}" />
</f:facet>
<h:outputText value="#{dataItem.actorId}" />
</rich:column>
<rich:column>
<f:facet name="header">
<a4j:commandLink value="Description" render="table"
action="#{manager.sortBy('Description')}" />
</f:facet>
<h:outputText value="#{dataItem.description}" />
</rich:column>
</rich:dataTable>
</h:form>
</h:body>
</f:view>
My tuned version of your TaskListBean
import java.io.Serializable;
import java.util.*;
import javax.annotation.PostConstruct;
import javax.faces.bean.*;
#ViewScoped
#ManagedBean(name="manager")
public class TaskListBean implements Serializable {
private ArrayList<TaskListData> dataList;
#PostConstruct
public void loadDataList() {
dataList = new ArrayList<TaskListData>();
TaskListData data1 = new TaskListData();
data1.setTaskInstanceId(1000l);
data1.setActorId("Willy smith");
data1.setTaskNode("In Charge");
data1.setDescription("CR 123456");
TaskListData data2 = new TaskListData();
data2.setTaskInstanceId(1001l);
data2.setActorId("Frank Sinatra");
data2.setTaskNode("Rejected");
data2.setDescription("CR 654321");
dataList.add(data1);
dataList.add(data2);
}
public void sortBy(String aColumn) {
System.out.println("sort by: " + aColumn);
// TODO sort the list by that attribute
}
public List<TaskListData> getDataList() {
return dataList;
}
}