struts+hibernate中文乱码问题,分不够就加! 有一个项目,用struts提交中文数据时候总是显示乱码,有什么方法可以解决呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 页面: encodeURIaction里这样处理: java.net.URLDecoder.decode(bankMsg, "UTF-8"); 做个过滤器,在web.xml中配置一下 URL中处理不了的中文用String 改下编码 从3个方面:1,数据库有些乱码问题是数据库的编码格式和画面显示格式不一样产生的。你可以确认一下数据库是不是utf-8,如果不是可以用utf-8建立数据库connection连接的时候也可以指定格式。2,xml文件,jsp,html文件这个在文件的开头都可以指定编码格式3,java程序response.setContentType("text/html;charset=GB2312"); web.xml配置如下: <filter> <filter-name>characterEncoding</filter-name> <filter-class>com.test.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> //自己根据情况改成你需要的编码如GBK </init-param> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>以下为java代码package com.test.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class CharacterEncodingFilter implements Filter { private static Log log = LogFactory.getLog(CharacterEncodingFilter.class); protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; public void destroy() { this.encoding = null; this.filterConfig = null; } public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (ignore || (request.getCharacterEncoding() == null)) { String encoding = selectEncoding(request); if (encoding != null) request.setCharacterEncoding(encoding); } chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); String value = filterConfig.getInitParameter("ignore"); if (value == null) this.ignore = true; else if (value.equalsIgnoreCase("true")) this.ignore = true; else if (value.equalsIgnoreCase("yes")) this.ignore = true; else this.ignore = false; } protected String selectEncoding(ServletRequest request) { return (this.encoding); }} 使用时注意编码保持统一,从jsp->java->数据库,三者编码注意一致。 很简单啊,做个过滤器,在web.xml中配置一下 觉得好菜鸟的,求 给 计算器的 思路。 怎样统计一个字符串中哪个字符出现的频率最高 关于combo的modifyListener? 请教基本语法this.class.getClassLoader()怎么理解? 日期字符串转成日期型的问题 java有静态构造函数吗? 请问,com.ms.iis.asp这个类库在哪里下载啊? 学java 能做什么? 请问jbuilder6里光标错位问题如何解决 简单的tomcat 问题?请教,在线等候 关于Image 旋转的问题 看看这个数组的输出形状,如何分析它才能写出这样的数组
encodeURI
action里这样处理:
java.net.URLDecoder.decode(bankMsg, "UTF-8");
URL中处理不了的中文用String 改下编码
1,数据库
有些乱码问题是数据库的编码格式和画面显示格式不一样产生的。
你可以确认一下数据库是不是utf-8,如果不是可以用utf-8建立数据库
connection连接的时候也可以指定格式。2,xml文件,jsp,html文件
这个在文件的开头都可以指定编码格式3,java程序
response.setContentType("text/html;charset=GB2312");
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>com.test.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value> //自己根据情况改成你需要的编码如GBK
</init-param>
</filter> <filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
以下为java代码package com.test.filter;import java.io.IOException;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class CharacterEncodingFilter implements Filter {
private static Log log = LogFactory.getLog(CharacterEncodingFilter.class); protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true; public void destroy() { this.encoding = null;
this.filterConfig = null; } public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException { if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
} public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false; } protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}