当前位置:编程学习 > JAVA >>

SSH框架中文参数乱码问题 高分请大家帮我

本人水平非常低,用的技术也比较老。struts是1.3 hibernate是3.0
主要问题是第一次查询可以顺利查出来,点击第一个分页,也能正常分页,第二次点击分页就什么也查询不出来了,下面的编码也与第一次发送的不同,百思不得其解。请大家帮我。

query.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@include file="/common/common.jsp" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<link href="style/oa.css" rel="stylesheet" type="text/css">
<script language="javascript" src="script/public.js"></script>

<script type="text/javascript">
window.onload= function()
{
    var typeValue = document.getElementById("orgtype").value;  

    var typeindex = 0;
    
    if(typeValue  == "客调")
    {
     typeindex = 1;
    }
    
    if(typeValue == "服务开通")
    {
     typeindex = 2;
    }
        if(typeValue == "综合激活")
    {
     typeindex = 3;
    }
        if(typeValue == "NM")
    {
     typeindex = 4;
    }
        if(typeValue == "TELANT")
    {
     typeindex = 5;
    }
        if(typeValue == "GIS")
    {
     typeindex = 6;
    }
        if(typeValue == "WEBGIS")
    {
     typeindex = 7;
    }
        if(typeValue == "信息发布")
    {
     typeindex = 8;
    }   
        
    document.getElementById("org").selectedIndex = typeindex;

 }


function hidequery()
   {
       var aaa = document.getElementById("querydiv");
       var bbb = document.getElementById("imgjt");

       if(aaa.style.display != "none")
{
aaa.style.display = "none";
//bbb.value = "展示";
bbb.src = "images/jiantouxia.gif";
}else
{
aaa.style.display = "block";
bbb.src = "images/jiantoushang.gif";
}
   }
   
  function resetquery()
   {
       document.getElementById("org").selectedIndex = 0;
    document.getElementById("name").value = "";
    document.getElementById("kldzd").value = "";
    document.getElementById("description").value = "";
    document.getElementById("queryids").value = "";
   }
</script>

<title>查询颗粒度</title>
</head>
<BODY bgColor=#dee7ff leftMargin=0 background="" topMargin=0 marginheight="0" marginwidth="0">
<center>

<pg:pager url="dim.do" items="${pm.total }" export="currentPageNumber=pageNumber">
<pg:param name="queryids"/>
<pg:param name="query" value="true"/>
<pg:param name="name"/>
<pg:param name="org"/>
<pg:param name="kldzd"/>
<pg:param name="description"/>
<pg:first>
<a href="${pageUrl}">首页</a>
</pg:first>
<pg:prev>
<a href="${pageUrl }">前页</a>
</pg:prev>
<pg:pages>
<c:choose>
<c:when test="${currentPageNumber eq pageNumber }">
<font color="red">${pageNumber }</font>
</c:when>
<c:otherwise>
<a href="${pageUrl }">${pageNumber }</a>
</c:otherwise>
</c:choose>
</pg:pages>
<pg:next>
<a href="${pageUrl }">后页</a>
</pg:next>
<pg:last>
<a href="${pageUrl }">尾页</a>
</pg:last>
</pg:pager>
           。。。。。
        <TBODY>
          <TR>
            <TD height=28 align=right vAlign=center noWrap background=images/list_middle.jpg>  
            <!-- 可以在这里插入分页导航条 -->
<pg:pager url="dim.do" items="${pm.total }" export="currentPageNumber=pageNumber">
<pg:param name="queryids"/>
<pg:param name="query" value="true"/>
<pg:param name="name"/>
<pg:param name="org"/>
<pg:param name="kldzd"/>
<pg:param name="description"/>
<pg:first>
<a href="${pageUrl}">首页</a>
</pg:first>
<pg:prev>
<a href="${pageUrl }">前页</a>
</pg:prev>
<pg:pages>
<c:choose>
<c:when test="${currentPageNumber eq pageNumber }">
<font color="red">${pageNumber }</font>
</c:when>
<c:otherwise>
<a href="${pageUrl }">${pageNumber }</a>
</c:otherwise>
</c:choose>
</pg:pages>
<pg:next>
<a href="${pageUrl }">后页</a>
</pg:next>
<pg:last>
<a href="${pageUrl }">尾页</a>
</pg:last>
</pg:pager>
     </TD>
          </TR>
        </TBODY>
      </TABLE>
</center>

</body>

