您的位置:首页 > 编程语言 > Java开发

Struts和Hibernate实现的查询分页显示

2008-06-26 16:37 591 查看
Struts和Hibernate实现的查询数据库中book后分页显示的源代码(BookBean类的代码不再显示):

业务实现过程:

1.选择按什么查询,包括书名、作者、出版社,然后输入要查询的内容,提交查询。

2.FormBean将提交的数据封装传递给Action进行处理。

3.Action根据所得到的数据,调用组装DAO的业务逻辑,进行查询。



PagerBean类的代码如下:

publicclassPager{

privateintcurrentPage=1;

privateintpageSize=10;

privateinttotalPages=0;

privateinttotalRows=0;

privateintpageStartRow=0;

privateintpageEndRow=0;

privatebooleanhasPrevious=false;

privatebooleanhasNext=false;

@SuppressWarnings("unchecked")

privateListbookList;

@SuppressWarnings("unchecked")

publicPager(Listlist){

this.currentPage=1;

this.bookList=list;

this.totalRows=list.size();

this.hasPrevious=false;

if(totalRows%pageSize==0)

totalPages=totalRows/pageSize;

else

totalPages=totalRows/pageSize+1;

if(currentPage>=totalPages)

hasNext=false;

else

hasNext=true;

if(currentPage==1)

hasPrevious=false;

else

hasPrevious=true;

if(totalRows<pageSize){

this.pageStartRow=0;

this.pageEndRow=totalRows-1;

}

else{

this.pageStartRow=0;

this.pageEndRow=pageSize-1;

}

}

publicintgetCurrentPage(){

returncurrentPage;

}

publicvoidsetCurrentPage(intcurrentPage){

this.currentPage=currentPage;

}

publicintgetPageSize(){

returnpageSize;

}

publicvoidsetPageSize(intpageSize){

this.pageSize=pageSize;

}

publicintgetTotalPages(){

returntotalPages;

}

publicvoidsetTotalPages(inttotalPages){

this.totalPages=totalPages;

}

publicintgetTotalRows(){

returntotalRows;

}

publicvoidsetTotalRows(inttotalRows){

this.totalRows=totalRows;

}

publicintgetPageStartRow(){

returnpageStartRow;

}

publicvoidsetPageStartRow(intpageStartRow){

this.pageStartRow=pageStartRow;

}

publicintgetPageEndRow(){

returnpageEndRow;

}

publicvoidsetPageEndRow(intpageEndRow){

this.pageEndRow=pageEndRow;

}

publicbooleanisHasPrevious(){

returnhasPrevious;

}

publicvoidsetHasPrevious(booleanhasPrevious){

this.hasPrevious=hasPrevious;

}

publicbooleanisHasNext(){

returnhasNext;

}

publicvoidsetHasNext(booleanhasNext){

this.hasNext=hasNext;

}

@SuppressWarnings("unchecked")

publicListgetBookList(){

returnbookList;

}

@SuppressWarnings("unchecked")

publicvoidsetBookList(ListbookList){

this.bookList=bookList;

}

@SuppressWarnings("unchecked")

publicListgetBooks(){

pageStartRow=(currentPage-1)*pageSize;

if(hasNext==false)

pageEndRow=totalRows-1;

else

pageEndRow=pageStartRow+pageSize-1;

Listbooks=newArrayList();

for(inti=pageStartRow;i<=pageEndRow;i++){

books.add((Bookinfo)bookList.get(i));

}

returnbooks;

}

@SuppressWarnings("unchecked")

publicListgetNextPage(){

currentPage=currentPage+1;

if(currentPage>=1&¤tPage<totalPages)

hasNext=true;

else

hasNext=false;

if(currentPage>1&¤tPage<=totalPages)

hasPrevious=true;

else

hasPrevious=false;

Listbooks=getBooks();

returnbooks;
}

@SuppressWarnings("unchecked")

publicListgetPreviousPage(){

currentPage=currentPage-1;

if(currentPage>=1&¤tPage<totalPages)

hasNext=true;

else

hasNext=false;

if(currentPage>1&¤tPage<=totalPages)

hasPrevious=true;

else

hasPrevious=false;

Listbooks=getBooks();

returnbooks;

}
}

searchForm类的源代码如下:

@SuppressWarnings("unchecked")

publicclasssearchFormextendsActionForm{

privatestaticfinallongserialVersionUID=1L;

privateStringsearchItem;

privateStringsearchContent;

publicStringgetSearchItem(){

returnsearchItem;

}

publicvoidsetSearchItem(StringsearchItem){

this.searchItem=searchItem;

}

publicStringgetSearchContent(){

returnsearchContent;

}

publicvoidsetSearchContent(StringsearchContent){

this.searchContent=searchContent;

}

}


searchAction类的源代码如下:

publicclasssearchActionextendsAction{

Pagerpager;

@SuppressWarnings("unchecked")

Listlist;

@SuppressWarnings("unchecked")

publicActionForwardexecute(ActionMappingmapping,ActionFormform,

HttpServletRequestrequest,HttpServletResponseresponse)

throwsException{

Stringaction=request.getParameter("action");

if(action==null||action.equals("null")){

list=selectBooks(form);

if(list.size()<0||list==null)

returnmapping.findForward("fail");

pager=newPager(list);

Listbooks=pager.getBooks();

request.setAttribute("ps",pager.getPageStartRow());

request.setAttribute("result",books);

request.setAttribute("pager",pager);

}else{

if(action=="prepage"||action.equals("prepage")){

Listbooks=pager.getPreviousPage();

request.setAttribute("ps",pager.getPageStartRow());

request.setAttribute("result",books);

request.setAttribute("pager",pager);

}

if(action=="nextpage"||action.equals("nextpage")){

Listbooks=pager.getNextPage();

request.setAttribute("ps",pager.getPageStartRow());

request.setAttribute("result",books);

request.setAttribute("pager",pager);

}

}

returnmapping.findForward("success");

}

@SuppressWarnings("unchecked")

publicListselectBooks(ActionFormform){

searchFormsearchform=(searchForm)form;

Stringitem=searchform.getSearchItem();

Stringcontent=searchform.getSearchContent();

searchDAOdao=newsearchDAO();

Listlist=null;

if(item.equals("bookname"))

list=dao.getBookBybookName(content);

if(item.equals("author"))

list=dao.getBookByAuthor(content);

if(item.equals("press"))

list=dao.getBookByPress(content);

returnlist;

}

}


