Struts+Hibernate+MyEclipse+Tomcat+MySQL的乱码
来源:互联网 发布:中国上市装饰公司知乎 编辑:程序博客网 时间:2024/05/22 16:38
显示界面,JSP 2.0
JAVA容器:Tomcat 5.X
MVC构架:Struts1.2
ORM工具:Hibernate 3
数据库:MySQL 5.0
编译IDE:MyEclipse 5.0
知道了有六个地方要设定编码,那么哪种编码最好呢?使用中文可以GBK,但我认为UTF-8会是更好的选择,如果想知道了解编码的更多细节,请访问:
计算机编码大全-A
计算机编码大全-B
好现在选定UTF-8,在上面的这六个地方一一设定吧:
显示界面,JSP
在页面的开头加入或修改成:<%@ page language="java" pageEncoding="utf-8"%> 红色部分为要设定部分。
JAVA容器:Tomcat
本人参照某兄台的之作:Tomcat中文编码问题解决方案(简) 有兴趣可以进去看看。
下面开始设定,在Tomcat安装目录下的conf文件打开Server.xml文件,找到相应段落并更改成以下字样:
<Connector port="8080" maxThreads="150" minSpareThreads="25"
maxSpareThreads="75" enableLookups="false" redirectPort="8443"
acceptCount="100" debug="0" connectionTimeout="20000"
disableUploadTimeout="true" URIEncoding="utf-8"/>
红色部分为要设定部分。
MVC构架:Struts
在这里添加一个字符集的Fliter,代码如下:
package com.ruder.filters;
//上面的包名表示你把这个Fliter文件放在哪个包中,在下面的Web.xml也要相应改动。
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 javax.servlet.UnavailableException;
public class SetCharacterEncodingFilter implements Filter {
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 {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
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);
}
}
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 javax.servlet.UnavailableException;
public class SetCharacterEncodingFilter implements Filter {
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 {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
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);
}
}
下面是对Web.xml的配置:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.ruder.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这里更详细请参照:彻底解决Tomcat 5.0.19中文乱码
ORM工具:Hibernate 3
在这里比较简单一点,在配置文件Hibernate.cfg.xml中的连接数据库URL改成这样:
jdbc:mysql://数据库的主机名/数据库名?useUnicode=true&characterEncoding=UTF-8
红色部分为要设定的部分。
数据库:MySQL5.0
如果您未安装MySQL,请在安装MySQL时选择数据库编码为UTF-8; 如果已经安装,请在MySQL Server Instance Config Wizard里设定数据库编码。
编译IDE:MyEclipse 5.0
打开MyEclipse5.0 选择Window –Preferences—General—Content Types—Text
在选中相应的要改的项,在下面的Default encoding填写UTF-8,确定即可。
- Struts+Hibernate+MyEclipse+Tomcat+MySQL的乱码
- Struts+Hibernate+MyEclipse+Tomcat+MySQL的乱码之解决
- Struts+Hibernate+MyEclipse+Tomcat+MySQL的乱码之解决
- Struts+Hibernate+MyEclipse+Tomcat+MySQL的乱码之解决
- Struts+Hibernate+MyEclipse+Tomcat+MySQL的乱码之解决
- Myeclipse+hibernate+struts+Tomcat 例子
- MyEclipse+Struts+Hibernate+Mysql…
- hibernate+struts+mysql乱码问题
- hibernate+struts+mysql有关utf8乱码问题的解决。
- 解决Struts + Hibernate + MySql中文乱码的问题
- MyEclipse+Struts+Hibernate+Mysql开发环境配置
- MyEclipse+Struts+Hibernate+Mysql开发环境配置
- struts+hibernate+MySql中文乱码解决方案
- Struts+Hibernate+MySql 中文乱码问题解决方案
- struts + hibernate + MySql 中文乱码 解决方案
- Struts+Hibernate+MySql中文乱码解决方案
- 解决struts+hibernate+mysql乱码问题
- myeclipse 用hibernate mysql中文乱码
- 雪花背景网页特效
- 按钮来调整网页背景色
- 可替代VB自带的Timer控件的Timer类
- JavaScript: 使用面向对象的技术创建高级 Web 应用程序
- xml文件打包后如何引用实例
- Struts+Hibernate+MyEclipse+Tomcat+MySQL的乱码
- Singleton设计模式的C#实现(转载)
- 微软BizTalk服务使得SOA简单化
- Internet Service Bus
- 金额转换成大写(从Sql Server版转换成的C#版)
- VB中对string array快速插入、删除某个元素的办法
- SQL Server2000 解密存储过程
- 15 个最酷的 Firefox 技巧
- BizTalk Server 2006 Web 服务