Pessal,
Consegui este exemplo e conseguir gerar um deploy mas ocorre um erro em tempo de execução.
PagerRenderer.java
package com.corejsf;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import javax.faces.component.NamingContainer;
import javax.faces.component.UIComponent;
import javax.faces.component.UIData;
import javax.faces.component.UIForm;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.el.ValueBinding;
import javax.faces.render.Renderer;
public class PagerRenderer extends Renderer {
public void encodeBegin(FacesContext context, UIComponent component)
throws IOException {
String id = component.getClientId(context);
UIComponent parent = component;
while (!(parent instanceof UIForm)) parent = parent.getParent();
String formId = parent.getClientId(context);
ResponseWriter writer = context.getResponseWriter();
String styleClass = (String) get(context, component, "styleClass");
String selectedStyleClass = (String) get(context, component,
"selectedStyleClass");
String dataTableId = (String) get(context, component, "dataTableId");
Integer a = (Integer) get(context, component, "showpages");
int showpages = a == null ? 0 : a.intValue();
// find the component with the given ID
UIData data = (UIData) findComponent(context.getViewRoot(),
getId(dataTableId, id), context);
int first = data.getFirst();
int itemcount = data.getRowCount();
int pagesize = data.getRows();
if (pagesize <= 0) pagesize = itemcount;
int pages = itemcount / pagesize;
if (itemcount % pagesize != 0) pages++;
int currentPage = first / pagesize;
if (first >= itemcount - pagesize) currentPage = pages - 1;
int startPage = 0;
int endPage = pages;
if (showpages > 0) {
startPage = (currentPage / showpages) * showpages;
endPage = Math.min(startPage + showpages, pages);
}
if (currentPage > 0)
writeLink(writer, component, formId, id, "<", styleClass);
if (startPage > 0)
writeLink(writer, component, formId, id, "<<", styleClass);
for (int i = startPage; i < endPage; i++) {
writeLink(writer, component, formId, id, "" + (i + 1),
i == currentPage ? selectedStyleClass : styleClass);
}
if (endPage < pages)
writeLink(writer, component, formId, id, ">>", styleClass);
if (first < itemcount - pagesize)
writeLink(writer, component, formId, id, ">", styleClass);
// hidden field to hold result
writeHiddenField(writer, component, id);
}
private void writeLink(ResponseWriter writer, UIComponent component,
String formId, String id, String value, String styleClass)
throws IOException {
writer.writeText(" ", null);
writer.startElement("a", component);
writer.writeAttribute("href", "#", null);
writer.writeAttribute("onclick", onclickCode(formId, id, value), null);
if (styleClass != null)
writer.writeAttribute("class", styleClass, "styleClass");
writer.writeText(value, null);
writer.endElement("a");
}
private String onclickCode(String formId, String id, String value) {
StringBuffer buffer = new StringBuffer();
buffer.append("document.forms[");
buffer.append("'");
buffer.append(formId);
buffer.append("'");
buffer.append("]['");
buffer.append(id);
buffer.append("'].value='");
buffer.append(value);
buffer.append("';");
buffer.append(" document.forms[");
buffer.append("'");
buffer.append(formId);
buffer.append("'");
buffer.append("].submit()");
buffer.append("; return false;");
return buffer.toString();
}
private void writeHiddenField(ResponseWriter writer, UIComponent component,
String id) throws IOException {
writer.startElement("input", component);
writer.writeAttribute("type", "hidden", null);
writer.writeAttribute("name", id, null);
writer.endElement("input");
}
public void decode(FacesContext context, UIComponent component) {
String id = component.getClientId(context);
Map parameters = context.getExternalContext()
.getRequestParameterMap();
String response = (String) parameters.get(id);
String dataTableId = (String) get(context, component, "dataTableId");
Integer a = (Integer) get(context, component, "showpages");
int showpages = a == null ? 0 : a.intValue();
UIData data = (UIData) findComponent(context.getViewRoot(),
getId(dataTableId, id), context);
int first = data.getFirst();
int itemcount = data.getRowCount();
int pagesize = data.getRows();
if (pagesize <= 0) pagesize = itemcount;
if (response.equals("<")) first -= pagesize;
else if (response.equals(">")) first += pagesize;
else if (response.equals("<<")) first -= pagesize * showpages;
else if (response.equals(">>")) first += pagesize * showpages;
else {
int page = Integer.parseInt(response);
first = (page - 1) * pagesize;
}
if (first + pagesize > itemcount) first = itemcount - pagesize;
if (first < 0) first = 0;
data.setFirst(first);
}
private static Object get(FacesContext context, UIComponent component,
String name) {
ValueBinding binding = component.getValueBinding(name);
if (binding != null) return binding.getValue(context);
else return component.getAttributes().get(name);
}
private static UIComponent findComponent(UIComponent component, String id,
FacesContext context) {
String componentId = component.getClientId(context);
if (componentId.equals(id)) return component;
Iterator kids = component.getChildren().iterator();
while (kids.hasNext()) {
UIComponent kid = (UIComponent) kids.next();
UIComponent found = findComponent(kid, id, context);
if (found != null) return found;
}
return null;
}
private static String getId(String id, String baseId) {
String separator = "" + NamingContainer.SEPARATOR_CHAR;
String[] idSplit = id.split(separator);
String[] baseIdSplit = baseId.split(separator);
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < baseIdSplit.length - idSplit.length; i++) {
buffer.append(baseIdSplit[i]);
buffer.append(separator);
}
buffer.append(id);
return buffer.toString();
}
}
PagerTag.java
package com.corejsf;
import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentTag;
public class PagerTag extends UIComponentTag {
private String showpages;
private String dataTableId;
private String styleClass;
private String selectedStyleClass;
public void setShowpages(String newValue) { showpages = newValue; }
public void setDataTableId(String newValue) { dataTableId = newValue; }
public void setStyleClass(String newValue) { styleClass = newValue; }
public void setSelectedStyleClass(String newValue) {
selectedStyleClass = newValue;
}
public void setProperties(UIComponent component) {
super.setProperties(component);
if (component == null) return;
com.corejsf.util.Tags.setInteger(component, "showpages", showpages);
com.corejsf.util.Tags.setString(component, "dataTableId",
dataTableId);
com.corejsf.util.Tags.setString(component, "styleClass",
styleClass);
com.corejsf.util.Tags.setString(component, "selectedStyleClass",
selectedStyleClass);
}
public void release() {
super.release();
showpages = null;
dataTableId = null;
styleClass = null;
selectedStyleClass = null;
}
public String getRendererType() { return "com.corejsf.Pager"; }
public String getComponentType() { return "javax.faces.Output"; }
}
Tags.java
package com.corejsf.util;
import java.io.Serializable;
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.el.MethodBinding;
import javax.faces.el.ValueBinding;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.webapp.UIComponentTag;
public class Tags {
public static void setString(UIComponent component, String attributeName,
String attributeValue) {
if (attributeValue == null)
return;
if (UIComponentTag.isValueReference(attributeValue))
setValueBinding(component, attributeName, attributeValue);
else
component.getAttributes().put(attributeName, attributeValue);
}
public static void setInteger(UIComponent component,
String attributeName, String attributeValue) {
if (attributeValue == null) return;
if (UIComponentTag.isValueReference(attributeValue))
setValueBinding(component, attributeName, attributeValue);
else
component.getAttributes().put(attributeName,
new Integer(attributeValue));
}
public static void setDouble(UIComponent component,
String attributeName, String attributeValue) {
if (attributeValue == null) return;
if (UIComponentTag.isValueReference(attributeValue))
setValueBinding(component, attributeName, attributeValue);
else
component.getAttributes().put(attributeName,
new Double(attributeValue));
}
public static void setBoolean(UIComponent component,
String attributeName, String attributeValue) {
if (attributeValue == null) return;
if (UIComponentTag.isValueReference(attributeValue))
setValueBinding(component, attributeName, attributeValue);
else
component.getAttributes().put(attributeName,
new Boolean(attributeValue));
}
public static void setValueBinding(UIComponent component, String attributeName,
String attributeValue) {
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
ValueBinding vb = app.createValueBinding(attributeValue);
component.setValueBinding(attributeName, vb);
}
public static void setActionListener(UIComponent component, String attributeValue) {
setMethodBinding(component, "actionListener", attributeValue,
new Class[] { ActionEvent.class });
}
public static void setValueChangeListener(UIComponent component,
String attributeValue) {
setMethodBinding(component, "valueChangeListener", attributeValue,
new Class[] { ValueChangeEvent.class });
}
public static void setValidator(UIComponent component,
String attributeValue) {
setMethodBinding(component, "validator", attributeValue,
new Class[] { FacesContext.class, UIComponent.class, Object.class });
}
public static void setAction(UIComponent component, String attributeValue) {
if (attributeValue == null) return;
if (UIComponentTag.isValueReference(attributeValue))
setMethodBinding(component, "action", attributeValue,
new Class[] {});
else {
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
MethodBinding mb = new ActionMethodBinding(attributeValue);
component.getAttributes().put("action", mb);
}
}
public static void setMethodBinding(UIComponent component, String attributeName,
String attributeValue, Class[] paramTypes) {
if (attributeValue == null)
return;
if (UIComponentTag.isValueReference(attributeValue)) {
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
MethodBinding mb = app.createMethodBinding(attributeValue, paramTypes);
component.getAttributes().put(attributeName, mb);
}
}
public static String eval(String expression) {
if (expression == null) return null;
if (UIComponentTag.isValueReference(expression)) {
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
return "" + app.createValueBinding(expression).getValue(context);
}
else return expression;
}
public static Integer evalInteger(String expression) {
if (expression == null) return null;
if (UIComponentTag.isValueReference(expression)) {
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
Object r = app.createValueBinding(expression).getValue(context);
if (r == null) return null;
else if (r instanceof Integer) return (Integer) r;
else return new Integer(r.toString());
}
else return new Integer(expression);
}
public static Double evalDouble(String expression) {
if (expression == null) return null;
if (UIComponentTag.isValueReference(expression)) {
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
Object r = app.createValueBinding(expression).getValue(context);
if (r == null) return null;
else if (r instanceof Double) return (Double) r;
else return new Double(r.toString());
}
else return new Double(expression);
}
public static Boolean evalBoolean(String expression) {
if (expression == null) return null;
if (UIComponentTag.isValueReference(expression)) {
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
Object r = app.createValueBinding(expression).getValue(context);
if (r == null) return null;
else if (r instanceof Boolean) return (Boolean) r;
else return new Boolean(r.toString());
}
else return new Boolean(expression);
}
private static class ActionMethodBinding
extends MethodBinding implements Serializable {
private String result;
public ActionMethodBinding(String result) { this.result = result; }
public Object invoke(FacesContext context, Object params[]) {
return result;
}
public String getExpressionString() { return result; }
public Class getType(FacesContext context) { return String.class; }
}
}
Erro:
18:11:12,644 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:683)
at javax.faces.webapp.UIComponentTag.encodeBegin(UIComponentTag.java:591)
at javax.faces.webapp.UIComponentTag.doStartTag(UIComponentTag.java:478)
at org.apache.jsp.teste.paginacao_jsp._jspx_meth_core_pager_0(org.apache.jsp.teste.paginacao_jsp:957)
at org.apache.jsp.teste.paginacao_jsp._jspService(org.apache.jsp.teste.paginacao_jsp:201)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
at com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
at com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:130)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:534)
Alguma idéia do que pode esta acontecendo?
É possível paginar um DataTable em JSF 1.1?
t+