The objective of this post is to enable the use of the custom pager tag found at http://www.javapassion.com/handsonlabs/jsfdatatable/#Exercise_6 within a web application that uses Facelets (https://facelets.dev.java.net). The original application was configured for deployment on the GlassFish Application Server provided by Sun Microsystems which may be downloaded from http://java.sun.com. However, the changes that will be presented will be applicable to any JavaServerFaces environment with Facelets dependencies.
What is NOT required
The files pager.tld and PagerTag.java given in the original example may be ignored for this implementation.
What IS required
The following are the basic libraries that are required along with a web container such as Apache Tomcat:
- jsf-facelets.jar
- el-impl-1.0.jar
- commons-beanutils.jar
- commons-collections.jar
- commons-digester.jar
- commons-logging.jar
- jsf-api.jar
- jsf-impl.jar
Configurations
One additional XML configuration file is required for use in specifying the Facelets tag namespace. You may give it any name you wish or use facelet.taglib.xml. The relevant content of the file is listed below:
<?xml version="1.0"?>
<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
"http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib>
<namespace>http://corejsf.com/pager</namespace>
<tag>
<tag-name>pager</tag-name>
<component>
<component-type>com.corejsf.OutputPager</component-type>
<renderer-type>com.corejsf.Pager</renderer-type>
</component>
</tag>
</facelet-taglib>
The above Facelets taglib file is defined in the web.xml as a context parameter as follows:
<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>/WEB-INF/path/to/facelet.taglib.xml</param-value>
</context-param>
<component>
<component-type>com.corejsf.OutputPager</component-type>
<component-class>faces.component.UIPager</component-class>
<component-extension>
<component-family>javax.faces.Output</component-family>
<renderer-type>com.corejsf.Pager</renderer-type>
</component-extension>
</component>
package faces.component;
import javax.el.ValueExpression;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
public class UIPager extends UIComponentBase {
private String dataTableId = null;
private Integer showpages = 10;
private String styleClass = null;
private String selectedStyleClass = null;
public UIPager() {
super();
setRendererType("com.corejsf.Pager");
}
@Override
public String getFamily() {
return "com.corejsf.Pager";
}
public String valueExpressionAsString(String attr) {
ValueExpression valueExp = getValueExpression(attr);
if (null != valueExp) {
return (String) valueExp.getValue(this.getFacesContext().getELContext());
}
else {
return null;
}
}
@Override
public Object saveState(FacesContext context) {
Object[] values = new Object[5];
values[0] = super.saveState(context);
values[1] = dataTableId;
values[2] = showpages;
values[3] = styleClass;
values[4] = selectedStyleClass;
return values;
}
@Override
public void restoreState(FacesContext context, Object state) {
Object[] values = (Object[]) state;
super.restoreState(context, values[0]);
this.dataTableId = (String) values[1];
this.showpages = (Integer) values[2];
this.styleClass = (String) values[3];
this.selectedStyleClass = (String) values[4];
}
public String getDataTableId() {
if (null != dataTableId) {
return dataTableId;
}
return valueExpressionAsString("dataTableId");
}
public void setDataTableId(String dataTableId) {
this.dataTableId = dataTableId;
}
public Integer getShowpages() {
String showpagesSetByUser = valueExpressionAsString("showpages");
if(null != showpagesSetByUser) {
return Integer.valueOf(showpagesSetByUser);
}
return showpages;
}
public void setShowpages(Integer showpages) {
this.showpages = showpages;
}
public String getStyleClass() {
if(null != styleClass) {
return styleClass;
}
return valueExpressionAsString("styleClass");
}
public void setStyleClass(String styleClass) {
this.styleClass = styleClass;
}
public String getSelectedStyleClass() {
if(null != selectedStyleClass) {
return selectedStyleClass;
}
return valueExpressionAsString("selectedStyleClass");
}
public void setSelectedStyleClass(String selectedStyleClass) {
this.selectedStyleClass = selectedStyleClass;
}
}
Conclusion
That is it. There are still some obvious improvements that can be made to the implementation presented herein. However, I wanted to keep the changes simple and few.