zoukankan      html  css  js  c++  java
  • [Java Web]Struts2解决中文乱码问题

    1.设置struts的字符编码,能够在struts.xml中添加下面代码:

    <constant name="struts.i18n.encoding" value="GBK" />

    或者找到struts的默认配置文件,位置在 struts2-core-2.3.16.3.jar 里面 org.apache.struts2 包中的 default.properties 文件。改动下面配置:

    ### This can be used to set your default locale and encoding scheme
    # struts.locale=en_US
    struts.i18n.encoding=GBK


    2.设置JSP页面字符编码:

    <%@ page language="java" contentType="text/html; charset=GBK"
        pageEncoding="GBK"%>


    3.假设以上方法还无法解决乱码,则有可能是由于tomcat在接受get请求时默认使用ISO-8859-1编码,须要找到 apache-tomcat-6.0.39/conf 下的 web.xml 文件。改动例如以下代码:

    <Connector URIEncoding="GBK" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" />

    如此3步就可以解决大部分Stuts2中文乱码问题,如实在无法解决,仅仅能手动用Java改动编码了。方法例如以下:

    public static String changeEncoding(String s, String oldEncoding, String newEncoding) {
    		String result = null;
    		try {
    			result = new String(s.getBytes(oldEncoding), newEncoding);
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		}
    		return result;
    	}




  • 相关阅读:
    VS2015编译OpenSSL1.0.2源码
    VS2015编译CURL7.54.0源码
    Mac OS Yosemite 文件批量重命名
    https 原理
    把本地仓库导入到Github
    jquery cdn加速注意事项
    关于CSS 里的_width是什么意思???
    HTML的footer置于页面最底部的方法
    html-include
    GitHub Permission to <<repository>> denied to <<username>>
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5233527.html
Copyright © 2011-2022 走看看