springmvc前台传递到controller层的中文乱码解决方法

本文详细介绍了在处理前端传来的数据时遇到的乱码问题及其解决方案。通过使用正确的字符集转换方法,如将ISO-8859-1编码的数据转换为UTF-8编码,可以有效避免乱码现象,确保数据正确传输。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



   @RequestMapping("/judge")
    public String judgeLogger(@RequestParam String userName, @RequestParam String password, @RequestParam String sex, RedirectAttributes redirectAttributes) {
//    乱码解决方法
        try {
            String name = new String(userName.getBytes("iso-8859-1"), "UTF-8");
            String s = new String(sex.getBytes("iso-8859-1"), "utf-8");

//--------------------------------------------------------------------------------
if (name != null && password != null && s != null) { if (loggerService.judgeLogger(name)==1) { redirectAttributes.addFlashAttribute("userName",name); redirectAttributes.addFlashAttribute("password",password); redirectAttributes.addFlashAttribute("sex",s); return "redirect:/log.do"; } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); return "404"; } return "404"; }

 

在传递过来的前台数据进行一次转码即可解决。但是我之前设置过request和response的编码,但是然并卵。

 


 

 

转载于:https://www.cnblogs.com/dailinfu/p/6344680.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值