tapestry 中文問題徹底解決(tomcat)

1. tomcat 中conf下的server.xml中設置爲UTF-8

2. 設置html編碼,可在*.application中添加

   <meta key="org.apache.tapestry.template-encoding">
          UTF-8      
    </meta> 

  即可設置tapestry全部頁面編碼爲UTF-8

3.在web.xml中設置filter

 在web.xml中添加

 <filter>
   <filter-name>encodingFilter</filter-name>
   <filter-class>org.xiaohongli.authmanager.filter.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>

  附EncodingFilter.java

 
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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EncodingFilter extends HttpServlet implements Filter {

 private static final long serialVersionUID = 1L;
    private FilterConfig filterConfig;
    
     public void init(FilterConfig filterConfig) throws ServletException {
         this.filterConfig = filterConfig;
     }
  
     public void doFilter(ServletRequest request, ServletResponse response,
                          FilterChain filterChain) {
         try {
          //         從WEB.xml配置文件中取出參數                 
          String encoding=filterConfig.getInitParameter("encoding");
             //        設置請求的編碼格式
             request.setCharacterEncoding(encoding);
             filterChain.doFilter(request, response);
         } catch (ServletException sx) {
             filterConfig.getServletContext().log(sx.getMessage());
         } catch (IOException iox) {
             filterConfig.getServletContext().log(iox.getMessage());
         }
     }

     public void destroy() {
     }

  protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
   super.doGet(arg0, arg1); 
  }

  protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
   super.doPost(arg0, arg1);  
  }
}

 總之所有編碼設爲UTF-8,中文問題就不會產生。因爲UTF-8編碼兼容所有其他的編碼,當然也包括gb2312,gbk

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章