sort on column header

Hi

I am new to JSF

I am reading the Core Java Server Faces book

I have a problem with column sorting program when I click the column header link it's not calling the sortBLast method ,could anybody help me please

here is the bean Name.java

publicclass Name{

private String first;

private String last;

privateboolean editable;

public Name(String first, String last){

this.first = first;

this.last = last;

}

publicvoid setFirst(String newValue){

first = newValue;

}

public String getFirst(){

return first;

}

publicvoid setLast(String newValue){

last = newValue;

}

public String getLast(){

return last;

}

publicboolean isEditable(){

return editable;

}

publicvoid setEditable(boolean newValue){

editable = newValue;

}

}

<--TableData.java -->

package com.corejsf;

import javax.faces.model.DataModel;

import javax.faces.model.ArrayDataModel;

publicclass TableData{

private DataModel filterModel =null;

privatestaticfinal Name[] names ={

new Name("Anna","Keeney"),

new Name("John","Wilson"),

new Name("Mariko","Randor"),

new Name("William","Dupont"),

};

public TableData(){

ArrayDataModel model =new ArrayDataModel(names);

filterModel =new SortFilterModel(model);

}

public DataModel getNames(){

return filterModel;

}

}

<-- here is theSortFilterModel.java -->

package com.corejsf;

import java.util.Arrays;

import java.util.Comparator;

import javax.faces.model.DataModel;

import javax.faces.model.DataModelListener;

publicclass SortFilterModelextends DataModel{

private DataModel model;

private Row[] rows;

privatestatic Comparator byLast =new

Comparator(){

publicint compare(Object o1, Object o2){

Row r1 = (Row) o1;

Row r2 = (Row) o2;

Name n1 = (Name) r1.getData();

Name n2 = (Name) r2.getData();

return n1.getLast().compareTo(n2.getLast());

}

};

privatestatic Comparator byFirst =new

Comparator(){

publicint compare(Object o1, Object o2){

Row r1 = (Row) o1;

Row r2 = (Row) o2;

Name n1 = (Name) r1.getData();

Name n2 = (Name) r2.getData();

return n1.getFirst().compareTo(n2.getFirst());

}

};

privateclass Row{

privateint row;

public Row(int row){

this.row = row;

}

public Object getData(){

int originalIndex = model.getRowIndex();

model.setRowIndex(row);

Object thisRowData = model.getRowData();

model.setRowIndex(originalIndex);

return thisRowData;

}

}

public SortFilterModel(DataModel model){

this.model = model;

int rowCnt = model.getRowCount();

if(rowCnt != -1){

rows =new Row[rowCnt];

for(int i=0; i < rowCnt; ++i){

rows[i] =new Row(i);

}

}

}

public String sortByLast(){

Arrays.sort(rows, byLast);

returnnull;

}

public String sortByFirst(){

Arrays.sort(rows, byFirst);

returnnull;

}

publicvoid setRowIndex(int rowIndex){

if(rowIndex == -1 || rowIndex >= model.getRowCount()){

model.setRowIndex(rowIndex);

}

else{

model.setRowIndex(rows[rowIndex].row);

}

}

// The following methods delegate directly to the

// decorated model

publicboolean isRowAvailable(){

return model.isRowAvailable();

}

publicint getRowCount(){

return model.getRowCount();

}

public Object getRowData(){

return model.getRowData();

}

publicint getRowIndex(){

return model.getRowIndex();

}

public Object getWrappedData(){

return model.getWrappedData();

}

publicvoid setWrappedData(Object data){

model.setWrappedData(data);

}

publicvoid addDataModelListener(DataModelListener listener){

model.addDataModelListener(listener);

}

public DataModelListener[] getDataModelListeners(){

return model.getDataModelListeners();

}

publicvoid removeDataModelListener(DataModelListener listener){

model.removeDataModelListener(listener);

}

}

here is the index.jsp page

<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<%@ page contentType="text/html;charset=windows-1252"%>

<html>

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

<f:view>

<head>

<link href="styles.css" rel="stylesheet" type="text/css"/>

<f:loadBundle basename="com.messages" var="msgs"/>

<title>

<h:outputText value="#{msgs.windowTitle}"/>

</title>

</head>

<body>

<h:form>

<h:dataTable value="#{tableData.names}" var="name"

styleClass="names" headerClass="namesHeader"

columnClasses="last,first">

<h:column>

<f:facet name="header">

<h:commandLink action="#{tableData.names.sortByLast}" onkeypress="submit();" >

<h:outputText value="#{msgs.lastColumnHeader}"/>

</h:commandLink>

</f:facet>

<h:outputText value="#{name.last}"/>

<f:verbatim>,</f:verbatim>

</h:column>

<h:column>

<f:facet name="header">

<h:commandLink action="#{tableData.names.sortByFirst}">

<h:outputText value="#{msgs.firstColumnHeader}"/>

</h:commandLink>

</f:facet>

<h:outputText value="#{name.first}"/>

</h:column>

</h:dataTable>

</h:form>

</body>

</f:view>

</html>

here is the faces-config.xml bean configuration

<managed-bean>

<managed-bean-name>tableData</managed-bean-name>

<managed-bean-class>com.corejsf.TableData</managed-bean-class>

<managed-bean-scope>session</managed-bean-scope>

</managed-bean>

<-- messages.properties file -->

name=Hello World

windowTitle=Sorting Java Beans

pageTitle=An array of names:

firstColumnHeader=First Name

lastColumnHeader=Last Name

lastnameColumn=Last Name

firstnameColumn=First Name

[13309 byte] By [Ruthera] at [2007-11-26 16:15:59]
# 1
Hmm, I don't have a direct solution to this specific problem, but it might be worth reading the http://balusc.xs4all.nl/srv/dev-jep-dat.html article and check out the EAR. It also treats the sorting of datatables.
BalusCa at 2007-7-8 22:39:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
could anybody provide the solution for the above problem plz..
Ruthera at 2007-7-8 22:39:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi,

what is the reason to put onkeypress="submit();"

in your jsp page? JSF uses it's own functions to fire an event so it is not possible to simply invoke the submit function to activate JSF framework correctly.

The other link for example (which fires sortByFirst) is working?

topfoxya at 2007-7-8 22:39:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...