Structs中文乱码解决方法
来源:互联网 发布:linux怎么安装声卡 编辑:程序博客网 时间:2024/04/30 14:37
最近在学习Structs,很遗憾的遇到了许多前辈们都遇到过的中文乱码问题。上Google求助,得到如下几种结果。特记录下来,一一参研。
1、在tomcat目录下的server.xml配置文件中的<Connector>标签中添加属性URIEncoding,令URIEncoding="GBK";
这个方法比较简单,但必须得的系统管理员权限。
2、重载ActionServlet的process()解决structs中文乱码问题:
写一个myActionServlet来并覆盖ActionServlet中的process()方法。
protected void process(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException ...{
/** *//**@todo Override this org.apache.struts.action.ActionServlet method*/
request.setCharacterEncoding("GB2312");//就加着一行一切都解决了
super.process(request, response);
}
/** *//**@todo Override this org.apache.struts.action.ActionServlet method*/
request.setCharacterEncoding("GB2312");//就加着一行一切都解决了
super.process(request, response);
}
改一下web.xml里面的配置
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>strutsdemo.myActionServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-name>action</servlet-name>
<servlet-class>strutsdemo.myActionServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
在每个JSP页面中中3、使用filter解决中文乱码问题
<%...@ page contentType="text/html;charset=GB2312"%>
<%...@ page pageEncoding="GB2312"%><!--可加可不加-->
<%...@ page pageEncoding="GB2312"%><!--可加可不加-->
3、使用filter配置解决structs中文问题
其实structs的中文问题是tomcat的国际化问题的一类,可以参考tomcat给出的example解决。
在tomcat中webapp目录下的web.xml配置文件中存在如下代码:
第一段:关于filter的配置去掉注释后加入应用程序的Web.xml中
<!-- Example filter to set character encoding on each request -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>EUC_JP</param-value>
</init-param>
</filter>
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>EUC_JP</param-value>
</init-param>
</filter>
在我的应用程序中为了支持中文,将第一段改为:
<!-- Example filter to set character encoding on each request -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
第二段:filter应用范围的配置
<!-- Example filter mapping to apply the "Set Character Encoding" filter
to *all* requests processed by this web application -->
<!--
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->
to *all* requests processed by this web application -->
<!--
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->
在应用程序中添加SetCharacterEncodingFilter类,这个类在tomcat的示例程序中可以找到。
package filters;
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;
public class SetCharacterEncodingFilter implements Filter ...{
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
/** *//**
* Take this filter out of service.
*/
public void destroy() ...{
this.encoding = null;
this.filterConfig = null;
}
/** *//**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
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);
}
/** *//**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
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
/** *//**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
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;
public class SetCharacterEncodingFilter implements Filter ...{
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
/** *//**
* Take this filter out of service.
*/
public void destroy() ...{
this.encoding = null;
this.filterConfig = null;
}
/** *//**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
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);
}
/** *//**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
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
/** *//**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) ...{
return (this.encoding);
}
}
- Structs中文乱码解决方法
- Structs解决中文乱码
- structs中文乱码处理----filter
- 在structs中处理jsp页面的中文乱码问题
- JbuilderX中文乱码解决方法
- struts2中文乱码解决方法
- SQLyog中文乱码解决方法
- struts中文乱码解决方法
- phpMyAdmin中文乱码解决方法
- 中文乱码解决方法
- 中文乱码解决方法
- Struts2中文乱码解决方法
- Struts2中文乱码解决方法
- struts中文乱码解决方法
- mysql中文乱码解决方法
- struts2中文乱码解决方法
- struts2中文乱码解决方法
- 中文乱码解决方法
- 公钥和私钥
- 人才中介师的含金量
- What Is a Native Method?
- arp工作原理与缺陷
- Hiring a CFO in China
- Structs中文乱码解决方法
- 如何在中国招聘一个财务总监
- c# 日期函数
- 输入的命令集锦
- ARP协议工作原理及详细解码
- c#文件下载问题
- div+css - CSS标准中级联的文档
- 将IP Address Control控件内容写入struct in_addr变量
- 应对新《劳动合同法》 万名华为员工自选去留