zoukankan      html  css  js  c++  java
  • struts2乱码问题

    简介:做了个功能,用的struts2,表单提交到后台,接收后打印出来的数据乱码。
     

    解决步骤:

    1. struts.xml中配置<constant name="struts.i18n.encoding" value="utf-8" />

        结果:乱码

    2. web.xml中配置CharsetEncodingFilter过滤器

        结果:还是乱码

    3. jsp页面编码 pageEncoding="UTF-8"

        结果:仍然乱码

    4. <form>标签里加 method="post"

        结果:正常显示

     

    网上一些见解:

    get方式乱码还是post乱码?post乱码可以使用过滤器解决,get方式一般要进行URLEncode,或者改tomcat配置。
    <Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
        enableLookups="false" redirectPort="8443" acceptCount="100"
        debug="0" connectionTimeout="20000"
        disableUploadTimeout="true" URIEncoding="GBK" />

    本来的URLEncoding="UTF-8"是没有的,加上这一句话,就行了,下面是为什么这么做的原因:

    <constant name="struts.i18n.encoding" value="UTF-8" /> 表示把struts2设置为utf-8,相当于response.setCharacterEncoding("UTF-8"),对HTTP请求的数据进行编码,但是get请求的数据是直接在URL中,通过配置struts2配置为utf-8或CharacterEncodingFilter拦截器都不会对URL进行拦截并转换。对于很多人来说会觉得用 request.setCharacterEncoding("字符集")可以指定解码方式,其实是不可以的,当看了servlet的官方API说明有对此方法的解释:

    Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader().

    可以看出对于get方法它是无能为力的。那么到底用什么编码方式解码数据的呢,这是tomcat的事情了,默认缺省用的是 iso-8859-1 所以出现了上面的配置方式。 当然了还的一种:

          new String(request.getParameter("name").getBytes("iso-8859-1"),"客户端指定的URL encode编码方式")

    总之,对于post来说,乱码问题是很容易解决的,关键在于get,因为所有页面上的编码设置对于get方法是彻底无效的。

     

  • 相关阅读:
    131. Palindrome Partitioning
    130. Surrounded Regions
    129. Sum Root to Leaf Numbers
    128. Longest Consecutive Sequence
    125. Valid Palindrome
    124. Binary Tree Maximum Path Sum
    122. Best Time to Buy and Sell Stock II
    121. Best Time to Buy and Sell Stock
    120. Triangle
    119. Pascal's Triangle II
  • 原文地址:https://www.cnblogs.com/xtreme/p/4626967.html
Copyright © 2011-2022 走看看