jsp page 分页文件,解决不同浏览器乱码

一个分页的jsp,可以自动在请求范围内获取所有参数,以便点下一页的时候自动穿过去参数,这里面有个乱码的问题,主要是浏览器上的,IE和火狐不一样,所以传参数的时候先吧参数编码变成ASCII码,这样在服务器端再转化一下就可以了


<%@page language="java" pageEncoding="UTF-8"%>
<%@page import="java.util.Enumeration"%>
<%@page import="com.hcoa.util.Page"%>
<%@page import="com.hcoa.util.DecoderEncoderUtil"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String url = request.getParameter("url");
Enumeration en = request.getParameterNames();
Page p = (Page) request.getAttribute("page");
String urlp = "";
int num = p.getCurrentPageNo();
while (en.hasMoreElements()) {
Object next = en.nextElement();
if (!next.equals("url") && !next.equals("pageNo")) {
String parm = request.getParameter(next.toString());
if (parm != null){
parm=DecoderEncoderUtil.encoder(parm);
urlp += "&" + next + "=" + parm;
}
}
}
%>
<label style="font-size: 12px; color: black;">
当前第
<s:property value="#request.page.getCurrentPageNo()" />
/
<s:property value="#request.page.getTotalPageCount()" />
总记录数:
<s:property value="#request.page.getTotalCount()" />
</label>
<s:if test="#request.page.getCurrentPageNo()!=1">
<%
out.write("<a href='" + url + "?pageNo=" + 1 + urlp + "'>"
+ "首页</a> ");
%>
</s:if>
<s:else>
<%
out.write("首页");
%>
</s:else>
<s:if test="#request.page.hasPreviousPage()">
<%
int prepage = num - 1;
out.write("<a href='" + url + "?pageNo=" + prepage + urlp
+ "'>" + "上一页</a> ");
%>
</s:if>
<s:else>
<%
out.write("上一页");
%>
</s:else>
<%
int totalDuan=(((Long)p.getTotalPageCount()).intValue()-1)/10+1;//总段数
int currentDuan=(p.getCurrentPageNo()-1)/10+1;//当前段数
if(currentDuan>1){//如果当前段数并且不是第一段
String pu = url + "?pageNo=" + ((currentDuan-1)*10) + urlp;
out.write("<a href='" + pu + "'>...</a> ");
}
//中间从当前段的第一页开始打印
int start=(currentDuan-1)*10+1;
int end=start+10;
if(currentDuan==totalDuan){//当前段就是最后一段
end=start+((Long)p.getTotalPageCount()).intValue()-(currentDuan-1)*10;
}
for (int i = start; i < end; i++) {
String outUrl;
outUrl = url + "?pageNo=" + i + urlp;
if (i == p.getCurrentPageNo()) {
out.write("<span class='now'>" + i + "</span>");
} else {
out.write("<a href='" + outUrl + "'>" + i + "</a> ");
}
}

if(totalDuan>1&&currentDuan!=totalDuan){//如果当前段数并且不是最后一段
String au = url + "?pageNo=" + (currentDuan*10+1) + urlp;
out.write("<a href='" + au + "'>...</a> ");
}
%>
<s:if test="#request.page.hasNextPage()">
<%
int nextpage = num + 1;
out.write("<a href='" + url + "?pageNo=" + nextpage + urlp
+ "'>" + "下一页</a> ");
%>
</s:if>
<s:else>
<%
out.write("下一页");
%>
</s:else>
<s:if
test="#request.page.getCurrentPageNo()!=#request.page.getTotalPageCount()&&#request.page.getTotalPageCount()!=0">
<%
out.write("<a href='" + url + "?pageNo="
+ p.getTotalPageCount() + urlp + "'>" + "尾页</a> ");
%>
</s:if>
<s:else>
<%
out.write("尾页");
%>
</s:else>


编码解码工具类:

package com.hcoa.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class DecoderEncoderUtil {
// tomcat默认的编码ISO-8859-1
// public String getKey() {
// try {
// key = URLEncoder.encode(key, "utf-8");
// } catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// }
// // System.out.println(key);
// return key;
// }
//
// public void setKey(String key) {
// try {
// if ("GET".equals(getRequest().getMethod().toUpperCase())) {
// key = new String(getRequest().getParameter("key").getBytes(
// "ISO-8859-1"), "utf-8");
// }
// key = URLDecoder.decode(key, "utf-8");
// } catch (Exception e) {
// e.printStackTrace();
// }
// // System.out.println(key);
// this.key = key.trim();
// }

// 以下是tomcat编码设置为utf-9
public static String encoder(String key) {
if (key == null) {
return null;
}
// ISO-8859-1
try {
key = URLEncoder.encode(key, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return key;
}

public static String decoder(String key) {
if (key == null) {
return null;
}
try {
key = URLDecoder.decode(key, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return key;

}

public static void main(String[] args) {
System.out.println(decoder("中文"));
System.out.println(encoder("中文"));
System.out.println(decoder("%E4%B8%AD%E6%96%87"));
}
}


page类:

package com.hcoa.util;

import java.io.Serializable;
import java.util.ArrayList;

/**
* @author RJW {@rennuoting@126.com}
* @version 1.0
* @copyright (C), 2007-2008, RJW. This program is protected by copyright laws.
* @file Page.java
* @date 2008-11-3 下午07:41:13
* @desc 分页对象. 包含数据及分页信息.
*/
@SuppressWarnings("unchecked")
public class Page implements Serializable {

private static final long serialVersionUID = 1L;

static private int DEFAULT_PAGE_SIZE = 20;

/**
* 每页的记录数
*/
private int pageSize = DEFAULT_PAGE_SIZE;

/**
* 当前页第一条数据的位置,从0开始
*/
private int start;

/**
* 当前页中存放的记录,类型一般为List
*/
private Object data;

/**
* 总记录数
*/
private long totalCount;

/**
* 构造方法,只构造空页
*/
public Page() {
this(0, 0, DEFAULT_PAGE_SIZE, new ArrayList());
}

/**
* 默认构造方法
*
* @param start 本页数据在数据库中的起始位置
* @param totalSize 数据库中总记录条数
* @param pageSize 本页容量
* @param data 本页包含的数据
*/
public Page(int start, long totalSize, int pageSize, Object data) {
this.pageSize = pageSize;
this.start = start;
this.totalCount = totalSize;
this.data = data;
}

/**
* 取数据库中包含的总记录数
*/
public long getTotalCount() {
return this.totalCount;
}

/**
* 取总页数
*/
public long getTotalPageCount() {
if (totalCount % pageSize == 0)
return totalCount / pageSize;
else
return totalCount / pageSize + 1;
}

/**
* 取每页数据容量
*/
public int getPageSize() {
return pageSize;
}

/**
* 当前页中的记录
*/
public Object getResult() {
return data;
}

/**
* 取当前页码,页码从1开始
*/
public int getCurrentPageNo() {
return (start / pageSize) + 1;
}

/**
* 是否有下一页
*/
public boolean hasNextPage() {
return (this.getCurrentPageNo() < this.getTotalPageCount());
}

/**
* 是否有上一页
*/
public boolean hasPreviousPage() {
return (this.getCurrentPageNo() > 1);
}

/**
* 获取任一页第一条数据的位置,每页条数使用默认值
*/
protected static int getStartOfPage(int pageNo) {
return getStartOfPage(pageNo, DEFAULT_PAGE_SIZE);
}

/**
* 获取任一页第一条数据的位置,startIndex从0开始
*/
public static int getStartOfPage(int pageNo, int pageSize) {
return (pageNo - 1) * pageSize;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值