在学习struts中,遇到了中文乱码问题,网上搜索了一些资料,解决了问题
使用get方法时:需要修改 $TOMCAT/conf/server.xml部署文件,在connectior属性中添加URIEncoding="GBK"即可
使用post方法时,有2种方法可选
1) 覆写ActionServlet的process方法,添加request.setEncoding("GBK");
2) 添加一个过滤器SetCharacterEncodingFilter类,此类可以在$TOMCAT/webapps/servlets-examples/WEB-INF/classes/filters找到,不过方便起见,自己加了点东西,源码如下:
public class SetCharacterEncodingFilter implements Filter {
private FilterConfig filterConfig;
public void destroy()
{
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException
{
// TODO Auto-generated method stub
try{
request.setCharacterEncoding("GB2312");
HttpServletResponse res = (HttpServletResponse)response;
res.setHeader("Pragma", "No-cache");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Pragram", "no-cache");
filterChain.doFilter(request, response);
}catch(ServletException e)
{
filterConfig.getServletContext().log(e.getMessage());
}catch(IOException e)
{
filterConfig.getServletContext().log(e.getMessage());
}
}
public void init(FilterConfig filterConfig) throws ServletException
{
// TODO Auto-generated method stub
this.filterConfig = filterConfig;
}
}
在web.xml中部署这个过滤器
<filter>
<filter-name>servfilter</filter-name>
<filter-class>com.yourcompany.struts.SetCharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>servfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
关于国际化问题,我曾经请教过老师,不过老师好像不是这个方向的
后来才发现是一个很愚蠢的问题,要求是ApplicationResources_zh_CN.properties或ApplicationResources_zh.properties 而我却用了ApplicationResources_cn.properties
切记CN是大写的
native2ascii -encoding gb2312 application_temp.properties application_CN.properties
native2ascii可以在$JAVA_HOME/bin下找到
Trackback: http://tb.blog.youkuaiyun.com/TrackBack.aspx?PostId=676002