Put the two 6-page classes in the util package:
Package com. OA. util;
Public class pager {
Private int totalrows; // The total number of rows.
Private int pagesize = 10; // number of lines displayed per page
Private int currentpage; // the current page number.
Private int totalpages; // the total number of pages.
Private int startrow; // start row of the current page in the database
Public Pager (){
}
Public Pager (INT _ totalrows ){
Totalrows = _ totalrows;
Totalpages = totalrows/pagesize;
Int mod = totalrows % pagesize;
If (mod> 0 ){
Totalpages ++;
}
Currentpage = 1;
Startrow = 0;
}
Public int getstartrow (){
Return startrow;
}
Public int gettotalpages (){
Return totalpages;
}
Public int getcurrentpage (){
Return currentpage;
}
Public int getpagesize (){
Return pagesize;
}
Public void settotalrows (INT totalrows ){
This. totalrows = totalrows;
}
Public void setstartrow (INT startrow ){
This. startrow = startrow;
}
Public void settotalpages (INT totalpages ){
This. totalpages = totalpages;
}
Public void setcurrentpage (INT currentpage ){
This. currentpage = currentpage;
}
Public void setpagesize (INT pagesize ){
This. pagesize = pagesize;
}
Public int gettotalrows (){
Return totalrows;
}
Public void first (){
Currentpage = 1;
Startrow = 0;
}
Public void previous (){
If (currentpage = 1 ){
Return;
}
Currentpage --;
Startrow = (currentpage-1) * pagesize;
}
Public void next (){
If (currentpage <totalpages ){
Currentpage ++;
}
Startrow = (currentpage-1) * pagesize;
}
Public void last (){
Currentpage = totalpages;
Startrow = (currentpage-1) * pagesize;
}
Public void refresh (INT _ currentpage ){
Currentpage = _ currentpage;
If (currentpage> totalpages ){
Last ();
}
}
}
Pagerhelp class:
Package com. OA. util;
Import javax. servlet. http. httpservletrequest;
Public class pagerhelp {
Public static pager getpager (httpservletrequest, int totalrows ){
// Defines the pager object for uploading to the page
Pager pager = new pager (totalrows );
// Obtain the current page number from the request object
String currentpage = httpservletrequest. getparameter ("currentpage ");
// If the current page number is blank, it indicates that this page is queried for the first time.
// If not empty, refresh the pager object and enter the current page number and other information.
If (currentpage! = NULL ){
Pager. Refresh (integer. parseint (currentpage ));
}
// Obtain the method of the current execution, home page, Previous Page, next page, and last page.
String pagermethod = httpservletrequest. getparameter ("pagemethod ");
If (pagermethod! = NULL ){
If (pagermethod. Equals ("first ")){
Pager. First ();
} Else if (pagermethod. Equals ("previous ")){
Pager. Previous ();
} Else if (pagermethod. Equals ("Next ")){
Pager. Next ();
} Else if (pagermethod. Equals ("last ")){
Pager. Last ();
}
}
Return pager;
}
}
7. The action in struts undoubtedly belongs to the C level, which can also be called the V layer:
Package com. OA. View. Struts. Action;
Import java. util. arraylist;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import org. Apache. Struts. action. Action;
Import org. Apache. Struts. Action. actionform;
Import org. Apache. Struts. Action. actionforward;
Import org. Apache. Struts. Action. actionmapping;
Import org. Apache. Struts. Actions. dispatchaction;
Import org. Apache. Struts. validator. dynavalidatorform;
Import com. OA. model. Service. iuserservice;
Import com. OA. util. Pager;
Import com. OA. util. pagerhelp;
Public class showalluseraction extends action {
Private iuserservice userservice;
Public actionforward execute (
Actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response ){
Arraylist list = new arraylist ();
Int totalrows; // The total number of record rows.
Totalrows = userservice. Counter ();
Pager page = new pager ();
Page = pagerhelp. getpager (request, totalrows );
List = (arraylist) userservice. findwithpage (page. getpagesize (), page. getstartrow ());
Request. setattribute ("list", list );
Request. setattribute ("page", page );
Return Mapping. findforward ("show_ OK ");
}
Public iuserservice getuserservice (){
Return userservice;
}
Public void setuserservice (iuserservice userservice ){
This. userservice = userservice;
}
}
8. Needless to say, the core configuration file of spring is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<Bean id = "datasource" class = "org. Apache. commons. DBCP. basicdatasource">
<Property name = "driverclassname">
<Value> com. MySQL. JDBC. Driver </value>
</Property>
<Property name = "url">
<Value> JDBC: mysql: // localhost: 3307/test </value>
</Property>
<Property name = "username">
<Value> root </value>
</Property>
<Property name = "password">
<Value> root </value>
</Property>
</Bean>
<Bean id = "sessionfactory" class = "org. springframework. Orm. hibernate3.localsessionfactorybean">
<Property name = "datasource">
<Ref bean = "datasource"/>
</Property>
<Property name = "hibernateproperties">
<Props>
<Prop key = "hibernate. dialect"> org. hibernate. dialect. mysqldialect </prop>
<Prop key = "hibernate. show_ SQL"> true </prop>
</Props>
</Property>
<Property name = "mappingresources">
<List>
<Value> COM/OA/data/bo/user. HBM. xml </value>
</List>
</Property>
</Bean>
<Bean id = "userdao" class = "com. OA. Data. Dao. impl. userdao">
<Property name = "sessionfactory">
<Ref bean = "sessionfactory"/>
</Property>
</Bean>
<! -- Create a transaction management class -->
<Bean id = "transactionmanager" class = "org. springframework. Orm. hibernate3.hibernatetransactionmanager">
<Property name = "sessionfactory">
<Ref local = "sessionfactory"/>
</Property>
</Bean>
<! -- Create a user transaction proxy class -->
<Bean id = "userdaoproxy" class = "org. springframework. transaction. Interceptor. transactionproxyfactorybean">
<Property name = "transactionmanager">
<Ref bean = "transactionmanager"/>
</Property>
<Property name = "target">
<Ref local = "userdao"/>
</Property>
<Property name = "transactionattributes">
<Props>
<Prop key = "insert *"> propagation_required </prop>
<Prop key = "Update *"> propagation_required, readonly </prop>
<Prop key = "*"> propagation_required, readonly </prop>
</Props>
</Property>
</Bean>
</Beans>
9 The service context also writes an XML file, servicecontext. XML, which can also be written in the application. xml file.
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<Bean id = "userservice" class = "com. OA. model. Service. impl. userservice">
<Property name = "userdao">
<Ref bean = "userdaoproxy"/>
</Property>
</Bean>
</Beans>
10 The viewcontext. xml file modifies and configures all actions, because each action has a service class object to process the business,
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<Bean name = "/login" class = "com. OA. View. Struts. Action. useraction" Singleton = "false">
<Property name = "userservice">
<Ref bean = "userservice"/>
</Property>
</Bean>
<Bean name = "/login_test" class = "com. OA. View. Struts. Action. login2action" Singleton = "false">
<Property name = "userservice">
<Ref bean = "userservice"/>
</Property>
</Bean>
<Bean name = "/test" class = "com. OA. View. Struts. Action. testaction" Singleton = "false">
<Property name = "userservice">
<Ref bean = "userservice"/>
</Property>
</Bean>
<Bean name = "/showalluser" class = "com. OA. View. Struts. Action. showalluseraction" Singleton = "false">
<Property name = "userservice">
<Ref bean = "userservice"/>
</Property>
</Bean>
</Beans>
11 The Struts-config file may be changed a lot after spring is used:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype Struts-config public "-// Apache Software Foundation // DTD struts configuration 1.2 //" http://struts.apache.org/dtds/struts-config_1_2.dtd ">
<Struts-config>
<Data-sources/>
<Form-beans>
<Form-bean name = "loginform" type = "org. Apache. Struts. validator. dynavalidatorform">
<Form-property name = "password" type = "Java. Lang. String"/>
<Form-property name = "username" type = "Java. Lang. String"/>
<Form-property name = "password1" type = "Java. Lang. String"/>
<Form-property name = "username1" type = "Java. Lang. String"/>
</Form-bean>
<Form-bean name = "testform" type = "org. Apache. Struts. validator. dynavalidatorform">
<Form-property name = "username2" type = "Java. Lang. String"/>
<Form-property name = "password2" type = "Java. Lang. String"/>
</Form-bean>
</Form-beans>
<Global-exceptions/>
<Global-forwards>
<Forward name = "gook" Path = "/OK. jsp"/>
</Global-forwards>
<Action-mappings>
<Action attribute = "loginform"
Input = "/login. jsp"
Name = "loginform"
Parameter = "method"
Path = "/login"
Validate = "true"
Scope = "request"
Type = "org. springframework. Web. Struts. delegatingactionproxy"/>
<Action
Attribute = "loginform"
Input = "/login. jsp"
Name = "loginform"
Parameter = "method"
Path = "/login_test"
Scope = "request"
Validate = "true"
Type = "org. springframework. Web. Struts. delegatingactionproxy"/>
<Action
Attribute = "testform"
Input = "/login. jsp"
Name = "testform"
Path = "/test"
Scope = "request"
Type = "com. OA. View. Struts. Action. testaction"/>
<Action
Path = "/showalluser"
Scope = "request"
Type = "org. springframework. Web. Struts. delegatingactionproxy">
<Forward name = "show_ OK" Path = "/OK. jsp"/>
</Action>
<Action Path = "/showalluser2" type = "com. OA. View. Struts. Action. showalluser2action">
<Forward name = "OK" Path = "/OK. jsp"/>
</Action>
</Action-mappings>
<Message-resources parameter = "com. OA. View. Struts. applicationresources"/>
<Plug-in classname = "org. Apache. Struts. Tiles. tilesplugin">
<Set-Property = "definitions-parser-validate" value = "true"/>
<Set-Property = "definitions-config" value = "/WEB-INF/tiles-config.xml"/>
</Plug-in>
<Plug-in classname = "org. Apache. Struts. validator. validatorplugin">
<Set-Property = "pathnames" value = "/WEB-INF/validator-rules.xml,/WEB-INF/validation. xml"/>
</Plug-in>
<Plug-in classname = "org. springframework. Web. Struts. contextloaderplugin">
<Set-Property = "contextconfiglocation" value = "/WEB-INF/applicationcontext. XML,/WEB-INF/servicecontext. XML,/WEB-INF/viewcontext. xml"/>
</Plug-in>
</Struts-config>
12 The last part is the page JSP file:
Login. jsp file:
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-bean" prefix = "Bean" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-html" prefix = "html" %>
<HTML>
<Head>
<Title> login </title>
</Head>
<Body>
<HTML: errors/> <br>
<HTML: Form Action = "/login_test">
<Bean: Message key = "user. Name"/>:< HTML: Text property = "username"/> <br/>
<Bean: Message key = "user. Password"/>:< HTML: Password property = "password"/> <br/>
<Input type = "Submit" value = "<Bean: Message key =" page. command. submit "/>" onclick = "document. forms [0]. action = 'login. do? Method = loginaction'; document. Forms [0]. Submit (); ">
</Html: Form>
<HR>
<A name = "A" href = "/showalluser. do? Pagemethod = first & currentpage = 0 "> show _ fcksavedurl =" "/showalluser. do? Pagemethod = first & currentpage = 0 "> show" _ fcksavedurl = ""/showalluser. do? Pagemethod = first & currentpage = 0 "> show" all user </a>
<A name = "A" href = "/showalluser2.do"> show all user </a>
<Br>
</Body>
</Html>
OK. jsp mainly displays the page display:
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-bean" prefix = "Bean" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-html" prefix = "html" %>
<% @ Page import = "Java. util. *" %>
<% @ Page import = "com. OA. Data. Bo. User" %>
<% @ Page import = "com. OA. util. pager" %>
<HTML>
<Head>
<Title> OK !!! </Title>
</Head>
<Body>
<Bean: Message key = "user. login. text2"/> & nbsp;
<Bean: Message key = "user. login. text4"/>
<HR>
<%
Arraylist list = new arraylist ();
User user = new user ();
If (request. getattribute ("list ")! = NULL)
{
List = (arraylist) request. getattribute ("list ");
For (INT I = 0; I <list. Size (); I ++)
{
User = (User) list. Get (I );
%>
<Table width = "100%" border = "1" cellspacing = "0" cellpadding = "0">
<Tr>
<TD Height = "25"> <% = user. GetUserName () %> </TD>
<TD Height = "25"> <% = user. getuserpwd () %> </TD>
</Tr>
</Table>
<%
}
}
%>
<%
If (request. getattribute ("page ")! = NULL)
{
Pager P = (PAGER) request. getattribute ("page ");
%>
<Table width = "100%" border = "1" cellspacing = "0" cellpadding = "0">
<Tr>
<TD Height = "25">
Page <% = P. getcurrentpage () %> total page <% = P. gettotalpages () %>
<A href = "/showalluser. do? Pagemethod = first & currentpage = 1%> "> homepage </a>
<A href = "/showalluser. do? Pagemethod = next & currentpage = <% = P. getcurrentpage () %> "> next page </a>
<A href = "/showalluser. do? Pagemethod = previous & currentpage = <% = P. getcurrentpage () %> "> previous page </a>
<A href = "/showalluser. do? Pagemethod = last & currentpage = <% = P. getcurrentpage () %> "> last page </a>
</TD>
</Tr>
</Table>
<%
}
%>
</Body>
</Html>