Using Struts2 and jquery Easyui to implement a simple crud system (iv)--Data paging

Source: Internet
Author: User

After completing the multi-select Delete function, the next step is to do the paging function. The previous paging was a troublesome issue. And when the amount of data is huge, it is obviously slower to take these elements out directly, so it is efficient to take a certain interval of data.



Before you write the page, the interface of course is self-made, after using the Ejui, really convenient a lot. It's convenient to what extent.

<table id= "DG" title= "My Users" class= "Easyui-datagrid" style= "width:700px"; height:500px "url=" List_ej "toolbar=" #toolbar "rownumbers=" true "fitcolumns=" true "  ><thead><tr> <!--This notation is also possible <th field= "id" width= ">id</th><th field=" name "Width=" >name</th>< th field= "password" width= ">password</th>--><th field=" ck "checkbox=" true "></th><th data-options= "field: ' ID ', Width: ' 200px '" >id</th><th data-options= "field: ' Name ', Width: ' 200px '" > name</th><th data-options= "field: ' Password ', Width: ' 200px ', align: ' right '" >password</th></tr ></thead></table>

Just add it to the property:

Pagination= "true" pagenumber = "1"
Pagination is displayed at the bottom of the paging toolbar, PageNumber is the original display of the number of pages, the first few pages.


The difficulty is how the background data is passed. It is all displayed when there is no relationship with the background to correctly handle the number of pages and data displayed per page, but when you select the number of pages, Ejui will correctly help you jump to the page. But this is obviously not what we want.


The back is stuck on the train of thought, how do I get the data from the paging column to the action?

The workaround is that the rows and page properties, respectively, correspond to the number of data bars and current pages displayed on one page, just declare the global variable in action and write out the corresponding get and set methods.

public class Controlaction extends actionsupport{private int rows;private int page;public int getRows () {return rows;} public void setrows (int rows) {this.rows = rows;} public int GetPage () {return page;} public void setpage (int page) {this.page = page;}

Easy to handle when you get it. We can write an encapsulated method that will get the number of pages and bars to the database operation and convert to JSON string.

Public String List_ej () {Actioncontext ctx=actioncontext.getcontext (); String result= ""; result= pageutil.getpage (page, rows); try {//servletactioncontext.getresponse (). Getwriter (). println (Jsonarray.fromobject (list)); Ctx.put ("json", result);} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return "Success";}

Pageutil.java

Package Util;import Java.sql.connection;import Java.sql.resultset;import java.sql.sqlexception;import Java.sql.statement;import Java.util.arraylist;import Java.util.list;import Net.sf.json.jsonarray;import Net.sf.json.jsonobject;public class Pageutil {public static string getpage (int pagenumber,int rows) {string result = ""; T count = 0;int number = 0; Connection C = Connecttosql.getconn (); Statement st = CONNECTTOSQL.GETST (c); list<user> list = new arraylist<user> (); try {ResultSet countrs = st.executequery ("SELECT count (*) from User") ; Countrs.next ();////system.out.println (Countrs.getint (1)); count = Countrs.getint (1);} catch (SQLException E1) {e1.printstacktrace ();} if (count%rows==0) {number =count/rows;} Else{number = count/rows+1;} SYSTEM.OUT.PRINTLN (number); try {int index = 0;IF (pagenumber-1>0) {index = (pagenumber-1) *rows-1;} ResultSet rs = st.executequery ("SELECT * from User Limit" +index+ "," +rows); and while (Rs.next ()) {User U = new user (); U.setid (rs . GETINT ("userid")); U.setnaMe (rs.getstring ("username")); U.setpassword (rs.getstring ("password")); List.add (U);}} catch (SQLException e) {e.printstacktrace ();} This lists the list as an array, output in JSON format is only output listlist<user> o = jsonarray.fromobject (list);//No string concatenation//string result = "{\" total\ ":" +count+ ", \" rows\ ":" +s+ "}"; the whole object to JSON format object has two properties total and rows property and rows inside is an array jsonobject Jo = new Jsonobject (); Jo.put ("Total", count); Jo.put ("Rows", list); result = Jo.tostring (); return result;}}

Ejui and Struts2 use where, ejui only need to accept the total number of data and the current page of data is the rows: The following JSON data, it will be automatically populated into the DataGrid.

{"Total": 5, "Rows": [{"id": 1277, "name": "DF2", "Password": "123"},{"id": 1278, "name": "45ty", "Password": "123"},{"id" : 1279, "name": "Sdfy", "Password": "123"},{"id": 1280x720, "name": "345356", "Password": "P"},{"id": 1281, "name": "Werwer", " Password ":" Twer "}]}

And once Ejui's rows and page change,

Once you select page and rows, the data is passed through Ajax, and then the DataGrid is partially refreshed.

It has the ability to pass parameters to the URL, and struts itself can take both of these values after it is defined by the get and set methods, so it is convenient.

Also very powerful, beautiful interface.


This simple crud system function is largely complete, but obviously not familiar with JSON and Easyui. This is still a lot to practice.

Project GitHub Address: Https://github.com/iaiti/EasyUI-Struts2.git


Separate the Java code from the JSP page code and create a new branch version2.0.

Using Struts2 and jquery Easyui to implement a simple crud system (iv)--Data paging

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.