</html>
按照网上的解决方案 我写了一个过滤器
CharsetAllEncodingFilter.java
ublic class CharsetAllEncodingFilter implements Filter {

private String encoding = null;

/**
* Request.java 对 HttpServletRequestWrapper 进行扩充, 不影响原来的功能并能提供所有的
* HttpServletRequest 接口中的功能. 它可以统一的对 Tomcat 默认设置下的中文问题进行解决而只需要用新的 Request
* 对象替换页面中的 request 对象即可.
*/
class Request extends HttpServletRequestWrapper {
  
   private String encoding = null;
   public Request(HttpServletRequest request) {
    super(request);
    encoding = CharsetAllEncodingFilter.this.encoding;
   }
   /**
   * 转换由表单读取的数据的内码. 从 ISO 字符转到 在web.xml中设置的encoding.
   */
   public String toChi(String input) {
    if(input == null) {
     return null;
    } else {
     try {
      byte[] bytes = input.getBytes("ISO8859-1");
      return new String(bytes, encoding);
     } catch (Exception ex) {
     }
     return null;
    }      
   }
   /**
   * Return the HttpServletRequest holded by this object.
   */
   private HttpServletRequest getHttpServletRequest() {
    return (HttpServletRequest) super.getRequest();
   }
   /**
   * 读取参数 -- 修正了中文问题.
   */
   public String getParameter(String name) {
    return toChi(getHttpServletRequest().getParameter(name));
   }
   /**
   * 读取参数列表 - 修正了中文问题.
   */
   public String[] getParameterValues(String name) {
    String values[] = getHttpServletRequest().getParameterValues(name);
    if (values != null) {
     for (int i = 0; i < values.length; i++) {
      values[i] = toChi(values[i]);
     }
    }
    return values;
   }
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
   HttpServletRequest httpreq = (HttpServletRequest) request;
   if (httpreq.getMethod().equals("POST")) {
    request.setCharacterEncoding(encoding);
   } else {
    request = new Request(httpreq);
   }
   chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
   encoding = filterConfig.getInitParameter("encoding");
}
}

web.xml配置如下
<filter>
<filter-name>CharsetEncodingFilter</filter-name>
<filter-class>com.xq.wdgl.util.CharsetAllEncodingFilter</filter-class>
<init-param>
      <param-name>encoding</param-name>
      <param-value>GB18030</param-value>
</init-param>
</filter>      
<filter-mapping>
<filter-name>CharsetEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
  <filter>
    <filter-name>pagerFilter</filter-name>
    <filter-class>com.xq.wdgl.web.PagerFilter</filter-class>
  </filter>
 <filter-mapping>
    <filter-name>pagerFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>











SSH 乱码 --------------------编程问答-------------------- <pg:pager url="dim.do" items="${pm.total }" export="currentPageNumber=pageNumber">
<pg:param name="queryids"/>
<pg:param name="query" value="true"/>
<pg:param name="name"/>
<pg:param name="org"/>
<pg:param name="kldzd"/>
<pg:param name="description"/>
<pg:first>
<a href="${pageUrl}">首页</a>
</pg:first>
<pg:prev>
<a href="${pageUrl }">前页</a>
</pg:prev>
<pg:pages>
<c:choose>
<c:when test="${currentPageNumber eq pageNumber }">
<font color="red">${pageNumber }</font>
</c:when>
<c:otherwise>
<a href="${pageUrl }">${pageNumber }</a>
</c:otherwise>
</c:choose>
</pg:pages>
<pg:next>
<a href="${pageUrl }">后页</a>
</pg:next>
<pg:last>
<a href="${pageUrl }">尾页</a>
</pg:last>
</pg:pager>

请问是否是这个分页框架的问题呢? --------------------编程问答-------------------- 是不是编码没有统一啊,如果不统一的话会出现有乱码的问题 --------------------编程问答-------------------- 1、比较第二页的url链接和第三页的url链接有何不同,应该只有页数不同才对。

2、在你CharsetAllEncodingFilter打印出第二页和第三页来的参数有什么不同。 --------------------编程问答-------------------- JSP的编码统一一下。 --------------------编程问答-------------------- 所有的JSP页面全部都是GB18030
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@include file="/common/common.jsp" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<link href="style/oa.css" rel="stylesheet" type="text/css">
<script language="javascript" src="script/public.js"></script>

如图所示我要将服务开通作为参数下传
第一次查询点击1,2,3下面的参数都是对的,出问题是查询过一次再点 参数就发现都变掉了

--------------------编程问答--------------------
引用 3 楼 u010255083 的回复:
1、比较第二页的url链接和第三页的url链接有何不同,应该只有页数不同才对。

2、在你CharsetAllEncodingFilter打印出第二页和第三页来的参数有什么不同。


好的 我调试下,太浮躁了,不停在网上看怎么解决 --------------------编程问答--------------------
引用 4 楼 k_scott 的回复:
JSP的编码统一一下。


您的意思是这个分页框架里面的编码不是GB18030??
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,