javaweb的struts2的分页查询操作
分页查询的实现:
1.利用action完成
action有成员变量:
除了实体对象的属性还有
// 分页显示
public int totalRecord;
// 每页显示条数
public int limitRecord = 5;
// 当前页
public int nowPage = 1;
// 总页数
public int totalPage;
// 实体对象查询结果集合
private LinkedList op;
2.sql语句:
select * from tb_userinfo where id="" limit a,b
这样只会查询a,b条间的记录。
是分页实现的一种方式。
直接上代码
1.后台:
查询action类
说明:
**delete()方法是查询主要实现。
gettotal()是获取总记录数与总页数
JDBC_S是查询model层
**
public String sql_s(String sql) {}//处理sql
可以使用其他方式实现,反正需要一个完整的sql;自己实现
Jdbc_DBCP_xiu是数据库链接池。
package controller.option;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.LinkedList;
import org.apache.struts2.ServletActionContext;
import com.dbcp.Jdbc_DBCP_xiu;
import model.linku.JDBC_S;
import model.linku.UserX;
public class UserXselect extends UserXinfo {
/**
* @author joker
*
* action 分页查询信息表tb_userinfo数据
*/
private static final long serialVersionUID = -8974990156314709622L;
// 分页显示
public int totalRecord;
// 每页显示条数
public int limitRecord = 5;
// 当前页
public int nowPage = 1;
// 总页数
public int totalPage;
// 结果集合
private LinkedList<UserX> op;
public LinkedList<UserX> getOp() {
return op;
}
public int getNowPage() {
return nowPage;
}
public void setNowPage(int nowPage) {
this.nowPage = nowPage;
}
public int getTotalRecord() {
return totalRecord;
}
public int getTotalPage() {
return totalPage;
}
public void setOp(LinkedList<UserX> op) {
this.op = op;
}
// 获取查询结果的总记录数总页数
public void getTotal() {
try {
String sql = this.sql_s("select count(*) from tb_userinfo where ");
Connection co = Jdbc_DBCP_xiu.getConnection();
// 获取查询结果的总记录数总页数
java.sql.PreparedStatement p = co.prepareStatement(sql);
ResultSet result = p.executeQuery();
// 设置结果
while (result.next()) {
this.totalRecord = result.getInt(1);
// 取整
if ((totalRecord % limitRecord) > 0)// 向上取整
this.totalPage = ((int) (totalRecord / limitRecord)) + 1;
else// 取整
this.totalPage = ((int) (totalRecord / limitRecord));
if (this.totalPage <= 0) {
this.totalPage = 1;
}
}
Jdbc_DBCP_xiu.release(co, p, result);
} catch (Exception e1) {
// TODO 自动生成的 catch 块
}
return;
}
// 处理整合数据
public String sql_s(String sql) {
if (!id.equals(""))
sql = sql + "id='" + id + "' and ";
if (!name.equals(""))
sql = sql + "name='" + name + "' and ";
if (!sex.equals(""))
sql = sql + "sex='" + sex + "' and ";
if (!local.equals(""))
sql = sql + "local='" + local + "' and ";
if (!status.equals(""))
sql = sql + "status='" + status + "' and ";
if (!lv.equals(""))
sql = sql + "lv='" + lv + "' and ";
// 表单传递
String form = "id=" + id + "&name=" + name + "&sex=" + sex + "&status=" + status + "&lv=" + lv + "&local="
+ local;
ServletActionContext.getRequest().getSession().setAttribute("theFinfo", form);
if (sql.lastIndexOf("and ") + 4 <= sql.length() && sql.lastIndexOf("and ") >= 0)
sql = sql.substring(0, sql.lastIndexOf("and ") - 1);
if (sql.length() <= "select * from tb_userinfo where ".length())
return "";
return sql;
}
public String select() throws Exception {
// TODO 自动生成的方法存根
// 获取查询结果的总记录数总页数
this.getTotal();
String sql = this.sql_s("select * from tb_userinfo where ");
if (sql.equals("")) {
this.addFieldError("id", "至少输入一项信息!!!");
return INPUT;
} else {
JDBC_S s = new JDBC_S(sql + "limit " + (nowPage - 1) * limitRecord + "," + limitRecord);
LinkedList<UserX> op1 = s.getSelectResult();
if (!op1.isEmpty()) {
op = op1;
return SUCCESS;
} else {
this.addFieldError("id", "查询无信息!!!");
return INPUT;
}
}
}
}
JDBC_S.java(查询model层)
package model.linku;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.LinkedList;
import com.dbcp.Jdbc_DBCP_xiu;
import com.opensymphony.xwork2.ActionSupport;
public class JDBC_S extends ActionSupport {
/**
* @author joker
*
*select
*/
private static final long serialVersionUID = -391369734025576555L;
private LinkedList<UserX> select = new LinkedList<>();
public LinkedList<UserX> getSelectResult() {
return select;
}
public JDBC_S(String sql) {
this.query(sql);
}
private void query(String sql) {
try {
Connection co = Jdbc_DBCP_xiu.getConnection();
java.sql.PreparedStatement p = co.prepareStatement(sql);
ResultSet result = p.executeQuery();
// 设置结果
while (result.next()) {
UserX u = new UserX(result.getString("id"),result.getString("name"), result.getString("sex"), result.getString("local"),
result.getString("status"), result.getString("lv"));
this.select.add(u);
}
Jdbc_DBCP_xiu.release(co, p, result);
} catch (Exception e1) {
// TODO 自动生成的 catch 块
}
}
}
2.前端:
select.jsp
op是action的成员
上一页,下一页的query.action自己配置
<s:form class="input" action="query.action" method="post">
<s:textfield class="input1" name="id" label="ID查找"></s:textfield>
<s:textfield class="input1" name="name" label="姓名查找"></s:textfield>
<s:textfield class="input1" name="local" label="地域查找"></s:textfield>
<s:textfield class="input1" name="lv" label="修为查找"></s:textfield>
<s:radio name="sex" label="性别查找" list="#{'男':'男','女':'女'}"></s:radio>
<s:radio name="status" list="#{'存活':'存活','死亡':'死亡'}" label="状态查找"></s:radio>
<s:submit value="查询数据信息"></s:submit>
</s:form>
<s:if test="op">
<div class="table">查询结果表</div>
<div class="table" style="border: 3px solid white">
<table class="table">
<thead>
<tr class="table">
<th class="th">ID</th>
<th class="th">姓名</th>
<th class="th">性别</th>
<th class="th">出生地</th>
<th class="th">当前状态</th>
<th class="th">当前修为</th>
</tr>
</thead>
<tbody>
<s:form theme="simple">
<s:iterator value="op" var="x">
<tr class="table">
<td class="td">
<s:property value="%{#x.id}" />
</td>
<td class="td">
<s:property value="%{#x.name}" />
</td>
<td class="td">
<s:property value="%{#x.sex}" />
</td>
<td class="td">
<s:property value="%{#x.local}" />
</td>
<td class="td">
<s:property value="%{#x.status}" />
</td>
<td class="td">
<s:property value="%{#x.lv}" />
</td>
</tr>
</s:iterator>
</s:form>
</tbody>
</table>
<div class="tablex">
每次限制查询与操作的记录数:
<s:property value="%{limitRecord}" />
<s:if test="%{nowPage-1}"></s:if>
<s:if test="nowPage>1">
<s:a href="query.action?nowPage=%{nowPage-1}&%{#session.theFinfo}">上一页</s:a>
</s:if>
<s:if test="nowPage<totalPage">
<s:a href="query.action?nowPage=%{nowPage+1}&%{#session.theFinfo}">下一页</s:a>
</s:if>
当前为第
<s:property value="%{nowPage}" />
页 ,共
<s:property value="%{totalPage}" />
页 ,共
<s:property value="%{totalRecord}" />
条记录
</div>
</div>
</s:if>
ok