struts+hibernate+jsp Page Implementation Paging

Source: Internet
Author: User

DAO Layer Database code:

 PackageCom.hanqi.dao;Importjava.util.ArrayList;Importjava.util.List;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.Transaction;ImportOrg.hibernate.boot.registry.StandardServiceRegistryBuilder;Importorg.hibernate.cfg.Configuration;ImportOrg.hibernate.service.ServiceRegistry;ImportCom.hanqi.utility.USer; Public classUserdao {PrivateConfiguration cfg =NULL ; Privateserviceregistry sr =NULL ; PrivateSessionfactory SF =NULL ; PrivateSession SE =NULL ; Transaction TS=NULL ;  PublicUserdao () {//Initialize HibernateCFG =NewConfiguration (). Configure (); SR=NewStandardserviceregistrybuilder (). Applysettings (Cfg.getproperties ()). build (); }                 Public voidinit () {SF=Cfg.buildsessionfactory (SR); Se=sf.opensession (); TS=se.begintransaction (); }                         Public voiddestory () {ts.commit ();                        Se.close ();         Sf.close (); }                                //Query List         PublicList<user> GetAll (intpage) {List<USer> list =NewArraylist<>() ;                         Init (); //set two lines per page and start PageList = Se.createquery ("from USer"). Setmaxresults (2). Setfirstresult (Page-1) * *). List ();                        Destory (); returnlist; }}

Next is the JSP page, where you pass the page number through the Get method of the A tag

<%@ Pagelanguage= "Java"ContentType= "text/html; charset=utf-8"pageencoding= "UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body><ahref= "AddUSer">Add user</a><BR><BR><ahref= "Selectuser?pages=1" >User List</a></Body></HTML>

Jump to the display screen

1 <% @pageImport= "Com.hanqi.utility.USer"%>2 <% @pageImport= "Java.util.List"%>3 <%@ Pagelanguage= "Java"ContentType= "text/html; charset=utf-8"4 pageencoding= "UTF-8"%>5 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">6 <HTML>7 <Head>8 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">9 <title>Insert Title here</title>Ten </Head> One <Body> A <% -  - List<user>List = (List<USer>) Request.getattribute ("Selectuser"); the  - For (USer us:list) - { -Out.print (US + "<ahref= ' deleteuser?user_id= ' +us.getuser_id () + "'>Delete</a>" ++"<ahref= ' modify?userid= ' +us.getuser_id () + "'>Modify</a>" -+"<BR>") ; + } A int pages = Integer.parseint (Request.getparameter ("pages")); at  - %> -  - <% if(List.size ()==0) {response.getwriter (). Write ("Page out of range"); - response.setheader ("Refresh", "3;url=selectuser?pages=1 ");} else{%> - <%%> in <ahref= "Selectuser?pages=<%=pages-1%>">Previous page</a> - <ahref= "Selectuser?pages=<%=pages+1%>">Next page</a> to <%} %> + </Body> - </HTML>

Struts file Configuration

1 <!--Search All -2         <Actionname= "Selectuser"class= "Com.hanqi.action.USerAction"Method= "Selectuser">3         4                 <resultname= "Fail">/web-inf/pages/fail.jsp</result>5         6                 <resultname= "Success">/web-inf/pages/selectuser.jsp</result>7         8         </Action>

Service Layer Method

Public String Selectuser ()    {        String RTN = "fail";                Try        {//Get native request            httpservletrequest HSR = Servletactioncontext.getrequest ();                        int page = Integer.parseint (Hsr.getparameter ("pages"));                        page = Integer.parseint (Hsr.getparameter ("pagess"));            Method of invoking a query            list<USer>  list = new UserService (). GETALL (page);                        Hsr.setattribute ("pages", page+1);                        Hsr.setattribute ("Selectuser", list);                    RTN = "Success";        } catch (Exception e)        {            e.printstacktrace ();        }                return RTN;    }

When the page number is exceeded, the error is prompted (judging by the list of JSPs) hibernate provides a method that returns List.size () as the number of rows (the last page may be less than the number of rows), or zero without data to determine

struts+hibernate+jsp Page Implementation 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.