JavaEye与李刚就Struts2中的struts.i18n.encoding的较量

JavaEye与李刚的吵闹已经有半年了,最近就struts2中的struts.i18n.encoding参数用途又展开了口水战。

贴子地址:http://www.javaeye.com/topic/292062 (目前仍处于JavaEye网站的首页左边)

李刚说 struts.i18n.encoding对于处理中文请求参数非常有用。对于获取中文请求参数,应该将该属性设置未gbk活db2312,当该参数为gbk时,相当于调用HttpServletRequest的setCharacterEncoding()
ahuaxuan说 :struts.i18n.encoding是指定response中返回流的编码方式,明确指出struts.i18n.encoding参数没有用于HttpServletRequest的setCharacterEncoding()方法。

 

统计了一下:跟贴的人中有90%支持着ahuaxuan,其中还包括javaeye站长robbin。

 

实际上要判断struts.i18n.encoding的值有没有用于HttpServletRequest的setCharacterEncoding()方法很简单,我就用最简单的HelloWorld来说明。
首先给出步骤:
第一步: 新 建web项目,把struts2.0.14中lib目录下的struts2-core-2.0.14.jar、commons-logging- 1.0.4.jar、freemarker-2.3.8.jar、ognl-2.6.11.jar和xwork-2.0.7.jar拷贝到WEB- INF/lib目录下。
第二步: 在web.xml中配置如下内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee "
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
第三步: 在src目录下放入struts.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd ">
<struts>
    <constant name="struts.configuration.xml.reload" value="true"/>
    <constant name="struts.devMode" value="true"/>
    <constant name="struts.i18n.encoding" value="GBK"/>
   
   <package name="sohu" namespace="/test" extends="struts-default">
  <action name="hello" class="com.sohu.game.HelloAction">
   <result>/hello.jsp</result>  
  </action>
   </package>
</struts>
第四步: 新建com.sohu.game.HelloAction,内容如下:
package com.sohu.game;
import org.apache.struts2.ServletActionContext;
public class HelloAction {
 private String encoding;
 public String getEncoding() {
  return encoding;
 }
 public String execute() throws Exception{
  this.encoding = ServletActionContext.getRequest().getCharacterEncoding();
  return "success";
 }
}
第五步: 在网站根目录下新建文件,内容如下:
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>hello</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
  </head>
  <body><h3>
   request.getCharacterEncoding()的内容为:<s:property value="encoding"/>
  </h3></body>
</html>

测试步骤:
1>浏览http://localhost:8080/ 项目的内容路径/test/hello.action,查看网页输出内容为:
request.getCharacterEncoding()的内容为:GBK
2>修改struts.xml文件中的struts.i18n.encoding="UTF-8",刷新网页,然后查看网页输出内容为:
request.getCharacterEncoding()的内容为:UTF-8
3>再修改struts.xml文件中的struts.i18n.encoding="gb2312",刷新网页,然后查看网页输出内容为:
request.getCharacterEncoding()的内容为:gb2312

上面结果说明了什么?说明了struts.i18n.encoding确实用于了HttpServletRequest的setCharacterEncoding()方法。

 

有人可能还不相信,好!我们就来解剖一下struts2.0.14的源代码,首先从 org.apache.struts2.dispatcher.FilterDispatcher入手,我们知道当请求到来时,会执行 Filter.doFilter()方法,那么我们打开org.apache.struts2.dispatcher.FilterDispatcher 的源码,找到doFilter()方法,如下:

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
 HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        ServletContext servletContext = getServletContext();
        String timerKey = "FilterDispatcher_doFilter: ";
        try {
            UtilTimerStack.push(timerKey);
            request = prepareDispatcherAndWrapRequest(request, response);//这里是重点
     //这里省略后面的代码
        } finally {
            try {
                ActionContextCleanUp.cleanUp(req);
            } finally {
                UtilTimerStack.pop(timerKey);
            }
        }
    }
}

我们可以看到,在doFilter()方法里面调用了prepareDispatcherAndWrapRequest()方法,展开prepareDispatcherAndWrapRequest()方法,代码如下:
protected HttpServletRequest prepareDispatcherAndWrapRequest(HttpServletRequest request, HttpServletResponse response) throws

ServletException {
        Dispatcher du = Dispatcher.getInstance();
        if (du == null) {
            Dispatcher.setInstance(dispatcher);
            dispatcher.prepare(request, response);//这里是重点
        } else {
            dispatcher = du;
        }
        //省略了一些代码
        return request;
}

在上面的prepareDispatcherAndWrapRequest()方法里面调用了org.apache.struts2.dispatcher.Dispatcher的prepare()方法,再展开prepare()方法,代码如下:
public void prepare(HttpServletRequest request, HttpServletResponse response) {
        String encoding = null;
        if (defaultEncoding != null) {
            encoding = defaultEncoding;
        }
      //省略了一些代码
        if (encoding != null) {
            try {
                request.setCharacterEncoding(encoding);//这里是重点
            } catch (Exception e) {
                LOG.error("Error setting character encoding to '" + encoding + "' - ignoring.", e);
            }
        }
        //省略了一些代码
}
在 上面的prepare()里调用了request.setCharacterEncoding(encoding),encoding的值是 defaultEncoding赋予的,我们看看defaultEncoding的值倒底是什么?在 org.apache.struts2.dispatcher.Dispatcher中找到了如下代码:
@Inject(StrutsConstants.STRUTS_I18N_ENCODING)
public static void setDefaultEncoding(String val) {
    defaultEncoding = val;
}
上面代码的含义是把struts.i18n.encoding的值赋给了defaultEncoding. 由此可见struts.i18n.encoding确实用于了HttpServletRequest的setCharacterEncoding()方法。

struts.i18n.encoding参数是用于设置默认的本地编码,不仅仅用于setCharacterEncoding()方法,在struts框架中如果需要使用默认的本地编码就会使用到该值。

 

李刚说struts.i18n.encoding用于了request.setCharacterEncoding()方法没错,ahuaxuan 的代码说明也没有错,但由于ahuaxuan的偏见,片面的实验结果,使的其冲晕了头脑,并放出“struts.i18n.encoding只用于 response中返回流的编码方式,转而攻击他人分不清Request/Response”。其后果同样也会误导初学者。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值