struts-config.xml文件如下:

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEstruts-configPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.1//EN""http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

<!--===========FormBeanandActionapplicatedtosearchbooks==========-->

<form-beans>

<form-beanname="searchForm"type="com.ustc.actionform.guix.searchForm">

</form-bean>

</form-beans>

<action-mappings>

<actionpath="/searchAction"type="com.ustc.action.guix.searchAction"name="searchForm"scope="request">

<forwardname="success"path="/searchresult.jsp"></forward>

<forwardname="fail"path="/searchfail.jsp"></forward>

</action>

</action-mappings>

<!--===========FormBeanandActionapplicatedtosearchbooks==========-->

<message-resourcesparameter="com.ustc.ApplicationResource"></message-resources>

</struts-config>


web.xml文件如下:


<?xmlversion="1.0"encoding="UTF-8"?>

<web-appxmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.4"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--===============================字符编码转换过滤器===================================-->

<filter>

<filter-name>setcharacterencoding</filter-name>

<filter-class>com.ustc.action.guix.SetCharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>gb2312</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>setcharacterencoding</filter-name>

<url-pattern>*.do</url-pattern>

</filter-mapping>

<!--===============================字符编码转换过滤器===================================-->

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

<init-param>

<param-name>debug</param-name>

<param-value>3</param-value>

</init-param>

<init-param>

<param-name>detail</param-name>

<param-value>3</param-value>

</init-param>

<load-on-startup>0</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>/index.jsp</welcome-file>

</welcome-file-list>

</web-app>



SetCharacterEncodingFilter类源代码如下:

publicclassSetCharacterEncodingFilterimplementsFilter{

protectedStringencoding=null;

protectedFilterConfigfilterConfig=null;

protectedbooleanignore=true;

@Override

publicvoiddestroy(){

//撤销所有全局变量的值,置为NULL

this.encoding=null;

this.filterConfig=null;

}

@Override

publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,

FilterChainchain)throwsIOException,ServletException{

//从web.xml中得到过滤器的字符串编码

if(ignore||(request.getCharacterEncoding()==null)){

Stringencoding=selectEncoding(request);

if(encoding!=null)

request.setCharacterEncoding(encoding);//设置字符串编码

}

chain.doFilter(request,response);

}

@Override

publicvoidinit(FilterConfigfilterConfig)throwsServletException{

//初始化方法判断web.xml中过滤器是否传入ignore参数,当ignore参数为yes或true时忽略本过滤器

this.filterConfig=filterConfig;

this.encoding=filterConfig.getInitParameter("encoding");

Stringvalue=filterConfig.getInitParameter("ignore");

if(value==null)

this.ignore=true;

elseif(value.equalsIgnoreCase("true"))

this.ignore=true;

elseif(value.equalsIgnoreCase("yes"))

this.ignore=true;

else

this.ignore=false;

}

publicStringselectEncoding(ServletRequestrequest){

return(this.encoding);

}

}


searchresult.jsp源代码如下:


<%@pagecontentType="text/html;charset=gb2312"language="java"%>

<%@tagliburi="/WEB-INF/struts-html.tld"prefix="html"%>

<%@tagliburi="/WEB-INF/struts-logic.tld"prefix="logic"%>

<%@tagliburi="/WEB-INF/struts-bean.tld"prefix="bean"%>

<html>

<head><title>查询结果</title></head>

<body>

<center><h2>查询结果</h2>

<html:linkpage="/index.jsp">首页</html:link>|

<hr/>

</center>

<br/><br/>

<tableborder="1"width="90%"align="center">

<tr>

<tdwidth="4%"align="center">序号

<tdwidth="37%"align="center">书名

<tdwidth="13%"align="center">作者

<tdwidth="10%"align="center">价格

<tdwidth="18%"align="center">出版社

</tr>

<logic:presentname="result"scope="request">

共<bean:writename="pager"property="totalPages"/>页

第<bean:writename="pager"property="currentPage"/>页

共<bean:writename="pager"property="totalRows"/>条记录

<logic:equalname="pager"property="hasPrevious"value="true">

<html:linkpage="/searchAction.do?action=prepage">上一页</html:link>

</logic:equal>

<logic:equalname="pager"property="hasNext"value="true">

<html:linkpage="/searchAction.do?action=nextpage">下一页</html:link>

</logic:equal>

<%!staticinti=1;%>

<logic:iterateid="abook"name="result"scope="request">

<tr>

<tdalign="center">

<%introwstart=Integer.parseInt((request.getAttribute("ps").toString()));out.print(rowstart+i);i++;%>

</td>

<td><bean:writename="abook"property="bookname"></bean:write></td>

<tdalign="center"><bean:writename="abook"property="author"></bean:write></td>

<tdalign="center"><bean:writename="abook"property="price"format="0.00"></bean:write></td>

<tdalign="center"><bean:writename="abook"property="press"></bean:write></td>

</tr>

</logic:iterate>

<%i=1;%>

</logic:present>

</table>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: