目录

html 中文乱码 HTML超链接中文乱码问题分析及解决方法

2023-07-29         字体:【大 中 小】
在线播放列表

[!--onlinepath--]

    Vm中一个超链接URL需要拼接中文作为Get请求的参数。如果直接拼接,传到后台Action的参数对象中后取出会是乱码,需要编码后再拼接到URL上。

    解决方法是在Action中添加一个成员变量,保存编码后的中文参数。在vm页面渲染时取出这个变量值,再拼接超链接。

    在这里碰到的问题是:调用java.net.URLEncoder的encode()方法时,如果没有显示指定字符集参数,那么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)方法依赖于平台字符集。

    本文由建站免费素材圈sucaiq.com分享!!!

    如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对素材圈的支持!若你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

上哪儿去买?

万卷网购买

扫码关注公众号

【标题】html 中文乱码 HTML超链接中文乱码问题分析及解决方法
标签: 中文   乱码   解决   方法   分析   链接   HTML   html   问题
版权声明

该文观点仅代表作者本人,大秦号系信息发布平台,大秦仅提供信息存储空间服务。
本文系作者授权发表,未经许可,不得转载。

评论