struts2开发页面偶尔中文乱码或者直接显示html,中文乱码时全部中文变成 ???chrome和firefox浏览器都会出
2个回答
展开全部
您好,感谢您对火狐的支持
如果数据库是乱码,导致读出来时候也是乱码。加上这段:
<%.@ page language="java"import="java.util."pageEncoding="ISO-8859-1">
您可以在火狐官方网站下载火狐浏览器,在火狐社区了解更多内容。希望我的回答对您有所帮助,如有疑问,欢迎继续在本平台咨询。
如果数据库是乱码,导致读出来时候也是乱码。加上这段:
<%.@ page language="java"import="java.util."pageEncoding="ISO-8859-1">
您可以在火狐官方网站下载火狐浏览器,在火狐社区了解更多内容。希望我的回答对您有所帮助,如有疑问,欢迎继续在本平台咨询。
展开全部
写个类叫EncodingFilter
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* 过滤器
* @author Administrator
*
*/
public class EncodingFilter implements Filter {
protected String encoding;
protected FilterConfig filterConfig;
public EncodingFilter() {
encoding = null;
filterConfig = null;
}
public void destroy() {
encoding = null;
filterConfig = null;
}
@SuppressWarnings("unchecked")
public void doFilter(ServletRequest servletrequest,
ServletResponse servletresponse, FilterChain filterchain)
throws IOException, ServletException {
String s = selectEncoding(servletrequest);
if (s != null) {
Map parameterMap = servletrequest.getParameterMap();
if (!parameterMap.isEmpty()) {
Iterator it = parameterMap.keySet().iterator();
String value[];
while (it.hasNext()) {
value = (String[]) parameterMap.get(it.next());
for (int i = 0; i < value.length; i++) {
value[i] = new String(value[i].getBytes("ISO-8859-1"),
encoding);
}
}
}
}
filterchain.doFilter(servletrequest, servletresponse);
}
public void init(FilterConfig filterconfig) throws ServletException {
filterConfig = filterconfig;
encoding = filterconfig.getInitParameter("encoding");
}
protected String selectEncoding(ServletRequest servletrequest) {
return encoding;
}
}
web.xml配置下
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>包名.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这个你没跟Spring集成自己写的
如果跟Spring集成直接用它的
在web.xml加入这个
<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>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
对了还有一个问题,就是你如果数据库是乱码,导致读出来时候也是乱码。
你在检查下,如果不是数据库问题,那就按上面说的做,应该就可以解决乱码问题
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* 过滤器
* @author Administrator
*
*/
public class EncodingFilter implements Filter {
protected String encoding;
protected FilterConfig filterConfig;
public EncodingFilter() {
encoding = null;
filterConfig = null;
}
public void destroy() {
encoding = null;
filterConfig = null;
}
@SuppressWarnings("unchecked")
public void doFilter(ServletRequest servletrequest,
ServletResponse servletresponse, FilterChain filterchain)
throws IOException, ServletException {
String s = selectEncoding(servletrequest);
if (s != null) {
Map parameterMap = servletrequest.getParameterMap();
if (!parameterMap.isEmpty()) {
Iterator it = parameterMap.keySet().iterator();
String value[];
while (it.hasNext()) {
value = (String[]) parameterMap.get(it.next());
for (int i = 0; i < value.length; i++) {
value[i] = new String(value[i].getBytes("ISO-8859-1"),
encoding);
}
}
}
}
filterchain.doFilter(servletrequest, servletresponse);
}
public void init(FilterConfig filterconfig) throws ServletException {
filterConfig = filterconfig;
encoding = filterconfig.getInitParameter("encoding");
}
protected String selectEncoding(ServletRequest servletrequest) {
return encoding;
}
}
web.xml配置下
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>包名.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这个你没跟Spring集成自己写的
如果跟Spring集成直接用它的
在web.xml加入这个
<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>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
对了还有一个问题,就是你如果数据库是乱码,导致读出来时候也是乱码。
你在检查下,如果不是数据库问题,那就按上面说的做,应该就可以解决乱码问题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询