Device Resource Management System-pagination

Source: Internet
Author: User

Device Resource Management System-pagination

  • Paging design

  Userindex. jsp

Userlist. jsp

Ajax framework

Requirements:

1. Two pages (xxxindex. jsp, xxxlist. jsp) are required)

2. Two form objects, form1 and form2, are used in a JSP page.

3. The content of form2 on the first JSP page of another JSP.

Principle:

The submission process is essentially the submission of form1 parameters. The fom1 parameters are used to query background data, return results, and the results are stored in xxxlist. JSP display, with xxxlist. replace xxxindex with JSP content. content of form2 in JSP

  • Some code

1. Import two java files, pagebean and pageinfo

Code in pagebean:

Private int pageno; // current page

Private Boolean firstpage; // whether the current page is the first page

Private Boolean lastpage; // whether the current page is the last page

Private int sumpage; // the total number of pages to store.

Private int pagesize; // The number of records displayed on the current page

Private int totalresult; // The total number of records to store

2. import one JS file, which is page. js and put it under the script folder.

3. Modify userindx. jsp

* Import the required JS File

<Script language = "JavaScript" src = "$ {pagecontext. Request. contextpath}/script/function. js"> </SCRIPT>

<Script language = "JavaScript" src = "$ {pagecontext. Request. contextpath}/script/pub. js"> </SCRIPT>

<Script language = "JavaScript" src = "$ {pagecontext. Request. contextpath}/script/validate. js"> </SCRIPT>

<Script language = "JavaScript" src = "$ {pagecontext. Request. contextpath}/script/page. js"> </SCRIPT>

* In form1, add three hidden fields for passing parameters.

* Modify the onclick event of the query button

<Input style = "font-size: 12px; color: black; Height = 20; width = 80 "id =" bt_add "type =" button "value =" query "name =" bt_find"

Onclick = "gotoquery ('System/elecuseraction_home.do ')"> & nbsp;

* Add pagination in form2

<! -- Ly add start -->

<Tr>

<TD width = "100%" Height = "1">

<Table border = "0" width = "100%" cellspacing = "0" cellpadding = "0">

<% Pagebean = (pagebean) request. getattribute ("page"); %>

<Tr>

<TD width = "15%" align = "Left"> total number of records: <% = pagebean. gettotalresult () %> entries </TD>

& Lt; TD width = "14%" align = "right" & gt; & lt;/TD & gt;

<% IF (pagebean. getfirstpage () {%>

<TD width = "8%" align = "center"> homepage & nbsp; | </TD>

<TD width = "10%" align = "center"> Previous Page & nbsp; | </TD>

<%} Else {%>

<TD width = "8%" align = "center"> <u> <a href = "#" onclick = "gotopage ('System/elecuseraction_home.do ', 'start ') "> homepage & nbsp; | </a> </u> </TD>

<TD width = "10%" align = "center"> <u> <a href = "#" onclick = "gotopage ('System/elecuseraction_home.do ', 'prev ') "> Previous Page & nbsp; | </a> </u> </TD>

<% }%>

<% IF (pagebean. getlastpage () {%>

<TD width = "10%" align = "center"> next page & nbsp; | </TD>

<TD width = "8%" align = "center"> last page </TD>

<%} Else {%>

<TD width = "10%" align = "center"> <u> <a href = "#" onclick = "gotopage ('System/elecuseraction_home.do ', 'Next ') "> next page & nbsp; | </a> </u> </TD>

<TD width = "8%" align = "center"> <u> <a href = "#" onclick = "gotopage ('System/elecuseraction_home.do ', 'end ') "> last page </a> </u> </TD>

<% }%>

<TD width = "6%" align = "center"> page <% = pagebean. getpageno () %> </TD>

<TD width = "6%" align = "center"> total pages <% = pagebean. getsumpage () %> </TD>

<TD width = "21%" align = "right"> to page <input size = "1" type = "text" name = "gopage">

<U> <a href = "#" onclick = "gotopage ('System/elecuseraction_home.do ', 'Go')"> OK </a> </u> </TD>

<TD> <input type = "hidden" name = "pageno" value = "<% = pagebean. getpageno () %>"> </TD>

<TD> <input type = "hidden" name = "prevpageno" value = "<% = (pagebean. getpageno ()-1) %>"> </TD>

<TD> <input type = "hidden" name = "nextpageno" value = "<% = (pagebean. getpageno () + 1) %>"> </TD>

<TD> <input type = "hidden" name = "sumpage" value = "<% = pagebean. getsumpage () %>"> </TD>

<TD> <input type = "hidden" name = "pagesize" value = ""> </TD>

</Tr>

</Table>

</TD>

</Tr>

<! -- Ly add end -->

4. Extract the content of form2 in userindex. jsp and create userlist. jsp

5. In the Struts. xml configuration file, add:

<! -- 2011-12-12, add page forwarding by page -->

<Result name = "list">

/WEB-INF/page/system/userlist. jsp

</Result>

6. modify it in elecuseraction. java.

// Add pagination in 2011-12-12 to pass the request object

List <elecuserform> List = elecuserservice. findelecuserlistbycondition (elecuserform, request );

Request. setattribute ("userlist", list );

String initflag = request. getparameter ("initflag ");

If (initflag! = NULL & initflag. Equals ("1 ")){

Return "list ";

}

7. Add in elecuserserviceimpl. Java

//, Add paging operation

// List <elecuser> List = elecuserdao. findcollectionbyconditionnopage (hqlwhere, Params, orderby );

Pageinfo = new pageinfo (request );

Operation logic:

Use the currentpageno attribute to indicate the current page number, which is the first page by default.

The pagesize attribute indicates the number of records displayed on the current page.

Use the req attribute to store the request object passed on the page

List <elecuser> List = elecuserdao. findcollectionbyconditionwithpage (hqlwhere, Params, orderby, pageinfo );

Request. setattribute ("page", pageinfo. getpagebean ());

8. modify it in commondaoimpl. java.

List <t> List = (list <t> export this.gethibernatetemplate(cmd.exe cute (New hibernatecallback (){

Public object doinhibernate (session)

Throws hibernateexception, sqlexception {

Query query = session. createquery (finalhql );

Setparams (query, Params );

Pageinfo. settotalresult (query. List (). Size ());

Operation logic:

Use the totalresult attribute to store the total number of records in the query list

Use the totalpage attribute to store the total number of pages (use the total number of records to calculate the number of records displayed on each page)

Query. setfirstresult (pageinfo. getbeginresult ());

Operation logic:

The beginresult attribute indicates the number of results displayed in the result list, that is, the first data displayed on each page.

Instantiate pagebean and add attributes

Query. setmaxresults (pageinfo. getpagesize ());

Return query. List ();

}

});

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.