Struts2默认对request进行了编码设置

直接贴Struts2的源码

按照执行流程

1.FilterDispacher类中:

  1. /** 
  2.       * Wrap and return the given request, if needed, so as to to transparently 
  3.       * handle multipart data as a wrapped class around the given request. 
  4.       * 
  5.       * @param request Our ServletRequest object 
  6.       * @param response Our ServerResponse object 
  7.       * @return Wrapped HttpServletRequest object 
  8.       * @throws ServletException on any error 
  9.       */  
  10.      protected HttpServletRequest prepareDispatcherAndWrapRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException {  
  11.         /* 
  12.          * 不明白,dispatcher实例是何时给本地线程的 
  13.          *  
  14.          * 经debug后,发现最初的dispatcher实例是在FilterDispatcher中的init方法已经初始化了 
  15.          */  
  16.          Dispatcher du = Dispatcher.getInstance();//每次的请求会是null:意味每次的本地线程都不同,因此会变成null  
  17.    
  18.          // Prepare and wrap the request if the cleanup filter hasn't already, cleanup filter should be  
  19.          // configured first before struts2 dispatcher filter, hence when its cleanup filter's turn,  
  20.          // static instance of Dispatcher should be null.  
  21.          if (du == null) {  
  22.               
  23.              Dispatcher.setInstance(dispatcher);  
  24.    
  25.                
  26.              // prepare the request no matter what - this ensures that the proper character encoding  
  27.              // is used before invoking the mapper (see WW-9127)  
  28.              /* 
  29.               * 对于该prepare方法:struts2默认设置了request的setCharacterEncoding()方法,姑且先 
  30.               * 不讨论设置成utf-8或者gbk会对获取客户端的参数有何种影响,我们首先肯定的是struts2对request 
  31.               * 进行了编码设置,因此我会觉得在struts2作为web框架中增加编码设置变得非常多余 
  32.               */  
  33.              dispatcher.prepare(request, response);  
  34.          } else {  
  35.              dispatcher = du;  
  36.          }  
  37.            
  38.          try {  
  39.              // Wrap request first, just in case it is multipart/form-data  
  40.              // parameters might not be accessible through before encoding (ww-1278)  
  41.              request = dispatcher.wrapRequest(request, getServletContext());  
  42.          } catch (IOException e) {  
  43.              String message = "Could not wrap servlet request with MultipartRequestWrapper!";  
  44.              LOG.error(message, e);  
  45.              throw new ServletException(message, e);  
  46.          }  
  47.    
  48.          return request;  
  49.      }  

2.Dispatcher类中:

  1. /** 
  2.      * Prepare a request, including setting the encoding and locale. 
  3.      * 
  4.      * @param request The request 
  5.      * @param response The response 
  6.      */  
  7.     public void prepare(HttpServletRequest request, HttpServletResponse response) {  
  8.         String encoding = null;  
  9.         //默认编码  
  10.         if (defaultEncoding != null) {  
  11.             encoding = defaultEncoding;  
  12.         }  
  13.   
  14.         Locale locale = null;  
  15.         if (defaultLocale != null) {  
  16.             locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());  
  17.         }  
  18.   
  19.         if (encoding != null) {  
  20.             try {  
  21.                 //设置默认编码  
  22.                 request.setCharacterEncoding(encoding);  
  23.             } catch (Exception e) {  
  24.                 LOG.error("Error setting character encoding to '" + encoding + "' - ignoring.", e);  
  25.             }  
  26.         }  
  27.   
  28.         if (locale != null) {  
  29.             response.setLocale(locale);  
  30.         }  
  31.   
  32.         if (paramsWorkaroundEnabled) {  
  33.             request.getParameter("foo"); // simply read any parameter (existing or not) to "prime" the request  
  34.         }  
  35.     }  

3.Dispatcher类中:

  1. public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext) throws IOException {  
  2.         // don't wrap more than once  
  3.         /* 
  4.          * 猜想疑惑:request的实例化使用sevlet容器进行初始化的, 
  5.          * 但是为何这边要出现if判断其属于那种request下的实例,StrutsRequestWrapper或者MultiPartRequestWrapper下 
  6.          * 那下面if如果要满足的话,前提必须进行了如下初始化 
  7.          *  猜想:HttpSevrletRequest request=new StrutsRequestWrapper(); 
  8.          * 那问题便出来了,如何进行request初始化,还是容器吗? 
  9.          * 每次请求都会初始化吗?他知道有StrutsRequestWrapper类吗? 
  10.          *  如果前面猜想不成立的话:那么if永远也进步下去 
  11.         */  
  12.         if (request instanceof StrutsRequestWrapper) {  
  13.             return request;  
  14.         }  
  15.         //由于contentype的不同会使用不同的解析方式对request中的信息进行解析,  
  16.         //故此进行判断  
  17.         String content_type = request.getContentType();  
  18.         if (content_type != null && content_type.indexOf("multipart/form-data") != -1) {  
  19.             //主要涉及到Container和实现类ContainerImpl的设计思路,待日后研究  
  20.             MultiPartRequest multi = getContainer().getInstance(MultiPartRequest.class);  
  21.             //这里包含了上传文件的解析流程,仔细看还是可以看懂的,具体问题具体研究吧  
  22.             request = new MultiPartRequestWrapper(multi, request, getSaveDir(servletContext));  
  23.         } else {  
  24.             //原来request在这里被struts2的request包裹类强奸了。尼玛。  
  25.             request = new StrutsRequestWrapper(request);  
  26.         }  
  27.   
  28.         return request;  
  29.     }  


如何更改struts2对于request的默认编码设置:<constant name="struts.i18n.encoding" value="utf-8/gbk" /> ps:默认情况下是utf-8

(struts2核心包下的default.properties中设置了默认编码值)

参考资料:http://blog.csdn.net/elimago/article/details/3601579


总结:

1.struts2默认对request进行了编码设置

2.Container和ContainerImpl实现类的设计准则待研究

3.对于上传文件的解析深入request = new MultiPartRequestWrapper(multi, request, getSaveDir(servletContext));待研究

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值