显示界面,
JSP 2.0
JAVA
容器:
Tomcat 5.X
MVC
构架:
Struts1.2
ORM
工具:
Hibernate 3
数据库:
MySQL 5.0
编译
IDE
:
MyEclipse 5.0
知道了有六个地方要设定编码,那么哪种编码最好呢?使用中文可以
GBK
,但我认为
UTF-8
会是更好的选择,如果想知道了解编码的更多细节,请访问:
好现在选定
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
,确定即可。