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");
}