|
1.首先jsp頁面要使用UTF-8編碼,個人建議將pageEncoding與contentType中的編碼全部設(shè)置為UTF-8 2.修改tomcat配置文件server.xml,添加一個屬性[紅色部分],如下 <Connector 3.編寫過濾器類 1. public class SetCharacterEncodingFilter implements Filter { 2. 3. protected String encoding = null; 4. 5. protected FilterConfig filterConfig = null; 6. 7. protected boolean ignore = true; 8. 9. /* 10. * (non-Javadoc) 11. * 12. * @see javax.servlet.Filter#destroy() 13. */ 14. public void destroy() { 15. 16. this.encoding = null; 17. this.filterConfig = null; 18. 19. } 20. 21. /* 22. * (non-Javadoc) 23. * 24. * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, 25. * javax.servlet.ServletResponse, javax.servlet.FilterChain) 26. */ 27. public void doFilter(ServletRequest request, ServletResponse response, 28. FilterChain chain) throws IOException, ServletException { 29. 30. // Conditionally select and set the character encoding to be used 31. if (ignore || (request.getCharacterEncoding() == null)) { 32. String encoding = selectEncoding(request); 33. if (encoding != null) 34. request.setCharacterEncoding(encoding); 35. } 36. 37. // Pass control on to the next filter 38. chain.doFilter(request, response); 39. } 40. 41. /* 42. * (non-Javadoc) 43. * 44. * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) 45. */ 46. public void init(FilterConfig filterConfig) throws ServletException { 47. 48. this.filterConfig = filterConfig; 49. this.encoding = filterConfig.getInitParameter("encoding"); 50. String value = filterConfig.getInitParameter("ignore"); 51. if (value == null) 52. this.ignore = true; 53. else if (value.equalsIgnoreCase("true")) 54. this.ignore = true; 55. else if (value.equalsIgnoreCase("yes")) 56. this.ignore = true; 57. else 58. this.ignore = false; 59. 60. } 61. 62. /* 63. * 64. */ 65. protected String selectEncoding(ServletRequest request) { 66. 67. return (this.encoding); 68. 69. } 70. 71. } public class SetCharacterEncodingFilter implements Filter { protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; /* * (non-Javadoc) * * @see javax.servlet.Filter#destroy() */ public void destroy() { this.encoding = null; this.filterConfig = null; } /* * (non-Javadoc) * * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, * javax.servlet.ServletResponse, javax.servlet.FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // Conditionally select and set the character encoding to be used if (ignore || (request.getCharacterEncoding() == null)) { String encoding = selectEncoding(request); if (encoding != null) request.setCharacterEncoding(encoding); } // Pass control on to the next filter chain.doFilter(request, response); } /* * (non-Javadoc) * * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) */ public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); String value = filterConfig.getInitParameter("ignore"); if (value == null) this.ignore = true; else if (value.equalsIgnoreCase("true")) this.ignore = true; else if (value.equalsIgnoreCase("yes")) this.ignore = true; else this.ignore = false; } /* * */ protected String selectEncoding(ServletRequest request) { return (this.encoding); } } 4.在web.xml中添加下列代碼 1. <filter> 2. <filter-name>Encoding</filter-name> 3. <filter-class> 4. struts.util.SetCharacterEncodingFilter 5. </filter-class> 6. <init-param> 7. <param-name>encoding</param-name> 8. <param-value>UTF-8</param-value> 9. </init-param> 10. </filter> 11. <filter-mapping> 12. <filter-name>Encoding</filter-name> 13. <url-pattern>/*</url-pattern> 14. </filter-mapping> 看了一下樓主鏈接中的filter代碼,應(yīng)該沒問題的。 如果不加filter,頁面采用utf-8,的確會在java代碼中出現(xiàn)中文亂碼的情況。 然后我就寫了一個filter,大致跟鏈接中的內(nèi)容一致,然后就解決了亂碼問題。 我貼一下我的代碼吧 web.xml代碼 <filter> <filter-name>charactarFileter</filter-name> <filter-class>com.yourcompany.struts.CharactarEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>charactarFileter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> filter實現(xiàn)代碼 package com.yourcompany.struts; 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 CharactarEncodingFilter implements Filter{ public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("utf-8"); chain.doFilter(request, response); } public void init(FilterConfig arg0) throws ServletException { } } 就這樣,寫好后重新發(fā)布項目,運行,解決問題。 1.修改Tomcat中conf/server.xml文件中 在 <Connector |
|
|