zoukankan      html  css  js  c++  java
  • 字符编码相关总结

    场景1:在html中定义表单submit_form,通过js中的$('#'+submit_form).ajaxSubmit提交表单,但是在后端收到表单提交的中文显示乱码。

    前段部分代码如下:test.html

    <form id="insterfestivals_form" method="post">
                    <br />
                    <p>
                        <label for="relation">所属人姓名:</label>
                        <input type="text" name="relation" id="relation"></input>
                    </p>
                    <p>
                        <label for="festival_type">&nbsp;节日类型:</label>
                        <input type="text" name="festival_type" id="festival_type"></input>
                    </p>
                    <p>
                        <label for="festival_ldate">&nbsp;&nbsp;&nbsp;农历:</label>
                        <input type="date" name="festival_ldate" id="festival_ldate"></input>
                    </p>
                    <p>
                        <label for="festival_ydate">&nbsp;&nbsp;&nbsp;阳历:</label>
                        <input type="date" name="festival_ydate" id="festival_ydate"></input>
                    </p>
                    <p>
                        <label for="remarks">&nbsp;&nbsp;&nbsp;寄语:</label>
                        <input type="text" name="remarks" id="remarks"></input>
                    </p>
                    <br />
                    <br />
                </form>
                <div id="but">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button id="insterfestivals">提交</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button id="reset">重置</button></div>
            

    后端java代码如下:test.java

    //添加登录用户所属成员的节日纪念日
        @RequestMapping(value="/insterfestivals",method=RequestMethod.GET,produces="text/html;charset=UTF-8")
        public  ModelAndView insterfestivals(@RequestParam("userid")String userid,String relation,String festival_type,String festival_ldate,String festival_ydate,String remarks)
        {
            System.out.println("userid:"+userid+";"+"relation:"+relation+";"+"festival_type:"+festival_type+";"+"festival_ldate:"+festival_ldate+";"+"festival_ydate:"+festival_ydate+";"+"remarks:"+remarks);
            //判断用户是否已将注册,后续再添加
            festival.setrelation_name(userid);
            festival.setrelation(relation);
            festival.setfestival_type(festival_type);
            //日期格式转换
            SimpleDateFormat dd=new SimpleDateFormat("yyyy-MM-dd");
            try
            {
                Date ldate=dd.parse(festival_ydate);
                Date ydate=dd.parse(festival_ydate);
                festival.setfestival_ldate(ldate);
                festival.setfestival_ydate(ydate);
            }catch(Exception e)
            {
                e.printStackTrace();
            }
            festival.setremarks(remarks);
            festivalServiceI.insertFestival(festival);
            
            ModelAndView insterfestivals = new ModelAndView("insterfestivals");
            insterfestivals.addObject("errCode", "0");
            return insterfestivals;//渲染视图名insterfestivals.jsp
        }

    解决方法:

    方法一:将get改为post,并在request_demo01.jsp中String content前面加一句:requst.setCharacterEncoding("GBK")。但是通过url提交的方法只能用get方法,所以第一中方法行不通。

    方法二:修改tomcat中Server.xml文件,在Connector节点中加入:useBodyEncodingForURI="true",如下:

    <Connector port="8080" 
    redirectPort="8443" acceptCount="100" 
    connectionTimeout="20000" useBodyEncodingForURI="true" />

    场景2:表单提交controller获得中文参数后乱码解决方案

    form表单提交方式为必须为post,get方式下面spring编码过滤器不起效果,只能参照本文(场景1

    修改web.xml,增加编码过滤器,如下(注意,需要设置forceEncoding参数值为true),代码如下:

    <filter>  
            <filter-name>characterEncodingFilter</filter-name>  
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
            <init-param>  
                <param-name>encoding</param-name>  
                <param-value>UTF-8</param-value>  
            </init-param>  
            <init-param>  
                <param-name>forceEncoding</param-name>  
                <param-value>true</param-value>  
            </init-param>  
        </filter>  
        <filter-mapping>  
            <filter-name>characterEncodingFilter</filter-name>  
            <url-pattern>/*</url-pattern>  
        </filter-mapping>  

    场景3:JS获取window.location.search地址中的参数值出现乱码

    url:http://localhost:8080/Spring_shizhan4ban_Chapter05/resources/js/insterfestivals.html?userid=%E8%B0%A2%E6%9C%9D%E8%BE%89

    实际在浏览器上看到的是:view-source:http://localhost:8080/Spring_shizhan4ban_Chapter05/resources/js/insterfestivals.html?userid=谢朝辉(gooler浏览器是GBK编码的),所以在获取地址参数中的值时需要转码。

    //js获取url中的参数值
    var data = {"userid":getQueryString("userid")}
    function getQueryString(name) 
    {
          var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
          var r = window.location.search.substr(1).match(reg);
          if (r != null) 
          {
            return decodeURI(r[2]);
          }
          return null;
    }
  • 相关阅读:
    springboot与docker
    Docker入门笔记(Centos7)
    记录VUE-CLI项目创建及初始化相关
    centos下安装mysql5.6
    GitLab权限介绍
    属性文件操作之Properties与ResourceBundle
    Shell入门基础
    JavaScript基础的记录
    Java基本排序算法
    解读闭包,这次从ECMAScript词法环境,执行上下文说起
  • 原文地址:https://www.cnblogs.com/zhabayi/p/6444492.html
Copyright © 2011-2022 走看看