Friday, May 2, 2014

JSF Pagination with Bootstrap 3

Based on JSF pagination example from Oracle, http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/JSFTablePagination/JSFTablePagination.html I modified codes to use with Bootstrap, shown here.

List of improvements:
1. new look with Bootstrap 3
2. add final/first page
3. no. of rows
List of softwares:
1. Bootstrap 3, http://getbootstrap.com/getting-started/
2. Previous example, http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/JSFTablePagination/JSFTablePagination.html
Step:
1. Install jquery and bootstrap 3 libraries to Netbeans.

2. At jsf page, I created a new form (shown at the end).

3. At PaginationHelper, modify and add as follows:
public PaginationHelper(int pageSize, int page) {
        this.pageSize = pageSize;
        this.page=page;
    }
 //current page
    public int getCurrentPage() {
        return page;
    }  
    public int getFinalPage(){
        int count =getItemsCount();
     
        return count % pageSize ==0 ? (count/pageSize)-1 : count/pageSize;
      
    }
    public void setFinalPages(){
         this.page=getFinalPage();
       
    }

4. At DvDLibraryBean, modify and add:
private int pageSize;
    private int [] page;
//getter and setter
 @PostConstruct
    public void postConstruct() {
       
        page =new int[]{5, 10,15,20,30, 50};
        pageSize=10;
    }
