Jadepool application example: Query and paging

Source: Internet
Author: User
Tags variable scope

It is very easy to achieve ethnic management. Refer to China's Administrative Division management. HereFocuses on the implementation of jadepool query and primefaces Paging.

1. Modify the indextemplate.xhtml menu item by referring to the Chinese Administrative Area Management
Change <P: menuitem value = "" url = "#"/> to <P: menuitem value = "文" Outcome = "cn_people"/>;

Create the cn_people.xhtml file. Because the outcome attribute is used, if the file does not exist, an exception occurs.

For the content of the cn_lele.xhtml file, see Attachment 1.

2. Create a managed Bean

@ManagedBean@SessionScopedpublic class PeopleBean {  ...}

Use @ managedbean injection to indicate that the class is a JSF managed bean, and the default variable name is peoplebean. You can use @ managedbean (name = "Minzu ") name the variable of the managed bean as "Minzu ".
@ Sessionscoped injection indicates that the variable scope of this class is within the session range.

For the peoplebean. Java content, see appendix 2.

1. Add four attributesAnd add the set and get methods for these four attributes.

Private map peoplemap = new linkedhashmap (); // national record private map peopleselected = new linkedhashmap (); // Private list of selected ethnic records <map> peoplelist = new arraylist (); // ethnic records list, private listmapdatamodel lelemodel = new listmapdatamodel (peoplelist, "lele_id"); // The National Data Model. "lele_id" is the primary key name of the cn_people database table.

Generate the set and get methods (content omitted) for the four attributes ).
These four attributes belong to M (data) at the MVC level ).
Peoplemap is equivalent to the Entity Bean or pojo bean in the Orm. In jadepool, map is used to replace all entity beans or pojo beans..

2. Record Initialization

Create initialization method:

    private void init() {        Jade j=new Jade();        peopleList=j.query("select * from cn_people order by people_id");        peopleModel = new ListMapDataModel(peopleList, "people_id");        j.commit();    }

Call Init () in the constructor ().

3. Import ethnic records

Like administrative divisions, ethnic records are packaged in jadepool-1.0-GBK and can be exported by calling the people () method of China. The exported records exist in the list <map> type, each record contains three key names: Minzu, pinxie, and Daima. In the cn_people table, there are three fields with the same name as the export record, which are used to save the export record fields with the same name. Change the field name zimu in the original cn_people table to Daima. After modifying the database structure information, you need to re-run the application to ensure that the jadepool uses the limited multi-column mode to reload the database structure information.

