JSP与struts2中的乱码问题,解决方法很多,今天我又尝试了一个新方法感觉不错,超简单
首先写一个filter
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
<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>
OK,问题解决!