How to make paging processing in Struts

xiaoxiao2021-03-06  54

How to make paging processing in Struts

Recently, Struts projects have recently involved in paging processing. Here is how to talk about how to talk in Struts in connection with the project.

You can choose to read all data at a time, then you will not read the data each time you paid. Another way you can read other data each time you read only the data you need to display each time. Two methods will be briefly introduced.

First introduce the first, data read in one. Suitable for small data, the first load page will be slower.

Item Disposal: The page that needs paging in the project is the res0011 page, which corresponds to the res0011ac.java (Actionform class), a total of three files in res0011.jsp.

1. The following configuration should be made in the struts-config.xml file:

TYPE = "com.apps.res.res0011ac"

Name = "res0011" scope = "session"

Validate = "true" input = "/ piece / comerror.jsp">

Explanation: res0011.page is actually res0011.jsp, and the project is different from the TILES feature of Struts, so there is some different pages. It is not necessary to study here, think he is a page. Other is not related to the project needs and paging, you can do not have to study. Key To note that scope must be set to session because our data is saved in the session. If you are set to Request, then the data you paid will be lost. Second, related variables in Actionform

In paging, we need the following variables:

Starting record: BeginRecordno

Number of records shown per page: RecordDivno

Total number of records: allrecordno

These variables are established in the ActionForm and generate the corresponding GET, SET method. Of course, the relevant content in the database must have a corresponding variable. For example, a User information is put into a HashMap, KEY is UserId, and Value is UserName. Of course, you can put anything you want, or DataBean.

The code fragment of the ActionFrom is as follows:

Private string recorddivno;

PRIVATE STRING BEGINRECORDNO;

PRIVATE STRING AllRecordno;

/ **

* @Param String

* /

Public void setallRecordno (String string) {

Allrecordno = String;

}

/ **

* @Return

* /

Public string getRecordno () {

Return allRecordno;

}

/ **

* @Return

* /

Public string getBeginRecordno () {

Return BeginRecordno;

}

/ **

* @Param String

* /

Public void setBeginRecordno (String string) {

BeginRecordno = String;

}

/ **

* @Return

* /

Public string getRecorddivno () {

Return Recorddivno;

}

/ **

* @Param String

* /

Public void setRecorddivno (String String) {

Recorddivno = String;

}

Third, how to use the variables described in "II" in JSP

First take the corresponding variable from the Form in the JSP

<%

Res0011af formres0011 = (res0011af) session.getattribute ("res0011");

Int Divnum;

String Recorddivno;

String beginRecordno;

Int beginRecordnoint;

Try {

Divnum = integer.parseint (Formres0011.getdivnum ());

Recorddivno = formres0011.GetRecorddivno ();

BeginRecordno = Formres0011.GetBeginRecordno ();

IF (Divnum == 0) {

Divnum = integer.parseint (Formres0011.GetallRecordno ());

}

}

Catch (Exception E) {

Divnum = integer.parseint (Formres0011.GetallRecordno ()); RecordDivno = formres0011.GetallRecordno ();

BeginRecordno = "1";

}

BeginRecordnoint = Integer.Parseint (BeginRecordno) - 1;

%>

Then use in the Struts tag:

"length =" <% = Recordivno%> "INDEXID =" indexout ">

<% = indexout.intValue () 1%>.

. . . .

Explanation: Properties Revcontents can think that it is the content in the database you define. For example, a USER's ArrayList is like the above. Where the devday property is UserId, the devName property is UserName. The code given is to take out data directly from the session, of course, more professional methods are:

Then use BeginRecordno.

Below is a paged LINK code snippet:

Bean: The parameters in Message are my special needs - how much each page is displayed according to a configuration file. So there is a parameter. If you are not like this, your MSG is likely to be "Previous", "Next". Attribute prepage and nextpage have no special things you can do if you can distinguish it. This value can be written in the ACTIONFORM's reset method. For example, the previous page is P, the next page is n. Someone asked if there is no previous page or the post page? In fact, this is very simple, you can add a control variable in Actionform. The final code is like this:

->

At this point, the code of JSP is almost.

Fourth, the logic process in the action

First, the data is read out when the page is initially chemically, and the corresponding variable start display page is typically starting from 0. Then there are some other initialization work. Then the corresponding page of the page will add the starting page code to the number of data numbers per page per page accordingly, and the corresponding data correctness check.

Code segment:

String pageevt = Request.getParameter ("pageevt");

/ / Remember the ID of the LINK we defined in JSP, right, is PageevT

IF (pageevt.equals ("p")) {

// Respond to the previous page

INT Ibeginno = Integer.Parseint (res0011form.getbeginRecordno ());

INT IRecorddivno = Integer.Parseint (res0011form.getRecorddivno ());

Res0011form.SetBeginRecordno (Ibeginno - IRecordDivno ");

} else IF (pageevt.equals ("n")) {

// Respond to the next page

INT Ibeginno = Integer.Parseint (res0011form.getbeginRecordno ());

INT IRecorddivno = Integer.Parseint (res0011form.getRecorddivno ());

Res0011form.SetBeginRecordno (Ibeginno IRecordDivno ");

} else {

// Picture initialization, acquire all data

Res0011form.setBeginRecordno ("1");

}

Return mapping.findforward (Target);

The second method, only read only the data displayed on the page at a time. Access to large data volume is acceptable.

It is understood that the first method, the second method is not difficult to achieve. The structure in the ActionForm is similar. Just the use of the JSP does not require the OFFSET attribute. How much is the data display. Determine from BeginRecordno to start displaying data, each time you read data from the database.

Flexible use you can combine two programs, such as only 500 data each time and then display 20 per page. This reduces the number of access to the database to improve the user's access speed for massive data.

Postscript: The first time I write technical articles, and everyone will be in Haihan. Although this article writes in the form of a project experience but welcomes everyone's discussion and criticism. Due to copyright, all code cannot be made, everyone forgive me. The article only explores and studies around technical issues, and ask for the original code, please also avoid excuse. Finally, thank you.

转载请注明原文地址:https://www.9cbs.com/read-115131.html

New Post(0)