栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

解决表单post,get到springMVC后台乱码的问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解决表单post,get到springMVC后台乱码的问题

一. 前台表单直接post或get,后台以request.getParameterMap()或request.getParameter()的形式接收

注意:前台,springmvc过滤器,以及jsp,编译器编码全部以utf-8为例

以request.getParameterMap()为例

注意:这里是一个从HttpServletRequest中获取参数的方法,至于request则是从类似于

@RequestMapping("/tradePortal/ReceiveCommands") 
public ModelAndView receiveCommands(HttpServletRequest request, HttpServletResponse response){} 

这样的方法中获取

public static Map getParamters(HttpServletRequest request){
    logger.info("开始获取请求参数");
    Map paramterMap = request.getParameterMap();
    Map paraMap = new ConcurrentHashMap();
    for (Map.Entry entry :
 paramterMap.entrySet()) {
      if (Array.getLength(entry.getValue())<1||StringUtil.isEmpty(((String)(Array.get(entry.getValue(),0))))){
 paraMap.put((String)entry.getKey(),null);
      }else {
 if (request.getMethod().equalsIgnoreCase("get")){
   try {
     paraMap.put((String)entry.getKey(),new String(((String)(Array.get(entry.getValue(),0))).getBytes("ISO-8859-1"),"utf-8"));
   } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
   }
 }else {
   paraMap.put((String)entry.getKey(),(String)(Array.get(entry.getValue(),0)));
 }
      }
    }
    logger.info("参数获取完毕。参数为:"+ JSON.toJSonString(paraMap));
    return paraMap;
  }

以上这篇解决表单post,get到springMVC后台乱码的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/142158.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号