Public void guidepeople () {China Cn = New China (); List <map> V = cn. people (); // For (map m: V) {// system. out. println (m); // {Minzu = Han nationality, pinxie = Han, daima = Ha} // change the field name zimu in the original cn_people table to the field name Daima //} Jade J = new JADE (); J. delete ("delete from cn_people"); J. insert ("cn_people", V, true); // true, automatically Insert the primary key peoplelist = J. query ("select * From cn_people order by people_id"); // query lelemodel = new listmapdatamodel (peoplelist, "people_id"); // create a data model J. commit ();}

3. Complete the content on the cn_lele.xhtml page

On the cn_people.xhtml page, add the primefaces P: tabview component and create two tabs, one for paging and the other for importing.

1. Import ethnic records

On the tab page, initialize the ethnic records with the following code:

<P: commandbutton value = "initialize nation" Action = "# {lelebean. guidepeople} "icon =" UI-icon-disk "update =" @ form: bottomform: bottomgrowl "/>

Click "initialize nation" to trigger the Action event and call the guidepeople method of the managed bean. The update attribute calls the Ajax method to update the display of the specified component element. Here, two component elements are updated, @ form and: bottomform: bottomgrowl, which are separated by "" spaces. @ Form indicates the current form. bottomform: bottomgrowl indicates the <P: growl/> message display component defined at the bottom of the template webpage.

Is the result of initialization of the national record, prompting the message to be presented by the P: growl component.

 


2. Browse ethnic records

Browsing ethnic records is achieved by using the P: datatable component of primefaces. This component presents the lelemodel data model. The Code is as follows:

<P: datatable id = "leledatatable" value = "# {lelebean. peoplemodel} "Var =" M "paginator =" true "paginatorposition =" bottom "rows =" 10 "paginatortemplate =" {currentpagereport} {firstpagelink} {tags} {pagelinks} {nextpagelink} {lastpagelink} {rowsperpagedropdown} "rowsperpagetemplate =" 10, 20, 40, 60 "selection =" # {peoplebean. peopleselected} "selectionmode =" single "> <P: column headertext =" primary key "style =" text-align: center; "sortby =" # {M. people_id} "># {M. people_id} </P: column> <P: column headertext = "national" sortby = "# {M. minzu} ">#{ M. minzu} </P: column> <P: column headertext = "spelling" sortby = "# {M. pinxie} ">#{ M. pinxie} </P: column> <P: column headertext = "letter" sortby = "# {M. daima} ">#{ M. daima} </P: column> </P: datatable>

Code annotation:

1. The Value Attribute connects to the data model,

2. The VaR attribute is used for each record,

3. paginator = "true" indicates that the navigation bar is to be displayed,

4. paginatorposition = "bottom": place the navigation bar to the bottom,

5. paginatortemplate is used to specify the buttons that appear in the navigation bar,

6. rows = "10" 10 records are displayed on each page,

7. rowsperpagetemplate = "10, 20, 40, 60" Paging solution,

8. Selection = "# {peoplebean. peopleselected}" Save the selected record to the leleselected attribute,

9. select a single row in selectionmode = "single.

In the main part of the table, the P: column component's sortby is used to specify the sorting field.

Ethnic pagination. The yellow record is the selected record.

Appendix 1: cn_lele.xhtml

<? XML version = '1. 0' encoding = 'gbk'?> <! Doctype composition public "-// W3C // dtd xhtml 1.0 transitional // en" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <UI: composition xmlns: Ui =" http://java.sun.com/jsf/facelets "template = ". /indextemplate.xhtml "xmlns: P =" http://primefaces.org/ui "xmlns: F =" http://java.sun.com/jsf/core "xmlns: c =" http://java.sun.com/jsp/jstl/core "xmlns: H =" http://java.sun.com/jsf/html "> <UI: define name = "content"> 

Appendix 2: lelebean. Java

/** Lelebean. java * 2013-03-31 * Hu */package China; import CN. jadepool. SQL. jade; import CN. jadepool. util. china; import Java. util. arraylist; import Java. util. linkedhashmap; import Java. util. list; import Java. util. map; import javax. faces. application. facesmessage; import javax. faces. bean. managedbean; import javax. faces. bean. sessionscoped; import javax. faces. context. facescontext;/*** implement Chinese national management ** @ author H KM */@ managedbean @ sessionscopedpublic class peoplebean {private map peoplemap = new linkedhashmap (); // national record private map peopleselected = new linkedhashmap (); // Private list of selected ethnic records <map> peoplelist = new arraylist (); // ethnic records list, used to record the query results of the cn_people table private listmapdatamodel lelemodel = new listmapdatamodel (peoplelist, "lele_id"); // ethnic data model, "lele_id" is the primary key name of the cn_people database table/*** creates a new instance Peoplebean */Public peoplebean () {Init ();} public map getpeoplemap () {return peoplemap;} public void setpeoplemap (MAP peoplemap) {This. peoplemap = peoplemap;} public map getpeopleselected () {return peopleselected;} public void setpeopleselected (MAP peopleselected) {This. peopleselected = peopleselected;} public list <map> getpeoplelist () {return peoplelist;} public void setpeoplelist (Li St <map> peoplelist) {This. peoplelist = peoplelist;} public listmapdatamodel getpeoplemodel () {return peoplemodel;} public void setpeoplemodel (listmapdatamodel peoplemodel) {This. peoplemodel = lelemodel;}/*** initialize ethnic records */private void Init () {Jade J = new JADE (); peoplelist = J. query ("select * From cn_people order by people_id"); lelemodel = new listmapdatamodel (peoplelist, "people_id "); J. commit ();} public void guidepeople () {China Cn = New China (); List <map> V = cn. people (); // For (map m: V) {// system. out. println (m); // {Minzu = Han, pinxie = Han, Daima = Ha} //} Jade J = new JADE (); J. delete ("delete from cn_people"); J. insert ("cn_people", V, true); // true, automatically Insert the primary key peoplelist = J. query ("select * From cn_people order by people_id"); peoplemodel = new listmapdatamodel (peoplelist, "people_id" ); J. Commit (); facescontext. getcurrentinstance (). addmessage (null, new facesmessage ("import", "Import ethnic records" + peoplelist. Size () +! "));}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.