struts2分页、乱码
用struts2 做了一个毕业设计,分页问题和中英文乱码问题解决不了,请各位高手指点?
[解决办法]
帖代码出来。
[解决办法]
<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>分页</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function chec(code)
{
var a=document.createElement("a");
a.href="/biYeSheJi/checkkh1.action?code="+code;
document.body.appendChild(a);
a.click();
}
function updat(code)
{
var a=document.createElement("a");
a.href="/biYeSheJi/upkh.action?code="+code;
document.body.appendChild(a);
a.click();
}
function delet(code)
{
var a=document.createElement("a");
a.href="/biYeSheJi/deletekh.action?code="+code;
document.body.appendChild(a);
a.click();
}
</script>
</head>
<body>
<s:form name="f" method="post" action="checkkh1.action">
<center>
<table>
<tr>
<td>
<s:textfield name="code" label="编号"></s:textfield>
</td>
<td>
<s:submit value="查询"></s:submit>
</td>
</tr>
</table>
</center>
</s:form>
<s:form name="f2">
<center>
<table border="2">
<tr>
<td>
<a href="fenye.action">获得客户信息</a>
<br>
</td>
</tr>
<tr>
<td width="10%">
编号
<br>
<br>
</td>
<td width="20%">
客户名称
<br>
<br>
</td>
<td width="10%">
联系人
<br>
<br>
</td>
<td width="10%">
电话
<br>
<br>
</td>
<td width="10%">
详细信息
<br>
<br>
</td>
<td width="10%">
修改
<br>
<br>
</td>
<td width="10%">
删除
<br>
<br>
</td>
</tr>
<s:iterator value="khList" id="model">
<tr>
<td>
<s:property id="model" value="code" />
</td>
<td>
<s:property id="model" value="name" />
</td>
<td>
<s:property id="model" value="lxr" />
</td>
<td>
<s:property id="model" value="lxrTelphone" />
</td>
<td>
<input type="button" name="button" value="详细信息"
onclick="chec('<s:property id="model" value="code"/>')">
</td>
<td>
<input type="button" name="button" value="修改"
onclick="updat('<s:property id="model" value="code"/>')">
</td>
<td>
<input type="button" name="button" value="删除"
onclick="delet('<s:property id="model" value="code"/>')">
</td>
</tr>
</s:iterator>
</table>
</center>
</s:form>
<s:form name="f3">
<table>
<tr>
<td>
共计
<s:property value="totalNum " />
条记录,【共
<s:property value="totalPage" />
页】
</td>
<td>
页面大小
<input type="text" name="pageSize" value="" />
条/页 当前第
<input type="text" name="currentPage" value="" />
页
</td>
<td>
<s:url id="shouye" action="fenye.action">
<s:param name="currentPage">
<s:property value="1" />
</s:param>
<s:param name="pageSize">
<s:property value="5" />
</s:param>
</s:url>
<s:a href="%{shouye}">首页</s:a>
<s:url id="shouye" action="fenye.action">
<s:param name="currentPage">
<s:property value="currentPage-1" />
</s:param>
<s:param name="pageSize">
<s:property value="5" />
</s:param>
</s:url>
<s:a href="%{xiayiye}">下一页</s:a>
<s:url id="shangyiye" action="fenye.action">
<s:param name="currentPage">
<s:property value="currentPage+1" />
</s:param>
<s:param name="pageSize">
<s:property value="5" />
</s:param>
</s:url>
<s:a href="%{shangyiye}">上一页</s:a>
</td>
<td>
跳至
<input type="text" maxlength="2" name="page" value=""/ >
页
<input type="button" value="GO" >
</td>
</tr>
</table>
</s:form>
</body>
</html>
[解决办法]
package request;
import model.KhModel;
import agent.PageAgent;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class PageAction extends ActionSupport{
private String id;
private String name;
private String lxr;
private String lxrTelphone;
private int pageSize; //每页大小
private int currentPage; //当前页
private int totalNum; //总的记录数
private List KhList;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLxr() {
return lxr;
}
public void setLxr(String lxr) {
this.lxr = lxr;
}
public String getLxrTelphone() {
return lxrTelphone;
}
public void setLxrTelphone(String lxrTelphone) {
this.lxrTelphone = lxrTelphone;
}
public List getKhList() {
return KhList;
}
public void setKhList(List khList) {
KhList = khList;
}
public void setTotalNum(int totalNum) {
this.totalNum = totalNum;
}
public String getTotalNum( )throws Exception
{
PageAgent agent = new PageAgent();
this.setTotalNum(agent.totalNum());
return "ok";
}
public String querryData() throws Exception
{
PageAgent agent = new PageAgent();
judge();
this.setKhList(agent.querryPage(currentPage,pageSize));
return "ok";
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void judge (){
if (Integer.valueOf(getCurrentPage()).toString()==null
[解决办法]
getCurrentPage()<=0)
{
currentPage=1;
}
else
currentPage=getCurrentPage();
}
}
package model;
public class PageModel {
private int amountCount; //总的记录数
private int perCount; //每一页的记录数
private int page; //页数
private int pageCount; //当页的第几条信息
public int getAmountCount() {
return amountCount;
}
public void setAmountCount(int amountCount) {
this.amountCount = amountCount;
}
public int getPerCount() {
return perCount;
}
public void setPerCount(int perCount) {
this.perCount = perCount;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
}
/**
*
*/
package agent;
/**
* @author Administrator
*
*/
import dao.PageDao;
import java.util.*;
public class PageAgent {
private PageDao dao;
public PageAgent()
{
dao = new PageDao();
}
public List querryPage(int currentPage,int pageSize)
{
return dao.querryPage(currentPage,pageSize);
} //返回数据
public int totalNum()
{
return dao.totalNum();
} //总记录数
public int totalPage()
{
return dao.totalPage();
} //总页数
public void first()
{
dao.first();
} //第一页
public void previous()
{
dao.previous();
} // 上一页
public void next()
{
dao.next();
} //下一页
public void forward(int page)
{
dao.forward(page);
} //跳至德页
}
package dao;
import java.sql.*;
import model.PageModel;
import model.KhModel;
import connection.DbConnectionUti;
import java.util.*;
public class PageDao {
private int totalNum; //总记录数
private int totalPage; //总页数
private int rowNum; //行数(第几行)
private int pageSize; // 每页显示6条信息
private int startRow; //起始行
private int currentPage; //当前页
public List querryPage(int currentPage,int pageSize)
{
int startRow; //起始行
int endRow; //结尾行
int _totalNum = totalNum(); //获得总记录数
List list = new ArrayList();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
DbConnectionUti db = new DbConnectionUti();
String sql = "";
conn = db.getConnection();
sql = "select id,name,lxr,lxrTelphone from tb_kh_info ";
System.out.println("&&&&&&"+sql);
startRow = (currentPage-1)*pageSize; //当页的首行
endRow = currentPage*pageSize; //当页的尾行记录
//-------------------分页的SQL语句------------------
sql = "select * from "+
"(select list.*,rownum rn from("+sql+")list "+
"where rownum<'"+endRow+"'" +
" )"+
"where rn>'"+startRow+"'"+
"order by id desc ";
System.out.println("%%%%%%%%&"+sql);
//-------------------分页的SQL语句------------------
try{
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next())
{
KhModel model = new KhModel(); //new 客户对象
model.setCode(rs.getString("id"));
model.setName(rs.getString("name"));
model.setLxr(rs.getString("lxr"));
model.setLxrTelphone(rs.getString("lxrTelphone"));
list.add(model);
}
}catch(Exception e)
{
e.printStackTrace();
}finally{
try{
if(rs!=null)
{
rs.close();
}
if(pstmt!=null)
{
pstmt.close();
}
if(conn!=null)
{
conn.close();
}
}catch(SQLException sqle)
{
sqle.printStackTrace();
}
}
return list;
}
//==================获得做的记录数
public int totalNum()
{
totalNum=0;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
DbConnectionUti db = new DbConnectionUti();
conn = db.getConnection(); //获得数据库连接
String sql = "";
sql = "select id,name,lxr,lxrTelphone from tb_kh_info ";
System.out.println("&&&&&&"+sql);
try{
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next())
{
totalNum = totalNum+1; //总记录数加1
}
}catch(Exception e)
{
e.printStackTrace();
}finally{
try{
if(rs!=null)
{
rs.close();
}
if(pstmt!=null)
{
pstmt.close();
}
if(conn!=null)
{
conn.close();
}
}catch(SQLException sqle)
{
sqle.printStackTrace();
}
}
return totalNum;
}
//==================获得做的记录数
public int totalPage()
{
int totalpage=0;
totalPage = (totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1);
return totalPage;
} //返回总页数
public void first()
{
currentPage = 1;
startRow = 0;
} //首页
public void previous() {
if (currentPage == 1) {
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
} // 上一页
public void next()
{
if(currentPage<totalPage)
{
currentPage++;
}else
{
currentPage = totalPage;
}
startRow = (currentPage-1)*pageSize;
} //下一页
public void forward(int page)
{
if(page<=totalPage)
{
currentPage = page; //当前页等于要跳至的页
startRow = (currentPage-1)*pageSize;
}
} //要跳的页号
//
// public static void main(String[] args)
// {
// List list = new ArrayList();
// PageDao dao = new PageDao();
// KhModel model = new KhModel();
//
// list = dao.querryPage(3);
// list.contains(model.getCode());
// int a=dao.totalNum();
// System.out.println("list="+list.contains(model.getCode()));
// System.out.println("a = "+a);
//
//
// }
}