1. 程式人生 > >struts2升級之後jsp到action傳值出現亂碼問題

struts2升級之後jsp到action傳值出現亂碼問題

web.xml中新增

 <filter>
   <filter-name>encodingFilter</filter-name>
   <filter-class>
   com.xinli.webapp.filter.EncodingConvertFilter 
   </filter-class>
</filter>
<filter-mapping>
   <filter-name>encodingFilter</filter-name>
   <url-pattern>/*</url-pattern> 
</filter
-mapping>

filter類:

package com.xinli.webapp.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;
import
javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 此編碼過濾器適用於解決struts2.1.6 * 前臺jsp頁面的中文傳入action出現亂碼 * * @author * */ public class EncodingConvertFilter implements Filter { @Override public void destroy() { } @Override public void doFilter
(ServletRequest req, ServletResponse rsp, FilterChain fc) throws IOException, ServletException { HttpServletRequest request=(HttpServletRequest) req; HttpServletResponse response=(HttpServletResponse) rsp; request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); fc.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { } }