html 中文亂碼 HTML超鏈接中文亂碼問題分析及解決方法
來源:易賢網 閱讀:1926 次 日期:2016-06-21 14:15:37
溫馨提示:易賢網小編為您整理了“html 中文亂碼 HTML超鏈接中文亂碼問題分析及解決方法”,方便廣大網友查閱!

Vm中一個超鏈接URL需要拼接中文作為Get請求的參數(shù)。如果直接拼接,傳到后臺Action的參數(shù)對象中后取出會是亂碼,需要編碼后再拼接到URL上。

解決方法是在Action中添加一個成員變量,保存編碼后的中文參數(shù)。在vm頁面渲染時取出這個變量值,再拼接超鏈接。

在這里碰到的問題是:調用java.net.URLEncoder的encode()方法時,如果沒有顯示指定字符集參數(shù),那么URLEncoder會使用默認字符集。這個默認字符集在Eclipse里跑main()方法和在Tomcat里跑Web應用,得到的結果不一樣,所以影響了編碼的結果。

代碼如下:

/**

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

}

方法的注釋中也說明了不建議使用的原因是,這個encode(String)方法依賴于平臺字符集。

更多信息請查看網頁制作

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯(lián)系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網