Jsp页面通过href拼接url向后台传中文乱码问题

需求是将jsp页面的中文内容通过url形式传给后台,若不经过编码转换直接传中文后台接收到的是乱码,想要获取到正确的数据就需要对中文字符进行编码转换,到后台以后在进行解码获取到正确的数据。

在jsp页面中,对汉字进行两次编码拼接到url中。

<a id=sendInfo href="#" onClick="javascript:location.href='${ctx}/good/goodBaseInfo/form?id=${goodBaseInfo.id}
&typeId=${tyId}${returnTypeId}&typeName='+encodeURI(encodeURI('${returnTypeName}'))+encodeURI(encodeURI('${tyName}'))+'&aId=${arId}${returnAreaId}&aName='+encodeURI(encodeURI('${returnAreaName}'))+encodeURI(encodeURI('${arName}'))">
        <img width="80px" height="80px" src="${ctxImage}/${goodBaseInfo.logoUrl}" />
</a>    

在后台controller中,导入import java.net.URLDecoder;包,采用URLDecoder.decode()进行解码。

try {
                tyname = URLDecoder.decode(goodBaseInfo.getTypeName(),"utf-8");
                arname = URLDecoder.decode(goodBaseInfo.getaName(),"utf-8");        
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
        model.addAttribute("typeName",tyname);
        model.addAttribute("areaName",arname);

猜你喜欢

转载自blog.csdn.net/tonyfreak/article/details/75271183