html 中文亂碼 HTML超鏈接中文亂碼問(wèn)題分析及解決方法
發(fā)布時(shí)間:2012-12-30 16:54:52 作者:佚名
我要評(píng)論

Vm中一個(gè)超鏈接URL需要拼接中文作為Get請(qǐng)求的參數(shù)如果直接拼接,傳到后臺(tái)Action的參數(shù)對(duì)象中后取出會(huì)是亂碼,需要編碼后再拼接到URL上,接下來(lái)將和大家分享一下解決方法
Vm中一個(gè)超鏈接URL需要拼接中文作為Get請(qǐng)求的參數(shù)。如果直接拼接,傳到后臺(tái)Action的參數(shù)對(duì)象中后取出會(huì)是亂碼,需要編碼后再拼接到URL上。
解決方法是在Action中添加一個(gè)成員變量,保存編碼后的中文參數(shù)。在vm頁(yè)面渲染時(shí)取出這個(gè)變量值,再拼接超鏈接。
在這里碰到的問(wèn)題是:調(diào)用java.net.URLEncoder的encode()方法時(shí),如果沒(méi)有顯示指定字符集參數(shù),那么URLEncoder會(huì)使用默認(rèn)字符集。這個(gè)默認(rèn)字符集在Eclipse里跑main()方法和在Tomcat里跑Web應(yīng)用,得到的結(jié)果不一樣,所以影響了編碼的結(jié)果。
/**
* Translates a string into <code>x-www-form-urlencoded</code>
* format. This method uses the platform'sdefault encoding
* as the encoding scheme to obtain thebytes for unsafe characters.
*
* @param s <code>String</code> to betranslated.
* @deprecated The resulting string mayvary depending on the platform's
* default encoding. Instead, use theencode(String,String)
* method to specify the encoding.
* @return the translated <code>String</code>.
*/
@Deprecated
public static String encode(String s) {
String str = null;
try {
str = encode(s, dfltEncName);
} catch(UnsupportedEncodingException e) {
// The system should always have theplatform default
}
return str;
}
方法的注釋中也說(shuō)明了不建議使用的原因是,這個(gè)encode(String)方法依賴于平臺(tái)字符集。
解決方法是在Action中添加一個(gè)成員變量,保存編碼后的中文參數(shù)。在vm頁(yè)面渲染時(shí)取出這個(gè)變量值,再拼接超鏈接。
在這里碰到的問(wèn)題是:調(diào)用java.net.URLEncoder的encode()方法時(shí),如果沒(méi)有顯示指定字符集參數(shù),那么URLEncoder會(huì)使用默認(rèn)字符集。這個(gè)默認(rèn)字符集在Eclipse里跑main()方法和在Tomcat里跑Web應(yīng)用,得到的結(jié)果不一樣,所以影響了編碼的結(jié)果。
復(fù)制代碼
代碼如下:/**
* Translates a string into <code>x-www-form-urlencoded</code>
* format. This method uses the platform'sdefault encoding
* as the encoding scheme to obtain thebytes for unsafe characters.
*
* @param s <code>String</code> to betranslated.
* @deprecated The resulting string mayvary depending on the platform's
* default encoding. Instead, use theencode(String,String)
* method to specify the encoding.
* @return the translated <code>String</code>.
*/
@Deprecated
public static String encode(String s) {
String str = null;
try {
str = encode(s, dfltEncName);
} catch(UnsupportedEncodingException e) {
// The system should always have theplatform default
}
return str;
}
方法的注釋中也說(shuō)明了不建議使用的原因是,這個(gè)encode(String)方法依賴于平臺(tái)字符集。
相關(guān)文章
HTML5在IE10、火狐下中文亂碼問(wèn)題的解決方法
寫(xiě)HTML5代碼時(shí)發(fā)現(xiàn)中文顯示亂碼,之前以為是不兼容IE10、然后換成火狐,誰(shuí)知也不行,經(jīng)反復(fù)研究測(cè)試,尋得個(gè)不錯(cuò)的解決方法,有類(lèi)似情況的朋友可以參考下2013-11-18- HTML5 UTF-8出現(xiàn)中文亂碼的情況,應(yīng)該有很多的朋友都有遇到吧,用記事本寫(xiě),保存后在網(wǎng)頁(yè)上運(yùn)行出現(xiàn)了亂碼,換成GB2312能正確顯示中文,下面就為大家探討下具體的解決方法2013-11-18
html文件的中文亂碼問(wèn)題與在瀏覽器中的顯示問(wèn)題
我今天的頁(yè)面也是出現(xiàn)了亂碼,所以在網(wǎng)上查找了相關(guān)的問(wèn)題,看來(lái)一下,這個(gè)方法挺不錯(cuò)的,我也試驗(yàn)了呢,我用的editp編輯器,在文檔—文件編碼—更改文件編碼—選擇自己所2013-02-26關(guān)于HTML編碼導(dǎo)致的亂碼問(wèn)題
今天遇到個(gè)問(wèn)題就是寫(xiě)的HTML代碼打開(kāi)顯示的是亂碼問(wèn)題,怎么處理這一問(wèn)題呢,下面小編通過(guò)圖文實(shí)例相結(jié)合給大家分享處理方法,一起看看吧2021-09-02