public PaginationHelper getPagination() {
        if (pagination == null) {
            pagination = new PaginationHelper(this.pageSize, 0) {
                @Override
...
}
     //go to the 1st page
    public String firstPage() {
      pagination=null; //reset pagination to null or setting to page=0;
     
      recreateModel();
      return "home";
    }
   
  
    //go to the last page
    public String lastPage() {
     
  
      getPagination().setFinalPages();
      recreateModel();
      return "home";
    }
   
  
    public String recreatePageSize(AjaxBehaviorEvent e) {
        pagination=null;
         recreateModel();
        
      return "home";
    }
5. Modify faces-config.xml with referring to list_Bootstrap.xhtml

Then run it. You will see above pictures.

Here are codes of list_Bootstrap.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:jsf='http://xmlns.jcp.org/jsf'

      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:h="http://xmlns.jcp.org/jsf/html">

    <h:head id='head_t'>


        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width initial-scale=1.0"/>
        <link rel="stylesheet" href="#{request.contextPath}/faces/js/libs/bootstrap/css/bootstrap.css"/>

        <h:outputScript library="js" name="jquery.js" />
        <h:outputScript library="js"  name="bootstrap.js" />

    </h:head>

    <body >

        <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#">JSF 2.2</a>
                </div>

            </div>
        </div>

        <!-- Main jumbotron for a primary marketing message or call to action -->
        <div class="jumbotron">
            <div class="container">
                <br/>
                <h2><h:outputFormat value="#{msg.welcome}">
                        <f:param value="#{empty login.username ? 'Guest' : login.username}"/>
                    </h:outputFormat>
                    !</h2>
                <h:outputFormat value="#{msg.welcome_list}">
                    <f:param value="#{dvd.size}"/>
                </h:outputFormat>

            </div>
        </div>

        <div class="container">
            <!-- Example row of columns -->
            <div class="row">
                <div class="col-sm-3">
                    <h:form>
                        <h4> <h:commandLink value="#{msg.goToList_library}" action="list_Bootstrap"/></h4>
                        <p style="color:gray ">#{msg.goToAdd_dvd}</p>
                        <p style="color:gray ">#{msg.goToSet_prefs}</p>
                        <hr/>
                        <p style="color:gray ">#{msg.loginBtnTxt}</p>
                    </h:form>
                </div>
                <div class="col-sm-9">

                    <div class="panel-group">
                        <div class="panel panel-info">

                            <div class="panel-heading">List of DvDs
                            </div>
                                <h:form id="listF1">  

                                    <h:outputText escape="false" value="#{msg.ListCustomerEmpty}" rendered="#{dvd.dtmdl.rowCount == 0}"/>
                                    <h:panelGroup rendered="#{dvd.dtmdl.rowCount > 0}"> &#xA0; &#xA0;
                                        <h:outputText   value="#{dvd.pagination.pageFirstItem + 1}..#{dvd.pagination.pageLastItem + 1}/#{dvd.pagination.itemsCount}"/>&nbsp;
                                        <h:commandLink action="#{dvd.previous}" value="#{msg.Previous} #{dvd.pagination.pageSize}" rendered="#{dvd.pagination.hasPreviousPage}"/>&nbsp;
                                        <h:commandLink action="#{dvd.next}" value="#{msg.Next} #{dvd.pagination.pageSize}" rendered="#{dvd.pagination.hasNextPage}"/>&nbsp;
                                        <h:dataTable value="#{dvd.dtmdl}" var="item"  class="table table-responsive table-bordered table-striped"
                                                    >
                                            <h:column>
                                                <f:facet name="header">#{msg.column_Title}</f:facet>
                                                    #{item.title}
                                            </h:column>
                                            <h:column>
                                                <f:facet name="header">#{msg.column_Year}</f:facet>
                                                    #{item.releaseyear}
                                            </h:column>
                                            <h:column>
                                                <f:facet name="header">#{msg.column_Genre}</f:facet>
                                                    #{item.genre}
                                            </h:column>
                                        </h:dataTable>
                                    </h:panelGroup>
                                    <div class="container-fluid">
                                        <div class="col-xs-12">
                                           
                                            <button   class="btn btn-default btn-sm" jsf:action="#{dvd.firstPage()}"
                                                      jsf:disabled="#{dvd.pagination.currentPage eq 0}">
                                                <span class="glyphicon glyphicon-step-backward"></span>
                                            </button>
                                            <button   class="btn btn-default btn-sm" jsf:action="#{dvd.previous}"
                                                      jsf:disabled="#{not dvd.pagination.hasPreviousPage}">
                                                <span class="glyphicon glyphicon-backward"></span>
                                            </button>
                                            &#xA0;
                                            <span class="text-primary">#{dvd.pagination.currentPage+1}</span>
                                            &#xA0;
                                            <button  class="btn btn-default btn-sm" jsf:disabled="#{not dvd.pagination.hasNextPage}"
                                                     jsf:action="#{dvd.next()}" >
                                                <span class="glyphicon glyphicon-forward"></span>
                                            </button>

                                            <button   class="btn btn-default btn-sm" jsf:action="#{dvd.lastPage()}"
                                                      jsf:disabled="#{dvd.pagination.currentPage eq dvd.pagination.finalPage}">
                                                <span class="glyphicon glyphicon-step-forward"></span>
                                            </button>
                                            &#xA0;

                                            <h:selectOneMenu  class=" btn"   value="#{dvd.pageSize}"
                                                              >
                                                <f:selectItems  value="#{dvd.page}" var="pg"
                                                                itemLabel="#{pg}"
                                                                itemValue="#{pg}"/>
                                                <f:ajax listener="#{dvd.recreatePageSize}" render="listF1"/>
                                            </h:selectOneMenu>
                                        </div>   

                                    </div>
                                    <p/>
                                </h:form>
                        </div>

                    </div>
                </div>

            </div>

            <hr/>

            <footer>
                <p>JSF 2.2 Pagination with Bootstrap</p>
            </footer>
        </div> <!-- /container -->
    </body>
</html>




Saturday, April 26, 2014

To configure your Netbeans with the lastest JSF version

I've tried many ways to use a new JSF version, even used Maven but I could not success.

If using Maven, you will get a new library version (you see at project files & library). When complying and running at Netbeans console, it still shows the old version, because the Netbeans refers a library to Glassish server (if using Glassish server).

Another way I ever tried by adding the new JSF library at Ant Library Manager, it still uses the old version.

Solution if you are using Glassish server:

1. Go to /Applications/Netbeans/glassfish-4.0/glassfish/modules/
2. Replace the javax.faces.jar with javax.faces-2.2.6.jar
3. Restart Netbeans
4. Clean and build your project
5. You will see at Netbeans console, like this:
Info:   Initializing Mojarra 2.2.6 ( 20140304-1537 https://svn.java.net/svn/mojarra~svn/tags/2.2.6@12949) for context '/myApplication'

Noted:
1. I don't know why when updating a new library with Netbeans, it could not work, for example JSF.
2. Once you finished updating JSF version, you will see JSF 2.2 (when creating new web application.