您现在的位置是:网站首页> 编程资料编程资料
html 中文乱码 HTML超链接中文乱码问题分析及解决方法_HTML/Xhtml_网页制作_
2021-09-13
1083人已围观
简介 Vm中一个超链接URL需要拼接中文作为Get请求的参数如果直接拼接,传到后台Action的参数对象中后取出会是乱码,需要编码后再拼接到URL上,接下来将和大家分享一下解决方法
Vm中一个超链接URL需要拼接中文作为Get请求的参数。如果直接拼接,传到后台Action的参数对象中后取出会是乱码,需要编码后再拼接到URL上。
解决方法是在Action中添加一个成员变量,保存编码后的中文参数。在vm页面渲染时取出这个变量值,再拼接超链接。
在这里碰到的问题是:调用java.net.URLEncoder的encode()方法时,如果没有显示指定字符集参数,那么URLEncoder会使用默认字符集。这个默认字符集在Eclipse里跑main()方法和在Tomcat里跑Web应用,得到的结果不一样,所以影响了编码的结果。
/**
* Translates a string into
* format. This method uses the platform'sdefault encoding
* as the encoding scheme to obtain thebytes for unsafe characters.
*
* @param s
* @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
*/
@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)方法依赖于平台字符集。
解决方法是在Action中添加一个成员变量,保存编码后的中文参数。在vm页面渲染时取出这个变量值,再拼接超链接。
在这里碰到的问题是:调用java.net.URLEncoder的encode()方法时,如果没有显示指定字符集参数,那么URLEncoder会使用默认字符集。这个默认字符集在Eclipse里跑main()方法和在Tomcat里跑Web应用,得到的结果不一样,所以影响了编码的结果。
复制代码
代码如下:/**
* 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;
}
方法的注释中也说明了不建议使用的原因是,这个encode(String)方法依赖于平台字符集。
相关内容
- HTML自学之旅(一)基本元素与属性练习(自写代码)_HTML/Xhtml_网页制作_
- 链接中的href=#是什么意思呢_HTML/Xhtml_网页制作_
- 网页制作中表格属性CellPad、CellSpace和Border描述及使用介绍_HTML/Xhtml_网页制作_
- input file自定义按钮美化(演示)_HTML/Xhtml_网页制作_
- 谈html mailto(电子邮件)实际应用_HTML/Xhtml_网页制作_
- this.parentNode.parentNode(父节点的父节点)是什么意思_HTML/Xhtml_网页制作_
- HTML相对路径(Relative Path)和绝对路径(Absolute Path)深入理解_HTML/Xhtml_网页制作_
- 表格(TABLE)标记(TAGS)详细介绍_HTML/Xhtml_网页制作_
- html 隐藏div HTML里隐藏表格TABLE或者DIV内容的css样式_HTML/Xhtml_网页制作_
- html text的常用事件和方法详细描述_HTML/Xhtml_网页制作_
