解决Struts乱码问题报错。只有这么多分了,解决后立刻给分
解决Struts乱码问题报错。
我的解决方法是:
1.所有页面都定义
<%@ page language= "java " contentType= "text/html;charset=UTF-8 " %>
2.配置Servelet的过滤器
新建一个类SetCharacterEncodingFilter.java
package com.vivian8117.struts;
import
java.io.IOException;
import javax.servlet.FilterChain;
import
javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import com.sun.org.apache.xalan.internal.xsltc.dom.Filter;
public class SetCharacterEncodingFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws
IOException,
ServletException {
request.setCharacterEncoding( "UTF-8 ");
// 传递控制到下一个过滤器
chain.doFilter(request, response);
}
public boolean test(int node) {
// TODO Auto-generated method stub
return false;
}
}
3配置web.xml 加上下面这段就报错,不加就没问题
<filter>
<filter-name> Set Character Encoding </filter-name>
<filter-class> com.vivian8117.struts.SetCharacterEncodingFilter </filter-class>
</filter>
<filter-mapping>
<filter-name> Set Character Encoding </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
4.打开tomcat的server.xml文件,找到区块,加入如下一行:
URIEncoding= "UTF-8 "
完整的应如下:
<Connector port= "8080 " maxThreads= "150 " minSpareThreads= "25 " maxSpareThreads= "75 " enableLookups= "false " redirectPort= "8443 " acceptCount= "100 " debug= "0 " connectionTimeout= "20000 " disableUploadTimeout= "true " URIEncoding= "UTF-8 "/>
我的web.xml是
<?xml version= "1.0 " encoding= "UTF-8 "?>
<web-app xmlns= "http://java.sun.com/xml/ns/j2ee " xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance " version= "2.4 " xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
<display-name> vivian8117 </display-name>
<filter>
<filter-name> Set Character Encoding </filter-name>
<filter-class> com.vivian8117.struts.SetCharacterEncodingFilter </filter-class>
</filter>
<filter-mapping>
<filter-name> Set Character Encoding </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
<servlet>
<servlet-name> action </servlet-name>
<servlet-class> org.apache.struts.action.ActionServlet </servlet-class>
<init-param>
<par