📄 在jsf中实现分页 jsf- 实现 java● 咖啡馆 -- jsf中文论坛.htm
字号:
alt=发送短消息
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/message.gif"
align=absMiddle border=0></A> <A title=发送邮件
href="http://www.hexiao.cn/jsf/sendemail.php?username=icess"><IMG
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/email.gif"
align=absMiddle border=0></A> <A
href="http://www.hexiao.cn/jsf/sendemail.php?action=tofriend&fid=13&tid=13&fpage="><IMG
alt=推荐此贴
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/emailto.gif"
align=absMiddle border=0></A> <A
href="http://www.hexiao.cn/jsf/post.php?action=quote&fid=13&tid=13&article=2"><IMG
alt=引用回复这个贴子
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/quote.gif"
align=absMiddle border=0></A> <A
href="http://www.hexiao.cn/jsf/post.php?action=modify&fid=13&tid=13&article=2"><IMG
alt=删除或编辑改帖子
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/edit.gif"
align=absMiddle border=0></A> 【字体:<A
onclick="text2.style.fontSize='16px';"
href="javascript:;">大</A> <A
onclick="text2.style.fontSize='14px';"
href="javascript:;">中</A> <A
onclick="text2.style.fontSize='12px';"
href="javascript:;">小</A>】
<HR width="100%" color=#e5e3e3 SIZE=1>
<IMG src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/10.gif"
align=left border=0> <SPAN
class=tpc_title> </SPAN><BR><SPAN class=tpc_content
id=text2><BR><BR>在JSF中实现<B
style="COLOR: red; BACKGROUND-COLOR: #ffff66">分页</B></FONT>(三)
<BR>好久没有写点东西了,这次想把JSF中的<B
style="COLOR: red; BACKGROUND-COLOR: #ffff66">分页</B></FONT>系列文章再扩充一点,说明一下查询和<B
style="COLOR: red; BACKGROUND-COLOR: #ffff66">分页</B></FONT>结合的情况,当我们把查询条件和查询结果放到一个页面上时,查询还是非常容易实现的,甚至不需要我们手工去从数据库中查询。
<BR><BR>在本系列文章中的第二篇中,介绍了一种 Load On
Demand的方式,我们在这里需要继续利用这种方式,并对其做一些小小的扩展。这里我们使用 Hibernate3
作为持久化方案。 <BR><BR>简单的介绍一下应用情景,一个系统中包含了一些 Customer
的信息,我们需要对其进行查询并对查询结果进行<B
style="COLOR: red; BACKGROUND-COLOR: #ffff66">分页</B></FONT>。
<BR><BR>首先处理条件查询的情况,通常会根据 VO 中的字段进行 like
型查询,有时候时间或数字之类的会使用Between查询,因为查询条件一般不会很复杂,在这里,使用 Hibernate3 中的
Criteria 查询来处理这样的情况,我们把所有的查询条件通过 Customer 这个 VO 传进来,然后只对非空字段进行
like 查询,我们用到这样的方法。 <BR><BR><BR><FONT
color=red>以下是代码:</FONT><BR><TEXTAREA class=java name=code rows=15 cols=100>public List queryByConditions(Customer customer, int startRow, int pageSize)
{
Criteria criteria = getSession().createCriteria(Customer. class );
if ( ! StringUtils.isEmpty(customer.getCustomerName()))
{
criteria.add(QueryUtils.getCriteriaParam( " customerName " , customer.getCustomerName()));
}
if ( ! StringUtils.isEmpty(customer.getAddress()))
{
criteria.add(QueryUtils.getCriteriaParam( " address " , customer.getAddress()));
}
if ( ! StringUtils.isEmpty(customer.getFax()))
{
criteria.add(QueryUtils.getCriteriaParam( " fax " , customer.getFax()));
}
return criteriaPagedList(criteria, startRow, pageSize);
}
</TEXTAREA><BR><BR>另外对应的一个count方法略去,只需要在前面加入一个<BR>criteria.setProjection(Projections.count("customerId"));
<BR><BR>因为考虑到以后的扩展,使用了一个Utils方法,QueryUtils.getCriteriaParam方法
<BR><BR><BR><BR><FONT color=red>以下是代码:</FONT><BR><TEXTAREA class=java name=code rows=15 cols=100>public static final SimpleExpression getCriteriaParam(String name, String param)
{
return Expression.like(name, " % " + param + " % " );
}
</TEXTAREA><BR><BR>我们可以很容易的在 Backing Bean 上通过 Service
层拿到这个查询结果的 List 和 Count 值,相关的getDatePage方法如下。<BR>如果你不了解这个
getDataPage 方法的含义,请仔细阅读“在JSF中实现<B
style="COLOR: red; BACKGROUND-COLOR: #ffff66">分页</B></FONT>(二)”一文,并仔细思考该方法的含义。
<BR><BR><BR><BR><FONT color=red>以下是代码:</FONT><BR><TEXTAREA class=java name=code rows=15 cols=100>protected DataPage getDataPage( int startRow, int pageSize)
{
List queryCustomerList = customerService.queryCustomer(customer, this .startRow, this .getPageSize());
int dataSetSize = customerService.countQueryCustomer(customer);
return new DataPage(dataSetSize, startRow, queryCustomerList);
}
</TEXTAREA><BR><BR>在同一个 Backing Bean 中,我们放了一个存放查询条件的VO -
Customer,并在页面中使用<t:saveState>保存其状态,使其查询条件不会随着翻页而丢失。
<BR><BR><t:saveState
value="#{customerListBean.customer}"/>
<BR><BR>在页面中,我们把所有的查询条件都放到该 VO 中,在 getDataPage
方法中就会在适当的时候调用新的查询条件来查询新的数据,这一切都不需要我们动手的。 <BR><BR>在 Backing
Bean 中有这样的一个方法: <BR><BR>public String
query()<BR>{<BR>dataModel.setWrappedData(getDataPage( 0 ,
getPageSize()));<BR>return " success " ;<BR>}
<BR>只是把数据清空,并强制 PagedListDataModel
读取数据,然后我们返回相同的页面,这个时候,系统按照用户输入的查询条件拿到查询结果以后,返回同一页面,该页面中的使用
LocalDataModel 的那个 DataTable 就会把结果显示出来。 <BR><BR>请注意,这里
LocalDataModel 和 Customer 都在同一个 Backing Bean 中。
<BR><BR>是不是觉得很简单呢,一切都归功于 getDataPage
这个方法,我们几乎不需要做什么额外的操作就可以达到我们的目的。<BR><BR></SPAN></TD></TR>
<FORM method=post>
<TR vAlign=bottom bgColor=#ffffff>
<TD colSpan=5><BR><!----><IMG
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/sigline.gif"
border=0> <BR>程序<BR><!----><FONT color=red>[2楼]</FONT> |
IP:已记录| <SPAN class=bold>Posted:</SPAN>2006-09-8 1:40 PM|</TD>
<TD vAlign=bottom align=right><A
href="javascript:scroll(0,0)"><IMG alt=回到顶端
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/top.gif"
border=0></A></TD></TR></FORM></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE width="100%" align=center>
<TBODY>
<TR>
<TD height=1></TD></TR></TBODY></TABLE><!--<!--帖子模版-->
<TABLE style="TABLE-LAYOUT: fixed; WORD-WRAP: break-word" cellSpacing=1
cellPadding=0 width="100%" align=center bgColor=#e5e3e3>
<TBODY>
<TR>
<TD
style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; PADDING-TOP: 5px"
vAlign=top width="20%" bgColor=#f7f7f7 height="100%"><A
name=lastatc></A><FONT face=Gulim color=#000066><SPAN
class=bold>ljwove</SPAN></FONT>
<TABLE style="TABLE-LAYOUT: fixed" cellSpacing=0 cellPadding=0
width="95%" border=0>
<TBODY>
<TR>
<TD align=middle><BR><IMG
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/0.gif"
border=0></TD></TR></TBODY></TABLE><BR><IMG
src="D:\resource\jsf\jsf分页\在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files\0(1).gif"><BR>级别:
<FONT color=#555555>新手上路<IMG alt=该用户目前不在线
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/offonline.gif"></FONT><BR>发贴:
<FONT color=green><SPAN class=bold>3</SPAN></FONT> (精华:<FONT
color=green><SPAN class=bold>0</SPAN></FONT>)<BR>威望: <FONT
color=#984b98><SPAN class=bold>1</SPAN></FONT><BR>咖啡豆: <FONT
color=red><SPAN
class=bold>3</SPAN></FONT><BR>注册时间:2006-10-16<BR>最后登陆:2006-11-03 </TD>
<TD vAlign=top width="80%" bgColor=#f7f7f7 height="100%">
<TABLE style="TABLE-LAYOUT: fixed; WORD-WRAP: break-word"
height="100%" cellSpacing=0 cellPadding=4 width="99%"
align=center><TBODY>
<TR>
<TD vAlign=top width="91%" bgColor=#f7f7f7 colSpan=5><A
href="http://www.hexiao.cn/jsf/profile.php?action=show&username=ljwove"><IMG
alt=查看作者资料
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/profile.gif"
align=absMiddle border=0></A> <A
href="http://www.hexiao.cn/jsf/message.php?action=write&msgid=ljwove"><IMG
alt=发送短消息
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/message.gif"
align=absMiddle border=0></A> <A title=发送邮件
href="http://www.hexiao.cn/jsf/sendemail.php?username=ljwove"><IMG
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/email.gif"
align=absMiddle border=0></A> <A
href="http://www.hexiao.cn/jsf/sendemail.php?action=tofriend&fid=13&tid=13&fpage="><IMG
alt=推荐此贴
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/emailto.gif"
align=absMiddle border=0></A> <A
href="http://www.hexiao.cn/jsf/post.php?action=quote&fid=13&tid=13&article=3"><IMG
alt=引用回复这个贴子
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/quote.gif"
align=absMiddle border=0></A> <A
href="http://www.hexiao.cn/jsf/post.php?action=modify&fid=13&tid=13&article=3"><IMG
alt=删除或编辑改帖子
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/edit.gif"
align=absMiddle border=0></A> 【字体:<A
onclick="text3.style.fontSize='16px';"
href="javascript:;">大</A> <A
onclick="text3.style.fontSize='14px';"
href="javascript:;">中</A> <A
onclick="text3.style.fontSize='12px';"
href="javascript:;">小</A>】
<HR width="100%" color=#e5e3e3 SIZE=1>
<IMG src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/2.gif"
align=left border=0> <SPAN
class=tpc_title> </SPAN><BR><SPAN class=tpc_content
id=text3><BR><BR>原来是这个样子的。不错。<BR></SPAN></TD></TR>
<FORM method=post>
<TR vAlign=bottom bgColor=#f7f7f7>
<TD colSpan=5><BR><!----><FONT color=red>[3楼]</FONT> | IP:已记录|
<SPAN class=bold>Posted:</SPAN>2006-10-16 2:52 PM|</TD>
<TD vAlign=bottom align=right><A
href="javascript:scroll(0,0)"><IMG alt=回到顶端
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/top.gif"
border=0></A></TD></TR></FORM></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE width="100%" align=center>
<TBODY>
<TR>
<TD height=1></TD></TR></TBODY></TABLE><!----><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" align=center>
<FORM name=jump method=post>
<TBODY>
<TR>
<TD align=left><SPAN class=bold>本版只有一页</SPAN></TD>
<TD vAlign=center align=right><SELECT
onchange="if(this.options[this.selectedIndex].value != '') { window.location=('thread.php?fid='+this.options[this.selectedIndex].value) }">
<OPTION value="" selected>快速跳至</OPTION> <OPTION
value="">公共讨论区</OPTION><OPTION value=10> |-
新闻聚焦</OPTION><OPTION value=4> |- 软件技术大杂荟</OPTION><OPTION
value="">JSF技术讨论区</OPTION><OPTION value=11> |-
JSF-新手入门</OPTION><OPTION value=13> |- JSF- 实现</OPTION><OPTION
value=15> |- JSF-AJAX应用</OPTION><OPTION value=16> |-
JSF-组件扩展</OPTION><OPTION value=19> |-
JSF-开发工具</OPTION><OPTION value=12> |- JSF-
Frails扩展项目</OPTION><OPTION value=17> |-
JSF-其他问题</OPTION><OPTION value="">其他开源技术交流区</OPTION><OPTION
value=5> |- Breezy</OPTION><OPTION value=7> |-
Lucene/Wicket/htmlParser</OPTION><OPTION
value="">咖啡馆站务管理区</OPTION><OPTION value=2> |-
咖啡馆管理区</OPTION></SELECT> </TD></TR></FORM>
<TR>
<TD align=left><BR><IMG
src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/home.gif"
align=absBottom> <SPAN class=bold><A
href="http://www.hexiao.cn/jsf/index.php">Java● 咖啡馆 -- JSF中文论坛</A>
-> <A
href="http://www.hexiao.cn/jsf/thread.php?fid=13&page=">JSF-
实现</A></SPAN></TD>
<TD align=right><BR><A
href="http://www.hexiao.cn/jsf/post.php?fid=13"><IMG
src="在JSF中实现分
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -