web網頁中文變成亂碼

碰到一個問題,在web頁面上面中文變成亂碼,也不是所有中文,只是在jscript文件中的中文都變成了亂碼:
當時解決步驟比較凌亂,也不知道哪個是真正生效,不過問題是解決了。
記錄如下
1.
tomcat:server.xml
<Connector port="10005" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="18443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>
2.
.bashrc
LANG=zh_CN.UTF-8
LC_CTYPE=zh_CN.UTF-8
LC_NUMERIC=en_US.UTF-8
LC_TIME=en_US.UTF-8
LC_COLLATE=zh_CN.UTF-8
LC_MONETARY=en_US.UTF-8
LC_MESSAGES=zh_CN.UTF-8
LC_PAPER=en_US.UTF-8
LC_NAME=en_US.UTF-8
LC_ADDRESS=en_US.UTF-8
LC_TELEPHONE=en_US.UTF-8
LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION=en_US.UTF-8
LC_ALL=
3.
web.xml
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<init-param>
<param-name>fileEncoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
4.
web.xml
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>com.deepblue.backend.management.filter.CharsetFilter</filter-class>
<init-param>
<param-name>requestEncoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

public class CharsetFilter implements Filter {

private String encoding;

public void init(FilterConfig config) throws ServletException {
encoding = config.getInitParameter("requestEncoding");
if (encoding == null) encoding = "UTF-8";
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain next)
throws IOException, ServletException {
// Respect the client-specified character encoding
// (see HTTP specification section 3.4.1)
if (null == request.getCharacterEncoding()) {
request.setCharacterEncoding(encoding);
}

// Set the default response content type and encoding
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");

next.doFilter(request, response);
}

public void destroy() {
}
}
5. 記得別忘記強制刷新網頁
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章