html超链接中⽂,HTML超链接中⽂乱码
Vm中⼀个超链接URL需要拼接中⽂作为Get请求的参数。如果直接拼接,传到后台Action的参数对象中后取出会是乱码,需要编码后再拼接到URL上。
解决⽅法是在Action中添加⼀个成员变量,保存编码后的中⽂参数。在vm页⾯渲染时取出这个变量值,再拼接超链接。
在这⾥碰到的问题是:调⽤java.URLEncoder的encode()⽅法时,如果没有显⽰指定字符集参数,那么URLEncoder会使⽤默认字符集。这个默认字符集在Eclipse⾥跑main()⽅法和在Tomcat⾥跑Web应⽤,得到的结果不⼀样,所以影响了编码的结果。
[java]
/**
* Translates a string into x-www-form-urlencoded
* format. This method uses the platform'sdefault encoding
* as the encoding scheme to obtain thebytes for unsafe characters.
*
* @param  s  String 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 String.
*/
@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;
}
/**
* Translates a string into x-www-form-urlencoded
* format. This method uses the platform'sdefault encoding
* as the encoding scheme to obtain thebytes for unsafe characters.
url编码处理*
* @param  s  String 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 String.
*/
@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)⽅法依赖于平台字符集。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。