struts2 编码方式配置:
1。采用自定义的方法。写个类继承 org.apache.struts2.dispatcher.FilterDispatcher
public class NewFilterDispatcher extends FilterDispatcher {
private static String encoding = "UTF-8";
public void init(FilterConfig filterConfig) throws ServletException {
super.init(filterConfig);
String encodingParam = filterConfig.getInitParameter("encoding");
if (encodingParam != null && encodingParam.trim().length() != 0) {
encoding = encodingParam;
}
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
super.doFilter(request, response, chain);
}
}
<filter>
<filter-name>struts</filter-name>
<!-- 自定义类 -->
<filter-class>
com.zh.test.filter.NewFilterDispatcher
</filter-class>
<init-param>
<!-- 编码方式 -->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2。另外,还可以直接在web.xml中配置sping的编码支持。
<filter>
<filter-name>setCharacterEncoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!-- filtered type -->
<filter-mapping>
<filter-name>setCharacterEncoding</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
当用第一种方法时,在tomcat6 启动默认工程时,会产生xml解析错误。也许是我犯的错误吧。不过tomcat6 jdk6 就是出现了问题。
现在的情况是等项目做完时,想改动代码编码方式时,根本就不能修改的。没有办法了。
参考文件:
http://www.iocblog.net/static/2007/587.html