sitemesh 装饰html 乱码解决
在网上搜了一整天的资料最后还是没有找到解决办法,网上那些方法,我可能没有弄对,反正就是照着弄了,就是弄不出来。第二天突然灵感一来,自己想了办法解决了。方法如下:
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{
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=UTF-8");
filterChain.doFilter(servletRequest, servletResponse);
}
public void init(FilterConfig arg0) throws ServletException {
System.setProperty("file.encoding","utf-8");
}
public void destroy() {
}
}
注意标红的两句,把上面那个filter配置在xml里面:
<filter>
<filter-name>loggerfilter</filter-name>
<filter-class>wu.sys.cores.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loggerfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如果还不能成功,可能是浏览器缓存,自己清空一下缓存。
补充:web前端 , HTML/CSS ,