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;
    	}




  • 相关阅读:
    LINQ -2015-04-27
    wireshark的安装
    c#中的classes和objects一些知识【1】
    初学C#,用vs去开始hello world!
    file_get_contents HTTP request failed! Internal Server Error
    验证码二(验证码使用)
    接口调用 POST
    接口调用 GET方式
    百度地图改标注样式
    Linux-常用命令
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5233527.html
Copyright © 2011-2022 走看看