`
superheizai
  • 浏览: 64327 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

JSF2.0 分页代码的实现

    博客分类:
  • JSF
阅读更多
JSF2中DataTable的实现并没有提供分页实现,相比于JSF1.2中可用的richfaces等实现中的分页来说,JSF2可用的其它实现包并不多。参考网上一位同学的代码,并修改它的代码的bug,形成了这个新的代码。
界面代码:

 <h:form>
        <h:dataTable  id="dt1" value="#{testPaginate.list}"  binding="#{testPaginate.htmlDataTable}"   border="1" var="item" rows="4" width="100%" headerClass="datagrid-header" >
	<h:column>
       			<f:facet name="choose">
       		 		<h:outputText value="chosse" />
        		</f:facet>
//这里实现了多选功能或者单选功能的实现
            <h:selectBooleanCheckbox >
                <f:ajax listener="#{testPaginate.ajaxSelect}" execute="@this"></f:ajax>
            </h:selectBooleanCheckbox>
            <h:commandButton action="#{testPaginate.select}"></h:commandButton>
                            <h:outputText value="#{item.name}"></h:outputText>
			</h:column>
            <h:column>
       			<f:facet name="header">
       		 		<h:outputText value="name" />
        		</f:facet>
                            <h:outputText value="#{item.name}"></h:outputText>
			</h:column>
			<h:column>
        		<f:facet name="header">
       	 			<h:outputText value="user"/>
        		</f:facet>
                            <h:outputText value="#{item.user}"></h:outputText>
			</h:column>
			</h:dataTable>
      
<h:outputText value="total:#{testPaginate.itemCount} "/>
<h:outputText value="per page:#{testPaginate.pageSize} "/>
<h:outputText value="total page:#{testPaginate.pageCount} "/>
<h:outputText value="current:#{testPaginate.pageIndex} "/>
//本来没有加ajax用来分页。后来发现这只对分页时第一页的selectonebooleancheckbox
//起作用。后来发现,如果不加render代码在commanbutton上面的话,使得提交后的界面的
//界面找不到majarra(firebug提示)。研究了半天(半碗水的水平,只能摸索着做了,呵
//呵),才解决
 <f:ajax execute="@form" render="@form">
 <h:commandButton  value="firstpage"  action="#{testPaginate.firstAction}" disabled="#{testPaginate.pageIndex == 1 || empty testPaginate.list}"/>
 <h:commandButton  value="prevpage"  action="#{testPaginate.prevAction}" disabled="#{testPaginate.pageIndex == 1 || empty testPaginate.list}"/>
 <h:commandButton  value="nextpage" action="#{testPaginate.nextAction}" disabled="#{testPaginate.pageIndex == testPaginate.pageCount || empty testPaginate.list}"/>
 <h:commandButton  value="lastpage"   action="#{testPaginate.lastAction}" disabled="#{testPaginate.pageIndex == testPaginate.pageCount || empty testPaginate.list}"/>
 <h:commandButton  value="load"   action="#{testPaginate.doSomething}"/>
 </f:ajax>
</h:form>

后台bean:



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author liujg
 */
@ManagedBean
@SessionScoped
public class TestPaginate {

    private HtmlDataTable htmlDataTable=new HtmlDataTable();

    public HtmlDataTable getHtmlDataTable() {
        return htmlDataTable;
    }

    public void setHtmlDataTable(HtmlDataTable htmlDataTable) {
        this.htmlDataTable = htmlDataTable;
    }

    public ArrayList<User> getList() {
        
           if(list.size()<1)
              for(int i=0;i<10;i++){
               User user=new User();
                user.setName("name"+i);
                user.setUser("user"+i);
                list.add(user);
              }
          
        return list;
    }



    public void setList(ArrayList<User> list) {
        this.list = list;
    }

    public ArrayList<User> selected=new ArrayList<User>();



    public ArrayList<User> getSelected() {
        return selected;
    }

    public void setSelected(ArrayList<User> selected) {
        this.selected = selected;
    }

    public void select(){
         int index=htmlDataTable.getRowIndex();
         User selectedUser=(User)htmlDataTable.getRowData();
         System.out.println("you choose "+index+" record ,and User is"+selectedUser.getName()+":::"+selectedUser.getUser());
    }
//ajaxSelect方法实现了多选功能。
    public void ajaxSelect(AjaxBehaviorEvent event){
           HtmlSelectBooleanCheckbox b = (HtmlSelectBooleanCheckbox)event.getComponent();
  HtmlSelectBooleanCheckbox def = (HtmlSelectBooleanCheckbox)event.getSource();
   System.out.println(b.isSelected());

               int index=htmlDataTable.getRowIndex();
         User selectedUser=(User)htmlDataTable.getRowData();
         System.out.println("you choose ajax"+index+" record ,and User is"+selectedUser.getName()+":::"+selectedUser.getUser());
         selected.add(selectedUser);
    }

    private ArrayList<User>  list=new  ArrayList<User>();

    public int getItemCount() {
        return htmlDataTable.getRowCount();
    }

    public int getPageCount() {
        double pageDouble = (htmlDataTable.getRowCount() + htmlDataTable.getRows() - 1) / htmlDataTable.getRows();
        int pageCount = (int) Math.ceil(pageDouble);
        return pageCount;
    }

    public int getPageSize() {
        return htmlDataTable.getRows();

    }

    public int getPageIndex() {
        if(htmlDataTable.getFirst()==0){
            return 1;
        }
        return htmlDataTable.getFirst()/htmlDataTable.getRows()+1;
    }

    public void doSomething(){
          htmlDataTable.setFirst(htmlDataTable.getFirst() + htmlDataTable.getRows());
    }

    public void nextAction() {
                htmlDataTable.setFirst(htmlDataTable.getFirst() + htmlDataTable.getRows());
    }

    public void lastAction() {
          htmlDataTable.setFirst(htmlDataTable.getRowCount()/htmlDataTable.getRows()*htmlDataTable.getRows());

    }

    public void firstAction() {

        htmlDataTable.setFirst(0);
    }

    public void prevAction() {

        htmlDataTable.setFirst(htmlDataTable.getFirst() - htmlDataTable.getRows());
    }


}

User类:


  


  
分享到:
评论

相关推荐

    jsf学生信息管理

    jsf2.0,增删改查,分页,批量删除.代码齐全,包括jar包,数据库sql文件.对于刚刚接触jsf的绝对有作用.

    JTurboExplorer:MySQL,Oracle,Sybase等数据库浏览器。 来源制造商JSF。-开源

    它是帮助程序员检查数据库并如此轻松地生成代码的工具。 列出数据库,MySQL,Oracle和Sybase的表和视图。 从Excel 97-2003文件加载... 从表中生成代码JSF2.0(Action)和PrimeFaces(xhtml),Struts 1.3的代码,等等。

    JAVA上百实例源码以及开源项目源代码

    Java源代码实现部分,比较有意思,也具参考性。像坐标控制、旋转矩阵、定时器、生成图像、数据初始化、矩阵乘法、坐标旋转、判断是否是顺时针方向排列、鼠标按下、放开时的动作等,都可在本源码中得以体现。 Java...

    JAVA上百实例源码以及开源项目

    Java源代码实现部分,比较有意思,也具参考性。像坐标控制、旋转矩阵、定时器、生成图像、数据初始化、矩阵乘法、坐标旋转、判断是否是顺时针方向排列、鼠标按下、放开时的动作等,都可在本源码中得以体现。 Java...

    JAVA程序开发大全---上半部分

    本书内容丰富、技术全面、案例实用,而且所有的实例都以MyEclipse工程的形式组织,并按章节的顺序组织在附书光盘中,源代码工程都经过精心调试,可以直接导入MyEclipse中运行。 本书内容精练、重点突出、实例丰富,...

    java开源包8

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包10

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包3

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包4

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包1

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包11

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包2

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包6

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包5

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包7

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包9

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包101

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    Java资源包01

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

Global site tag (gtag.js) - Google Analytics