java GET方式传递中文参数乱码问题

java  GET方式传递中文参数乱码需要用到 java.net.URLDecoder和 java.net.URLEncoder这两个类里面的decode("变量","UTF-8")和encode("中文","UFP-8"),这两个方法前一个是解码后一个是转码。注意:转码时要转两次,解码需要一次就能够解决中文参数乱码问题。URL传递参数时服务器会自动解码一次,然后我们拿到参数后再解码一次,所以转码要转两次。

 

java后台代码:

传递中文参数代码
    @RequestMapping("/clearActive")
    public ModelAndView deleteActive(HttpServletRequest request, PageInfo page, ActiveAlarmInfo actives)
    {
        try
        {
            //从request范围内获取告警类型和告警原因,如果获取的属性不为空则进行转码并放到类对象中
            String alarmType = URLEncoder.encode(URLEncoder.encode("告警类型", "UTF-8"), "UTF-8");
            actives.setAlarmType(alarmType);
         
            String probableCause = URLEncoder.encode(URLEncoder.encode("告警原因", "UTF-8"), "UTF-8");
            actives.setProbableCause(probableCause);
       
            //从request范围内获取activeID,并根据activeID获取ActiveAlarmInfo信息
            String activeID = request.getParameter("activeID");
            ActiveAlarmInfo active = activeService.getOneActiveByActiveID(activeID);
           
            if (null != active)
            {
                //给notificationType赋值
                active.setNotificationType("ClearedAlarm");
               
                //调用delete方法
                activeService.deleteActiveByActiveID(activeID, active);
            }
            else
            {
                request.setAttribute("result", Global.FAIL);
            }
        }
        catch (Exception e)
        {
            logger.error(Global.LOG_EXCEPTION_NAME, e);
        }
        String url =
            "/active/getAllActiveInfo?alarmType=" + actives.getAlarmType()
                + "&perceivedSeverity=" + actives.getPerceivedSeverity() + "&probableCause=" + actives.getProbableCause();
 
        return new ModelAndView(new RedirectView(request.getContextPath() + url));
    }
 
 接收中文参数代码
 @RequestMapping("/getAllActiveInfo")
    public ModelAndView getAllActiveInfo(HttpServletRequest request, HttpServletResponse response,
        ActiveAlarmInfo active, PageInfo page)
    {
        try
        {
            //从request范围内获取告警类型和告警原因,如果获取的属性不为空则进行转码并放到类对象中
            if (null != request.getParameter("alarmType") && !"".equals(request.getParameter("alarmType")))
            {
                String alarmType = URLDecoder.decode(request.getParameter("alarmType"), "UTF-8");
                active.setAlarmType(alarmType);
            }
            if (null != request.getParameter("probableCause") && !"".equals(request.getParameter("probableCause")))
            {
                String probableCause = URLDecoder.decode(request.getParameter("probableCause"), "UTF-8");
                active.setProbableCause(probableCause);
            }
           
            //获取所有告警配置信息和基站信息
            List<AlarmInfoConfig> alarmList = forService.getAlarmInfo();
            List<HnbInfo> hnbList = hnbService.getHnbList();
           
            //C权限的用户可以获取所有的活跃告警信息,B权限的用户可以获取告警级别严重的所有告警,A权限用户只能获取网管断链的告警
            List<ActiveAlarmInfo> activeList = null;
            if (null != roleBank && null != active)
            {
                activeList = activeService.getAllActiveInfo(active, page);
            }
           
            //把对象放到request范围内
            request.setAttribute("hnbList", hnbList);
            request.setAttribute("alarmList", alarmList);
            request.setAttribute("activeList", activeList);
            request.setAttribute("active", active);
            request.setAttribute("page", page);
        }
        catch (Exception e)
        {
            logger.error(Global.LOG_EXCEPTION_NAME);
        }
        return new ModelAndView("warning/active_List");
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值