strtus2中中文乱码怎么解决?我是通过From表单传值的
我找了好多答案都没有解决,如在tomcat配置,在Action配置request.setCharacterEncoding("utf-8");。strtus.xml也配置了<constant name="struts.i18n.encoding" value="utf-8" /> ,还有就是那个过滤器是什么?具体怎么配置 --------------------编程问答-------------------- 如果你是struts2,你就strtus.xml也配置了<constant name="struts.i18n.encoding" value="utf-8" />然后form表单全用post方式 --------------------编程问答-------------------- 如果用filter就在web.xml文件中<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>你filter的具易做图置</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
你的filter就这样写
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter implements Filter {
private String encoding = "utf-8";
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
filterChain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
--------------------编程问答-------------------- 如果你是get方式提交,那你就在tomcat的server.xml
中找到类似
<Connector
port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true"/>的标签
然后加上 URIEncoding="UTF-8" --------------------编程问答-------------------- 你filter的具易做图置怎么写啊,我在src的filter文件下里 建了你这个EncodingFilter。jav文件 --------------------编程问答-------------------- 你在src下面写就直接写你filter类名 --------------------编程问答-------------------- 用到spring了么 如果集成了spring 可以用spring带的filter --------------------编程问答--------------------
--------------------编程问答-------------------- 写个filter,配置到web.xml里,问题就解决了。 --------------------编程问答-------------------- 其实可以 用struts2.0的dispathFilter入口 就能解决中文 但是这个是被废弃了 不建议自己写filter
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
补充:Java , Java EE