Struts2用通配符进行模糊分页查询

jap页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script>
	function $(v){
		return document.getElementById(v);
	}
	//每页显示数量
	var pageSize;   //当前每页显示记录数
	var tid;		//图书类别编号
	var bname;		//图书名称
	//用于改变页数和每页记录数时提交form表单
	function fun(v){
		$("page").value=v;					//当前页数
		$("pb.pageSize").value=pageSize;	//当前每页显示记录数
		$("tid").value=tid;					//图书类别编号
		$("bname").value=bname;				//图书名称
		$("frm").submit();					//提交表单
	}
	//一开始就给每页的显示记录赋值,否则当你点击下一页时,pageSize的值为未定义,
	//因为每当你点击切换页面时,页面都会刷新,pageSize的值自然也未定义
	window.οnlοad=function(){
		pageSize=$("pageSize").value;		//将页面下的每页显示记录数的值赋给定义的(var)每页显示记录数,以便与点击下一页时,pageSize有数值
		tid=$("tid").value;
		bname=$("bname").value;
	}
	//当表单提交时,将当前页和每页显示记录数一起赋给隐藏域
	function fun2(v){
		$("page").value=v;
		$("pb.pageSize").value=$("pageSize").value;
	}
</script>
<body>
	<h2>图书列表</h2><hr>
	<table align="center" width=800 border=1 rules="rows">
		<tr>
			<td colspan=5>
				<s:form action="book_list" id="frm" theme="simple" οnsubmit="fun2(1)">
					<input type="hidden" name="pb.page" id="page">				
					<input type="hidden" name="pb.pageSize" id="pb.pageSize">
					图书类别:<s:select list="bt" headerKey="0" headerValue="全部" listKey="tid" listValue="type" name="b.tid" id="tid" ></s:select>
					图书名称:<s:textfield name="b.bname" id="bname" ></s:textfield>	
					<s:submit value="查询"></s:submit>
				</s:form>
			</td>
		</tr>
		<tr>
			<th>图书编号</th><th>图书类型</th><th>图书名称</th><th>图书作者</th><th>是否借出</th>
		</tr>
		<s:iterator value="pb.blist">
			<tr align="center">
				<td><s:property value="bid" /></td>
				<td><s:property value="type" /></td>
				<td><s:property value="bname" /></td>
				<td><s:property value="author" /></td>
				<td><s:property value='isOut=="Y"?"不可借":"可借"' /></td>
			</tr>
		</s:iterator>
		<tr>
			<td colspan=5>
				<s:if test="pb.page==1 && pb.page<pb.totalPage">
					<a href="javascript:void(0)" οnclick="fun(${pb.page+1})">下一页</a>
					<a href="javascript:void(0)" οnclick="fun(${pb.totalPage})">末页</a>
				</s:if>
				<s:if test="pb.page>1 && pb.page<pb.totalPage">
					<a href="javascript:void(0)" οnclick="fun(1)">首页</a>
					<a href="javascript:void(0)" οnclick="fun(${pb.page-1})">上一页</a>
					<a href="javascript:void(0)" οnclick="fun(${pb.page+1})">下一页</a>
					<a href="javascript:void(0)" οnclick="fun(${pb.totalPage})">末页</a>
				</s:if>
				<s:if test="pb.page!=1 && pb.page==pb.totalPage">
					<a href="javascript:void(0)" οnclick="fun(1)">首页</a>
					<a href="javascript:void(0)" οnclick="fun(${pb.page-1})">上一页</a>
				</s:if>
				<s:if test="pb.page==1 && pb.page==pb.totalPage">仅有此页</s:if>
				<s:if test="pb.totalRecord==0">暂无该类图书</s:if>
				          
				第<font style="color:red;font-weight:bold">${pb.page }</font>页 / 共<font style="color:red;font-weight:bold">${pb.totalPage }</font>页  
				每页显示<input id="pageSize" style="font-weight:bold" size=3 οnblur="pageSize=this.value;fun(1)" value="${pb.pageSize }" >条
			</td>
		</tr>
	</table>
	
</body>
</html>


dao层

package com.mingde.dao.impl;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import com.mingde.dao.BookDao;
import com.mingde.po.Book;
import com.mingde.po.BookType;
import com.mingde.po.PageBean;
import com.mingde.utils.JdbcUtils;


public class BookDaoImpl implements BookDao {
	QueryRunner qr;
	public BookDaoImpl() {
		qr=new QueryRunner(JdbcUtils.getDataSource());
	}
	
	@Override
	public PageBean seeAll(int page, int pageSize, String bname, int tid) throws Exception {
		String sql="select b.*,bt.type,rownum r from books b, booktype bt where b.tid=bt.tid ";
		if(tid!=0)sql+=" and b.tid="+tid;
		if(null!=bname && !"".equals(bname))sql+=" and bname like '%"+bname+"%' ";
		String sql2="select * from ("+sql+")t where r>"+(page-1)*pageSize+" and r<="+page*pageSize;
		int totalRecord = qr.query(sql, new BeanListHandler<>(Book.class)).size();
		int totalPage=(int)Math.ceil((double)totalRecord/pageSize);
		List<Book> blist=qr.query(sql2, new BeanListHandler<>(Book.class));
		return new PageBean(page, pageSize, totalRecord, totalPage, blist);
	}


	@Override
	public List<BookType> seeBookType() throws Exception {
		return qr.query("select * from BookType", new BeanListHandler<>(BookType.class));
	}
}

Action类

package com.mingde.action;

import java.util.List;

import com.mingde.dao.BookDao;
import com.mingde.dao.impl.BookDaoImpl;
import com.mingde.po.Book;
import com.mingde.po.BookType;
import com.mingde.po.PageBean;
import com.opensymphony.xwork2.ActionSupport;


@SuppressWarnings("serial")
public class BookAction extends ActionSupport{
	
	private BookDao bd=new BookDaoImpl();
	private PageBean pb=new PageBean();
	private Book b=new Book();
	private List<BookType> bt;
	
	public String list() throws Exception {
		bt=bd.seeBookType();
		if(pb.getPage()==0)pb.setPage(1);
		if(pb.getPageSize()==0)pb.setPageSize(5);
		pb=bd.seeAll(pb.getPage(),pb.getPageSize(),b.getBname(),b.getTid());
		System.out.println(pb);
		return "list";
	}

	public void setB(Book b) {
		this.b = b;
	}
	
	/**  GET和SET方法 **/
	
	public PageBean getPb() {
		return pb;
	}

	public List<BookType> getBt() {
		return bt;
	}

	public void setBt(List<BookType> bt) {
		this.bt = bt;
	}

	public void setPb(PageBean pb) {
		this.pb = pb;
	}

	public Book getB() {
		return b;
	}

}

pageBean类的属性

	private int page;   		//当前页数
	private int pageSize;		//页面显示多少条记录数
	private int totalPage;		//总页数
	private int totalRecord;	//总记录数
	private List<Book> blist=new ArrayList<>();	//查询的结果集合

Struts.XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
	<constant name="struts.devMode" value="true"></constant>
	<package name="struts" extends="struts-default">
		<action name="*_*" class="com.mingde.action.BookAction" method="{2}">
			<result name="{2}">/WEB-INF/{1}/{2}.jsp</result>
		</action>
	</package>
</struts>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值