JSP與struts2中的亂碼問題,解決方法很多,今天我又嘗試了一個新方法感覺不錯,超簡單
首先寫一個filter
[java]
public class SetCodeFilter implements Filter {
@Override
public void destroy() { }
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
req.setCharacterEncoding("UTF-8");
chain.doFilter(req, res);
}
@Override
public void init(FilterConfig arg0) throws ServletException { }
}
public class SetCodeFilter implements Filter {
@Override
public void destroy() { }
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
req.setCharacterEncoding("UTF-8");
chain.doFilter(req, res);
}
@Override
public void init(FilterConfig arg0) throws ServletException { }
}
在web.xml配置filter
[html]
<filter>
<filter-name>SetCodeFilter</filter-name>
<filter-class>com.shop.web.filter.SetCodeFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SetCodeFilter</filter-name>
<url-pattern>*.do</url-pattern>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter>
<filter-name>SetCodeFilter</filter-name>
<filter-class>com.shop.web.filter.SetCodeFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SetCodeFilter</filter-name>
<url-pattern>*.do</url-pattern>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>