Struts乱码问题解决方法
2009-01-07 17:22
232 查看
http://blog.flashtc.com/article.asp?id=112
web.xml中添加:
<filter>
<filter-name>sessionFilter</filter-name>
<filter-class>
com.zjcds.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>sessionFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sessionFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
类:SetCharacterEncodingFilter implements
package com.cds;
import java.io.IOException;
import java.util.Locale;
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.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts.Globals;
public class SetCharacterEncodingFilter implements Filter {
// ----------------------------------------------------- Instance Variables
/**
* The default character encoding to set for requests that pass through this
* filter.
*/
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value is
* null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- Public Methods
/**
* Take this filter out of service.
*/
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);
}
}
// Syncronize JSTL and struts locale settings
HttpSession session = ((HttpServletRequest) request).getSession(true);
Locale locale = (Locale) session.getAttribute(Globals.LOCALE_KEY);
if (locale == null) {
locale = request.getLocale();
}
// 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 Methods
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
web.xml中添加:
<filter>
<filter-name>sessionFilter</filter-name>
<filter-class>
com.zjcds.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>sessionFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sessionFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
类:SetCharacterEncodingFilter implements
package com.cds;
import java.io.IOException;
import java.util.Locale;
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.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts.Globals;
public class SetCharacterEncodingFilter implements Filter {
// ----------------------------------------------------- Instance Variables
/**
* The default character encoding to set for requests that pass through this
* filter.
*/
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value is
* null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- Public Methods
/**
* Take this filter out of service.
*/
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);
}
}
// Syncronize JSTL and struts locale settings
HttpSession session = ((HttpServletRequest) request).getSession(true);
Locale locale = (Locale) session.getAttribute(Globals.LOCALE_KEY);
if (locale == null) {
locale = request.getLocale();
}
// 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 Methods
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
相关文章推荐
- STRUTS+AJAX+JSP 请求到后台乱码问题解决方法
- linux+tomcat struts 2 Jsp页面传值中文乱码问题解决方法
- STRUTS乱码问题解决的最简单方法
- struts 上传文件乱码问题解决方法
- STRUTS+AJAX+JSP 请求到后台乱码问题解决方法
- SpringMVC Ajax response中文返回乱码问题的解决方法
- jsp中 ajax的get请求的中文乱码问题的解决方法
- struts控件取文件名乱码。一个不是很好的解决方法。
- 用ajax传递参数时遇到中文乱码问题的解决方法
- Python字符串的encode与decode研究心得乱码问题解决方法
- 关于利用Jsoup解析HTML中&nbsp;变成非传统空格或乱码问题解决方法
- Eclipse下开发Struts完整解决乱码问题
- 解决Tomcat中文乱码问题之多解决方法
- Tomcat中文乱码问题的原理和解决方法
- JDBC连接MySQL的乱码问题之解决方法
- CodeMan的Linux问题解决集锦:Linux打开阅读chm文件和解决chm中文乱码的方法
- Struts Invalid path was requested 问题分析及解决方法
- 解决JSP URL请求中文乱码问题方法
- sublime text 3中文乱码问题解决的方法
- php chr() ord()中文截取乱码问题解决方法