您的位置:首页 > 编程语言 > Java开发

Struts2中的struts.i18n.encoding的较量

2016-05-07 16:27 344 查看
贴子地址: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 code
<?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 code
<?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,内容如下:

Java code
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";
}
}


第五步:在网站根目录下新建文件,内容如下:

HTML code
<%@ 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()方法,如下:

Java code
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()方法,代码如下:

Java code
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()方法,代码如下:

Java code
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中找到了如下代码:

Java code
@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”。其后果同样也会误导初学者。

javaeye站长robbin和javaeye的90%网友支持了ahuaxuan的观点说明了什么?这让我想起了当时我在传智播客培训java时,黎老师就告诉过我们“在计算机科学中,没有谁是权威,最权威的是计算机,结果就让计算机去说吧”,现在看来真的要自己多动手去验证。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: