在web.xml中加上
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.huahang.tj.struts.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
这里会涉及一个Filter,源码如下:
import javax.servlet.*;
import java.io.IOException;
.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 {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
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);
}
加上这个后,在action中就可以直接从form中接收gb2312编码的数据了,返回时自然也是gb2312了
分享到:
相关推荐
struts2配置过滤器 示例代码
Struts如何解决中文添加到数据库
struts核心过滤器FilterDispatcher介绍,struts核心过滤器FilterDispatcher介绍
Struts 2 过滤器的总结
tomcat、struts2、servlet Filter(过滤器) 工作原理 学习servlet的好资料,懂得工作原理,更容易理解问题.
Struts 2 过滤器和拦截器的区别
中文乱码问题很头疼,但一个好的过滤器就可以解决问题啦
STRUTS:filter过滤器
Struts2编写的通用的禁止用户非法访问非登录页面以外的其他页面的拦截器,过滤器,程序附带运行说明和可执行文件,希望给您的工作或学习带来帮助
结合struts2编写的通用过滤器filter,内附有word文档操作详解,配置十分方便,曾经做ssh项目的时候用于拦截浏览器地址栏非法登录,用在非法登录拦截这一块非常适合,经过测试没有问题。
页面开发时经常会出现中文乱码问题,需要我们对编码进行转换。手工转换很麻烦,写个过滤器就好多了。
乱码解决方案和过滤器的解决与用户的登陆error解决 乱码解决方案和过滤器的解决与用户的登陆error解决
NULL 博文链接:https://hongyegu.iteye.com/blog/765001
这是一个不错的例子,使用过滤器解决了字符乱码的问题
struts中文乱码问题解决详细步骤(两种方法)
使用Struts实现了向MySQL数据库中存储图片的功能。有一些问题没能解决:中文乱码问题,上传图片不能太大等等。虽然不太完善,作为参考还是可以的
struts2技巧,笔记.zip 指定404,500页面 utf-8过滤器 防SQL注入 解决乱码
JSP,Servlet,Struts2,struts1,数据库乱码处理
这是一个struts2实例 涉及的技术包括: 1.自定义过滤器 2.文件上传下载 3.登录功能 等等
struts2 实现oracle数据库的增删改查