struts 显示中文问题

chendonghr 2008-09-01 01:51:16
我用STRUTS框架做了个论坛,在后台输入数据,然后在前台就可以显示了,输入英文时一切正常,但是输入中文,前台就会出现乱码,但是在MYSQL里却还是显示中文,请教各位怎么解决
...全文
69 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhbwuj 2008-09-01
  • 打赏
  • 举报
回复
写一个编码过滤器,懒的话就复制tomcat自带的就可以,要配置一下web.xml

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.cn.nst.filter.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>
jianyi296 2008-09-01
  • 打赏
  • 举报
回复
你把提交方式改下,把get改为post试下。
hanbb 2008-09-01
  • 打赏
  • 举报
回复
乱码问题,在页面中设定编码,不如"GBk"

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