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”。其後果同樣也會誤導初學者。