- 浏览: 97580 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
chifanbua:
我基本上在iteye上很少留言的 不过你的这个写的确实不错 用 ...
Oracle decode函数说明 -
shaoxuexue86:
谢谢啊
RMI 开发步骤 -
huxiaojun_198213:
RMIRegistHelper只是负责注册远程对象,与客户端所 ...
RMI 开发步骤 -
shaoxuexue86:
请问客户端程序ClientTest 与 RMIRegistHe ...
RMI 开发步骤 -
huxiaojun_198213:
本章节可能有一些细节方面翻译得不是很好,有出入的地方,还请各 ...
RMI动态类加载
Lazy JSF Primefaces Datatable Pagination – Part 1
Today we will do JSF datatable pagination with a Lazy List with a view scoped managed bean. What all those words/expressions mean?
There are several JSF frameworks today that provide datatables with a ready pagination, columns sorter and others functionalities. Today we will use the Primefaces datatable.
Usually the datatables will put the displayed List, with the entities, inside the user http session. Increasing the objects inside the user session will have a direct impact on the server performance; each user that displays a datatable, that keeps a list in the session, will be allocating more and more memory in the server.
To look like real life, our post will use JPA and HSQLDB as database, and we will use JPQL to query the data.
In the end of this post you will find the link to download the source code.
We will use:
■JSF 2.0 – JBoss 7 Implementation
■JBoss 7.1 – the code of this post should be applied to all servers
■Eclipse Indigo
■JPA 2.0 – JBoss 7 Implementation
■HSQLDB (2.2.8) – The HSQL it is a memory database, it will be easier to run it.
■Primefaces 3.2
This post is not about good development practices nor about adding classes layers of project modeling. I just intend to show how to do a pagination without a session managed bean.
We will have only one entity that will be persisted, the Player class. Bellow the class code:
We will need to create a persistence.xml file inside the “src/META-INF” folder:
To abstract the database transaction we will use a class named MyTransaction:
You can see in the code above that the class is just an abstraction to the database connection; it will help us with the database queries.
You could use any type of connection or you could use even a EJB to be avoiding this manually connection management.
Check the Connection class code:
We could use an injected UserTransaction by the JSF but we chose to use JNDI lookup.
There are several Primefaces calls that are invoked outside the JSF context, you may get some NullPointerException if you try to access the references that should be injected.
There are several ways to work with this issue, but we will use the JNDI Lookup to the EntityManager and to the UserTransaction.
Our last class will be the PlayerDAO:
In the PlayerDAO class we have only 3 methods to work with our pagination. Notice that there is no method to list all the players in our database.
Create the folder “YOU_JBOSS/modules/org/hsqldb/main“. Inside this folder create a file named “module.xml“. Write the code bellow inside the “module.xml” file:
Copy the “hsqldb.jar” file to the folder “main” that you just created. This file you will find inside the HSQLDB jar you download in the path “hsqldb-2.2.8.zip/hsqldb-2.2.8/hsqldb/lib“.
Edit the file “YOU_JBOSS/standalone/configuration/standalone.xml” and add the code bellow in the “datasources” node:
In the drivers node add:
from:http://www.javacodegeeks.com/2012/04/lazy-jsf-primefaces-datatable.html
Today we will do JSF datatable pagination with a Lazy List with a view scoped managed bean. What all those words/expressions mean?
There are several JSF frameworks today that provide datatables with a ready pagination, columns sorter and others functionalities. Today we will use the Primefaces datatable.
Usually the datatables will put the displayed List, with the entities, inside the user http session. Increasing the objects inside the user session will have a direct impact on the server performance; each user that displays a datatable, that keeps a list in the session, will be allocating more and more memory in the server.
To look like real life, our post will use JPA and HSQLDB as database, and we will use JPQL to query the data.
In the end of this post you will find the link to download the source code.
We will use:
■JSF 2.0 – JBoss 7 Implementation
■JBoss 7.1 – the code of this post should be applied to all servers
■Eclipse Indigo
■JPA 2.0 – JBoss 7 Implementation
■HSQLDB (2.2.8) – The HSQL it is a memory database, it will be easier to run it.
■Primefaces 3.2
This post is not about good development practices nor about adding classes layers of project modeling. I just intend to show how to do a pagination without a session managed bean.
We will have only one entity that will be persisted, the Player class. Bellow the class code:
package com.model; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Player implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int hashCode() { return getId(); } @Override public boolean equals(Object obj) { if(obj instanceof Player){ Player player = (Player) obj; return player.getId() == getId(); } return false; } }
We will need to create a persistence.xml file inside the “src/META-INF” folder:
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="JSFPU" transaction-type="JTA"> <jta-data-source>java:/JSFDS</jta-data-source> <properties> <property name="hibernate.show_sql" value="false" /> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" /> <property name="hibernate.connection.shutdown" value="true" /> <property name="hibernate.hbm2ddl.auto" value="create-drop" /> </properties> </persistence-unit> </persistence>
To abstract the database transaction we will use a class named MyTransaction:
package com.connection; import java.io.Serializable; import javax.persistence.EntityManager; import javax.transaction.HeuristicMixedException; import javax.transaction.HeuristicRollbackException; import javax.transaction.NotSupportedException; import javax.transaction.RollbackException; import javax.transaction.SystemException; public class MyTransaction implements Serializable { /** * */ private static final long serialVersionUID = 1L; private Connection connection = new Connection(); public void begin() throws NotSupportedException, SystemException { connection.begin(); } public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException { connection.commit(); } public int getStatus() throws SystemException { return connection.getStatus(); } public void rollback() throws IllegalStateException, SecurityException, SystemException { connection.rollback(); } public void setRollbackOnly() throws IllegalStateException, SystemException { connection.setRollbackOnly(); } public void setTransactionTimeout(int timeout) throws SystemException { connection.setTransactionTimeout(timeout); } public static MyTransaction getNewTransaction() { return new MyTransaction(); } public EntityManager getEntityManager() { return connection.getEntityManager(); } }
You can see in the code above that the class is just an abstraction to the database connection; it will help us with the database queries.
You could use any type of connection or you could use even a EJB to be avoiding this manually connection management.
Check the Connection class code:
package com.connection; import java.io.Serializable; import javax.naming.Context; import javax.naming.InitialContext; import javax.persistence.EntityManager; import javax.transaction.HeuristicMixedException; import javax.transaction.HeuristicRollbackException; import javax.transaction.NotSupportedException; import javax.transaction.RollbackException; import javax.transaction.SystemException; import javax.transaction.UserTransaction; public class Connection implements Serializable { private static final long serialVersionUID = 1L; /** * Get the user transaction by JNDI * * @return the user transaction */ public UserTransaction getUserTransaction() { UserTransaction ut = null; try { Context c = new InitialContext(); ut = (UserTransaction) c.lookup("java:comp/UserTransaction"); } catch (Exception e) { e.printStackTrace(); } return ut; } /** * Get the EntityManayger by JNDI * * @return the entity manager */ public EntityManager getEntityManager() { EntityManager em = null; try { Context initCtx = new InitialContext(); // The JSFPU must be written in the web.xml em = (EntityManager) initCtx.lookup("java:comp/env/JSFPU"); } catch (Exception e) { e.printStackTrace(); } return em; } public void begin() throws NotSupportedException, SystemException { getUserTransaction().begin(); } public void commit() throws SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException { getUserTransaction().commit(); } public int getStatus() throws SystemException { return getUserTransaction().getStatus(); } public void rollback() throws IllegalStateException, SecurityException, SystemException { getUserTransaction().rollback(); } public void setRollbackOnly() throws IllegalStateException, SystemException { getUserTransaction().setRollbackOnly(); } public void setTransactionTimeout(int timeout) throws SystemException { getUserTransaction().setTransactionTimeout(timeout); } }
We could use an injected UserTransaction by the JSF but we chose to use JNDI lookup.
There are several Primefaces calls that are invoked outside the JSF context, you may get some NullPointerException if you try to access the references that should be injected.
There are several ways to work with this issue, but we will use the JNDI Lookup to the EntityManager and to the UserTransaction.
Our last class will be the PlayerDAO:
package com.dao; import java.io.Serializable; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.Query; import com.connection.MyTransaction; import com.model.Player; public class PlayerDAO implements Serializable { private static final long serialVersionUID = 1L; private MyTransaction myTransaction; public PlayerDAO(MyTransaction transaction) { this.myTransaction = transaction; } /** * Find players in the DB * * @param startingAt the first "row" db that the query will search * @param maxPerPage the amount of records allowed per "trip" in the DB * @return a players java.util.List */ @SuppressWarnings("unchecked") public List<Player> findPlayers(int startingAt, int maxPerPage) { EntityManager em = myTransaction.getEntityManager(); // regular query that will search for players in the db Query query = em.createQuery("select p from Player p"); query.setFirstResult(startingAt); query.setMaxResults(maxPerPage); return query.getResultList(); } /** * Creates 100 players in the DB */ public void create100Players() { EntityManager em = myTransaction.getEntityManager(); Player player; for (int x = 0; x < 100; x++) { player = new Player(); player.setName("Player: " + x); player.setAge(x); em.persist(player); } em.flush(); } /** * Sum the number of players in the DB * * @return an int with the total */ public int countPlayersTotal() { EntityManager em = myTransaction.getEntityManager(); Query query = em.createQuery("select COUNT(p) from Player p"); Number result = (Number) query.getSingleResult(); return result.intValue(); } }
In the PlayerDAO class we have only 3 methods to work with our pagination. Notice that there is no method to list all the players in our database.
Create the folder “YOU_JBOSS/modules/org/hsqldb/main“. Inside this folder create a file named “module.xml“. Write the code bellow inside the “module.xml” file:
<module xmlns="urn:jboss:module:1.0" name="org.hsqldb"> <resources> <resource-root path="hsqldb.jar" /> </resources> <dependencies> <module name="javax.api" /> <module name="javax.transaction.api" /> </dependencies> </module>
Copy the “hsqldb.jar” file to the folder “main” that you just created. This file you will find inside the HSQLDB jar you download in the path “hsqldb-2.2.8.zip/hsqldb-2.2.8/hsqldb/lib“.
Edit the file “YOU_JBOSS/standalone/configuration/standalone.xml” and add the code bellow in the “datasources” node:
<datasource jndi-name="java:/JSFDS" pool-name="JSFDS_POOL" enabled="true" jta="true" use-java-context="true" use-ccm="true"> <connection-url> jdbc:hsqldb:mem:jsfinmemory </connection-url> <driver> hsqldb </driver> <pool> <prefill>false</prefill> <use-strict-min>false</use-strict-min> <flush-strategy>FailingConnectionOnly</flush-strategy> </pool> <security> <user-name> sa </user-name> <password> </password> </security> </datasource>
In the drivers node add:
<driver name="hsqldb" module="org.hsqldb"/>
from:http://www.javacodegeeks.com/2012/04/lazy-jsf-primefaces-datatable.html
发表评论
-
jsf+spring
2012-12-05 01:52 687附件为spring+jsf整合 -
JSF2 + Primefaces3 + Spring3 & Hibernate4 Integration Project
2012-12-05 01:04 1562This article shows how to integ ... -
JSF 2 + Spring 3 integration example
2012-12-05 00:41 1550In this tutorial, we will show ... -
primefaces cook book 源代码分享
2012-11-23 02:30 1224primefaces cook book 源代码分享 -
Full Web Application Tomcat JSF Primefaces JPA Hibernate – Part 2
2012-11-19 23:20 2114Full Web Application Tomcat JSF ... -
Full Web Application Tomcat JSF Primefaces JPA Hibernate – Part 1
2012-11-19 23:20 2533Full Web Application Tomcat JSF ... -
Lazy JSF Primefaces Datatable Pagination – Part 2
2012-11-18 19:19 1411Lazy JSF Primefaces Datatable P ...
相关推荐
Lazy
Lazy_Theta_star是在 Theta_star上的进一步改进,Theta_star是当节点加入open表时和当前点的父节点进行比较g值是否更小,对一些不必要的节点计算浪费了时间,而Lazy_Theta_star则是在弹出open表后进行比较,减少了...
jQuery_lazyload插件示例Demo
Lazyload是通过延迟加载来实现按需加载,达到节省资源,加快浏览速度的目的。 网上也有不少类似的效果,这个Lazyload主要特点是: 支持使用window(窗口)或元素作为容器对象; 对静态(位置大小不变)元素做了大量...
Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预加载的处理方式正好是相反的. 在包含很多...
很好的资源,快来下载,里面有经典的例子。让开发lazy起来吧
lazyload, JavaScript, 图片懒加载, h5
图片懒加载lazyload,增快页面访问速度。
Lazy Load Plugin for jQuery
macOS Mojave 10.14 18A391 Lazy Installer.cdr
Lazy Foo' Productions - Beginning Game Programming v2.0 SDL 游戏编程教程 by Lazy Foo CHM 打包下载,包含示例代码,页面代码使用JavaScript Chill着色,看着更舒服了。 官网:lazyfoo.net/tutorials/SDL/index...
lazyload-2.x 最新版本包括api使用
lazyload技术内幕,当下最流行的图片浏览技术
C# Lazy<T> 延迟优化算法对比 C# Lazy<T> 延迟优化算法对比
jquery.lazyload.min.js插件 直接下载引入即可~~~~~~~
lazy binomial heaps的oython实现,优先队列。采用双向循环链表实现,api:merge,insert,find_min,extractMin,coalesce_step,updateMin。
优化网站加载速度,使用lazyload.js,有完整的demo,有jquery.lazyload.js和jquery-1.11.0.min.js
JQuery Lazyload加载图片实例
lazyload1.9.3,图片延迟随滚动条显示。$(function() { $("img.lazyload").lazyload()});
Lazyload图片延迟加载效果