Paginatore completo in JSF

Forum relativo ai framework per l'utilizzo della tecnologia JSP

Paginatore completo in JSF

Messaggioda alesslud » gio nov 10, 2011 4:38 pm

Vi mostriamo un esempio in cui viene visualizzato un paginatore completo in JSF che visualizza i dati in tabella.
L'esempio crea un custom tag da poter utilizzare sulla vostra applicazione JSF.
Allegati
paginatore.zip
Il seguente progetto è stato testato sotto Eclipse Helios e JBoss 5.1 e con una JDK 1.6
(23.83 KiB) Scaricato 285 volte
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

index.jsp

Messaggioda alesslud » gio nov 10, 2011 4:39 pm

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

<f:view>
<head>
<link href="styles.css" rel="stylesheet" type="text/css" />
<title>Paginatore</title>
</head>
<body>
<h:form>
<h:dataTable id="timezones" value="#{baseBean.data}" styleClass="names"
headerClass="namesHeader" footerClass="namesHeader"
columnClasses="name" var="row" rows="10">
<h:column>
<f:facet name="header">
<h:outputText value="Nome" />
</f:facet>
<h:outputText value="#{row}" />
</h:column>
</h:dataTable>
<aquilasiti:pager dataTableId="timezones" showpages="10"
selectedStyleClass="currentPage" />
</h:form>
</body>
</f:view>
</html>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

styles.css

Messaggioda alesslud » gio nov 10, 2011 4:39 pm

.currentPage {
color: red;
background: yellow;
}

.name {
text-align: left;
background-color: PowderBlue;
font-style: italic;
color: red;
}

.namesHeader {
text-align: center;
font-style: italic;
color: silver;
background-color: aqua;
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

index.html

Messaggioda alesslud » gio nov 10, 2011 4:39 pm

<html>
<head>
<meta http-equiv="Refresh" content= "0; URL=index.faces"/>
<title>Start Web Application</title>
</head>
<body>
<p>Please wait for the web application to start.</p>
</body>
</html>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

web.xml

Messaggioda alesslud » gio nov 10, 2011 4:40 pm

<web-app xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

paginatore.tld

Messaggioda alesslud » gio nov 10, 2011 4:40 pm

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>0.03</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>paginatore</short-name>
<uri>http://aquilasiti.com/paginatore</uri>
<description>
This tag library contains a pager tag.
</description>

<tag>
<name>pager</name>
<tag-class>com.aquilasiti.PaginatoreTag</tag-class>
<attribute>
<name>dataTableId</name>
</attribute>
<attribute>
<name>showpages</name>
</attribute>
<attribute>
<name>styleClass</name>
</attribute>
<attribute>
<name>selectedStyleClass</name>
</attribute>
</tag>
</taglib>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

faces-config.xml

Messaggioda alesslud » gio nov 10, 2011 4:40 pm

<?xml version="1.0"?>

<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

<faces-config>
<render-kit>
<renderer>
<component-family>javax.faces.Output</component-family>
<renderer-type>com.aquilasiti.Paginatore</renderer-type>
<renderer-class>com.aquilasiti.PaginatoreRenderer</renderer-class>
</renderer>
</render-kit>

<managed-bean>
<managed-bean-name>baseBean</managed-bean-name>
<managed-bean-class>com.aquilasiti.TimeZoneIDBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

TagPaginatore.java

Messaggioda alesslud » gio nov 10, 2011 4:41 pm

package com.aquilasiti.util;

import java.util.Iterator;
import java.util.Map;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

public class TagPaginatore {

public static ValueExpression createExpression(String attributeValue) {
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = context.getELContext();
ValueExpression valueExpression = elFactory.createValueExpression(
elContext, attributeValue, Object.class);
return valueExpression;
}


public static void setString(UIComponent component, String attributeName,
String attributeValue) {
if (attributeValue == null)
return;
ValueExpression valueExpression = createExpression(attributeValue);
if (!valueExpression.isLiteralText())
setValueBinding(component, valueExpression, attributeName);
else
component.getAttributes().put(attributeName, attributeValue);
}

public static void setInteger(UIComponent component, String attributeName,
String attributeValue) {
if (attributeValue == null)
return;
ValueExpression valueExpression = createExpression(attributeValue);
if (!valueExpression.isLiteralText())
setValueBinding(component, valueExpression, attributeName);
else
component.getAttributes().put(attributeName,
Integer.parseInt(attributeValue));
}

public static void setDouble(UIComponent component, String attributeName,
String attributeValue) {
if (attributeValue == null)
return;
ValueExpression valueExpression = createExpression(attributeValue);
if (!valueExpression.isLiteralText())
setValueBinding(component, valueExpression, attributeName);
else
component.getAttributes().put(attributeName, attributeValue);
}

public static void setBoolean(UIComponent component, String attributeName,
String attributeValue) {
if (attributeValue == null)
return;
ValueExpression valueExpression = createExpression(attributeValue);
if (!valueExpression.isLiteralText())
setValueBinding(component, valueExpression, attributeName);
else
component.getAttributes().put(attributeName, attributeValue);
}

public static void setStrings(UIComponent component, Map map) {
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
setString(component, (String) entry.getKey(),
(String) entry.getValue());
}
}

public static void setValueBinding(UIComponent component,
ValueExpression valueExpression, String attributeName) {
component.setValueExpression(attributeName, valueExpression);
}
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

TimeZoneIDBean.java

Messaggioda alesslud » gio nov 10, 2011 4:41 pm

package com.aquilasiti;

public class TimeZoneIDBean {
private String[] data = java.util.TimeZone.getAvailableIDs();

public String[] getData() {
return data;
}
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

PaginatoreTag.java

Messaggioda alesslud » gio nov 10, 2011 4:42 pm

package com.aquilasiti;

import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentELTag;

import com.aquilasiti.util.TagPaginatore;

public class PaginatoreTag extends UIComponentELTag {
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;
TagPaginatore.setInteger(component, "showpages", showpages);
TagPaginatore.setString(component, "dataTableId", dataTableId);
TagPaginatore.setString(component, "styleClass", styleClass);
TagPaginatore
.setString(component, "selectedStyleClass", selectedStyleClass);
}

public void release() {
super.release();
showpages = null;
dataTableId = null;
styleClass = null;
selectedStyleClass = null;
}

public String getRendererType() {
return "com.aquilasiti.Paginatore";
}

public String getComponentType() {
return "javax.faces.Output";
}
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

PaginatoreRenderer.java

Messaggioda alesslud » gio nov 10, 2011 4:42 pm

package com.aquilasiti;

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.render.Renderer;

public class PaginatoreRenderer 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) {
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();
}
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am


Torna a JSF e Struts

Chi c’è in linea

Visitano il forum: Nessuno e 1 ospite

cron