java搜索分页_java模糊查询并分页

public class PagerTag extends SimpleTagSupport {

private String uri;//分页要执行的action路径

private Integer curpage;//当前页

private Integer pagesize;//每页显示的记录数

private Integer pagecount;//总页数

private Integer rowcount;//总记录数

public Integer getCurpage() {

return curpage;

}

public void setCurpage(Integer curpage) {

this.curpage = curpage;

}

public Integer getPagesize() {

return pagesize;

}

public void setPagesize(Integer pagesize) {

this.pagesize = pagesize;

}

public Integer getPagecount() {

return pagecount;

}

public void setPagecount(Integer pagecount) {

this.pagecount = pagecount;

}

public Integer getRowcount() {

return rowcount;

}

public void setRowcount(Integer rowcount) {

this.rowcount = rowcount;

}

public String getUri() {

return uri;

}

public void setUri(String uri) {

this.uri = uri;

}

//每次执行标签时会调用toTag

public void doTag() throws JspException, IOException {

//获得页面的输出流

JspWriter out  = this.getJspContext().getOut();

//通过流往页面写数据

out.println("

");

out.println("共" +rowcount+ "行记录,每页");

out.println("条");

out.println("当前第页/共"+ pagecount + "页");

out.println("第一页");

if(curpage > 0) {

out.println("上一页");

}

if(curpage 

out.println("下一页");

}

out.println("最后一页");

out.println("

");

}

}

标签的tld文件 :my.tld

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"

version="2.1">

1.1

my

http://java.pojo.com/tag

pager

com.pojo.web.tag.PagerTag

empty

uri

true

true

curpage

true

true

pagesize

true

true

pagecount

true

true

rowcount

true

true

模糊查询加分页的展示jsp界面

HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

管理电影档期

信息查询

影片名称影厅名称日期

电影档期管理

电影图片

电影名称

日期

时间

影厅

票价

编辑

删除

查看电影订票情况

" width="100px" height="100px" />

'">删除

">修改

&fid=&rdate=&rtime=">查看

curpage="${ curpage }"

pagesize="${ pagesize}"

pagecount="${ pagecount}"

rowcount="${rowcount }"

/>

这里注意到的一点就是 需要导入自定义标签

还有一点 注意到自定义标签 uri的写法  由于我的想法是 将查询条件利用参数传递过去(参数存在request作用域下)

最重要的就是我在路径后面加上了一个temp参数 方便没有带条件的查询[条件的参数为空] (需要对比前面的分页标签类里的uri写法)

上面 Release_findAllPaging.action  在struts.xml里配置 不打算贴出来了 对应的类是ReleaseAction 调用的dao类为ReleaseDao

ReleaseAction类重要方法和属性(get set 方法省略...)

//保存分页的属性

protected Integer curpage = 0;//当前第几页

protected Integer pagesize = 4;//每页条数

protected Integer pagecount;//总页数

protected Integer rowcount; //总行数

//分页查询

public String findAllPaging(){

//地址栏提交中文  tomcat 配置 URIEncoding="GB2312"

HttpServletRequest req = ServletActionContext.getRequest();//拿到请求对象

//表单里的值

String fname = req.getParameter("filmInfo.fname");

String cid = req.getParameter("cinemaInfo.cid");

String rdate = req.getParameter("rdate");

if(fname != null && !fname.equals("")){

req.setAttribute("fname", fname);//不为空的话,放到request作用域下

}

if(cid != null && !cid.equals("")){

req.setAttribute("cid", cid);

}

if(rdate != null && !rdate.equals("")){

req.setAttribute("rdate", rdate);

}

if(fname == null){

fname = (String)req.getAttribute("fname");//等于空 从request作用域下取

}

if(cid == null){

cid = (String)req.getAttribute("cid");

}

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

rdate = (String)req.getAttribute("rdate");

}

request.put("list",dao.findAllPaging(curpage, pagesize, fname, cid, rdate));//查询当前页的记录 放到list里

//存入当前页

request.put("curpage", curpage);

//存入每页条数

request.put("pagesize", pagesize);

//调用dao获得总行数

Integer rowcount = dao.getRowCount(fname,cid,rdate);

//算出总页数

//101行,每页10条 10.1 使用11页

int pagecount =(int)Math.ceil( (rowcount /( pagesize + 0.0)) );

//总页数

request.put("pagecount", pagecount);

//总条数

request.put("rowcount", rowcount);

return "listAction";

}

dao类重要方法

//根据条件 模糊查询 总记录数

public Integer getRowCount(String fname,String cid,String rdate){

String strSQL = "select count(r) from Release as r where 1 = 1";

if(fname != null && !fname.equals("")){//如果equals在前面的话,容易报 nullpoint 异常

strSQL += " and r.filmInfo.fname like :fname";

}

if(cid != null && !cid.equals("")){

strSQL += " and r.cinemaInfo.cid = :cid";

}

if(rdate != null && !rdate.equals("")){

strSQL += " and r.rdate = :rdate";

}

Query query = HibernateSessionFactory.getSession().createQuery(strSQL);

if(fname != null && !fname.equals("")){//如果equals在前面的话,容易报 nullpoint 异常

query.setString("fname", "%" + fname + "%");

}

if(cid != null && !cid.equals("")){

query.setInteger("cid", new Integer(cid));

}

if(rdate != null && !rdate.equals("")){

query.setString("rdate", rdate);

}

List list = query.list();

return (Integer)list.get(0);

}

//分页模糊查询 档期

public List findAllPaging(int curpage,int pagesize,String fname,String cid,String rdate) {

String strSQL = "select r from Release as r where 1 = 1";

if(fname != null && !fname.equals("")){//如果equals在前面的话,容易报 nullpoint 异常

strSQL += " and r.filmInfo.fname like :fname";

}

if(cid != null && !cid.equals("")){

strSQL += " and r.cinemaInfo.cid = :cid";

}

if(rdate != null && !rdate.equals("")){

strSQL += " and r.rdate = :rdate";

}

Query query = HibernateSessionFactory.getSession().createQuery(strSQL);

if(fname != null && !fname.equals("")){//如果equals在前面的话,容易报 nullpoint 异常

query.setString("fname", "%" + fname + "%");

}

if(cid != null && !cid.equals("")){

query.setInteger("cid", new Integer(cid));

}

if(rdate != null && !rdate.equals("")){

query.setString("rdate", rdate);

}

query.setFirstResult(curpage*pagesize)

.setMaxResults(pagesize);

List list = query.list();

return list;

}

save_snippets.png

还有要注意点的是 条件是采用get方式传递 中文的问题解决 需在tomcat下配置文件 加上URIEncoding="GB2312"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值