Netbeans: Xắp xếp và phân trang trong EJB

trong bài này tôi dùng Netbeans Visual Web Pack, và database TRAVEL. Trong bài này có 2 phần 1) Enterprise application dùng Ejb. 2) Web application hiển thị dữ liệu.

  1. Tạo EJB modul

    tạo ejb project:

  • new project

  • Enterprise / Ejb modul

  • next

  • project name : TravelEjb

  • Server Sun, Java EE Version J2EE 5

  • Finish

Tạo entity class:

  • new file and folder

  • Persistent/ Entity Class from database

  • Create datasource

  • add datasource

  • chọn bảng PERSON

  • next

  • tạo persistent Unit

  • Create

  • Finish

Tạo stateless sessionbean

  • new Session Bean

  • EJB Name: Person, package: com.travel.servise

  • Ta sẽ tạo cả Remote và Local

  • Ta cần thêm 2 thư viện: ToplinkEssentials(có sẵn trong netbeans), Dataprovider.jar(netbeans-home/rave2.0/modules/ext)

  • Trong class PersonBean.java, chuột phải chọn Add Business Method. Ta cần 2 phương thức getPersons(), getcount()

  • Code như sau

public List getPersons(int startPosition, int maxResult, SortCriteria[] sc) {

String query = "select object(o) from Person as o";

if(sc != null){

query = query + " " + "order by " + " " + "o."+sc[0].getCriteriaKey();

if(!sc[0].isAscending()){

query = query + " desc";

}

}

Query q = em.createQuery(query);

q.setMaxResults(maxResult);

q.setFirstResult(startPosition);

return q.getResultList();

}

/* Count */

public int getCount() {

Query q = em.createQuery("select count(o) from Person as o ");

Long count = (Long)q.getSingleResult();

if(count != null){

return count.intValue();

}

return 0;

}


Build

  1. Tạo Web modul

    ta sẽ tạo một visual web application:

  • new project

  • Web/ Visual Web Application

  • Next

  • Project name: TravelWeb

Thêm lib TravelEjb vô TravelWeb

Tạo DataProvider:

  • Tạo class PersonDP

  • Chuột phải lên code chọn Enterprise Resource / call Enterprise Bean

  • Chọn bean Person

  • Thêm vào các phương thức sau: refreshDP(), getRowCount(), next(), previous()

  • Thêm vào 2 thuộc tính: startPosition and maxresults

  • Như sau:

    public class PersonDP extends ObjectListDataProvider{

    /* controls page size*/

    private int maxresults =5;

    /* starting row*/

    private int startPosition;

    /** Creates a new instance of PersonDP */

    public PersonDP() {

    /* needed by netbeans editor */

    setObjectType(Person.class);

    }

    /* fetch a fresh list of persons from ejb layer*/

    public void refreshDP(){

    clearObjectList();

    setList(lookupPersonBean().getPersons(getStartPosition(),getMaxresults(),null));

    }

    /* Fetch sorted list of persons from ejb layer*/

    public void refreshDP(SortCriteria[] sc){

    clearObjectList();

    setList(lookupPersonBean().getPersons(getStartPosition(),getMaxresults(),sc));

    }

    /* find count */

    public int getRowCount() throws DataProviderException {

    if(Beans.isDesignTime()){

    return 5;

    }

    return lookupPersonBean().getCount();}

    public int getMaxresults() {

    return maxresults;

    }

    public void setMaxresults(int maxresults) {

    this.maxresults = maxresults;}

    public int getStartPosition() {

    return startPosition;

    }

    public void setStartPosition(int startPosition) {

    this.startPosition = startPosition;

    refreshDP();

    }

    public void setLastPosition() {

    startPosition = (lookupPersonBean().getCount()/maxresults) * maxresults;

    refreshDP();

    }

    /*move to next page*/

    public void next(){

    if(startPosition + maxresults <= lookupPersonBean().getCount()){

    startPosition = startPosition + maxresults;

    }

    refreshDP();

    }

    public void previous(){

    if(startPosition <>

    startPosition = startPosition - maxresults;

    }

    if (startPosition <>

    startPosition =0;

    }

    refreshDP();

    }

    /* Helper for calling ejb service . Created by netbeans*/

    private PersonLocal lookupPersonBean() {

    try {

    Context c = new InitialContext();

    return (PersonLocal) c.lookup("java:comp/env/ejb/PersonBean");

    } catch(NamingException ne) {

    Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne);

    throw new RuntimeException(ne);

    }

    }

    }

Ta sẽ thự hiện phần view như sau:

  • Mở page1.jsp

  • Kéo Table trong Palette\Basic

  • Thêm vào SessionBean1 thuộc tính persondp kiểu PersonDP(khởi tạo luôn)

  • Build TravelWeb và TravelEjb

  • Restart netbeans

  • Mở Page1

  • Chuột phải lên table chọn table layout

  • Get Data Form chọn persondp

  • Trong tab Option chọn Show Clear Sort Button


Tạo EJBTableSorter.java

  • Tạo class EjbTableSorter.java

  • tạo trường ejbTableSorter trong SessionBean1(khởi tạo luôn)

    public class EjbTableSorter implements TableDataSorter , Serializable {

    /** Creates a new instance of EjbTableSorter */

    public EjbTableSorter() {

    }

    protected SortCriteria sortCriteria[];

    protected Locale sortLocale;

    /** Creates a new instance of EjbSorter */

    public EjbTableSorter(SortCriteria sortCriteria[], Locale sortLocale) {

    this.sortCriteria = sortCriteria;

    this.sortLocale = sortLocale;

    }

    public void setSortCriteria(SortCriteria[] sortCriteria) {

    this.sortCriteria = sortCriteria;

    }

    public SortCriteria[] getSortCriteria() {

    return sortCriteria;

    }

    public void setSortLocale(Locale locale) {

    this.sortLocale= locale;

    }

    public Locale getSortLocale() {

    return this.sortLocale;

    }

    /* returns sorted list of keys. Gets frersh sorted list of objects from ejb layer*/

    public RowKey[] sort(TableDataProvider provider, RowKey[] rows) throws DataProviderException {

    if(!Beans.isDesignTime()){

    PersonDP persondp = (PersonDP)provider;

    persondp.refreshDP(sortCriteria);

    System.err.println(" Executed sort EjbSorter.java");

    return persondp.getRowKeys(rows.length,null);

    }

    return provider.getRowKeys(rows.length,null);

    }

    }

Kết nối xắp xếp với trang

  • Mở page1 lên

  • Trong phần Layout chọn TableRowGroup1, chuột phải chọn Property Bindings

  • Chọn All radio

  • Trong khung bên trái chọn TableDataSorter

  • Trong khung bên phải chọn SessionBean1 \ ejbSorter

  • Close

Tạo button cho phân trang

  • Mở page1

  • Kéo GroupPanel vào page

  • Kéo 4 button như bên

  • Double vào button để đến phương thức hiện thực action cho button. Ta thêm lần lượt các phương thức sau vào từng button:

    <<: getSessionBean1().getPersondp().setStartPosition(0);

    < : getSessionBean1().getPersondp().previous();

    > : getSessionBean1().getPersondp().next();

    >>: getSessionBean1().getPersondp().setLastPosition();

  • Deloy Travel Ejb

  • Run Travel Web.

Cao Trong Hien

1 Response to "Netbeans: Xắp xếp và phân trang trong EJB"

Nặc danh said :
lúc 21:25 4 tháng 5, 2009
neu co hinh anh nua thi cang tot de nhin va truc quan hon

Đăng nhận xét