出家如初,成佛有余

Struts2中Datetimepicker控件的中文问题

Posted in Uncategorized by chuanliang on 2008/03/30

为了与现有数据库字符集GBK统一,在Struts2中采用GBK作为缺省的编码方式,与此对应的编码相关的配置:

1、JVM中缺省字符集

    或者在环境变量中配置JAVA_OPTS=%JAVA_OPT%;-Dfile.encoding=GBK -Duser.language=zh_CN
    或者在Tomcat的启动脚本中配置JAVA_OPTS=%JAVA_OPT%;-Dfile.encoding=GBK -Duser.language=zh_CN

2、Tomcat的URIEncoding ,处理GET方式的乱码问题

在server.xml中配置:

<Connector port="8080" maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               connectionTimeout="20000" disableUploadTimeout="true"
               URIEncoding="GBK"

3、Struts2的Locale配置

在Struts.xml中配置
    <constant name=”struts.locale” value=”zh_CN” />
    <constant name=”struts.i18n.encoding” value=”GBK” />

4、采用Spring的过滤器对POST页面编码

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>
        org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>GBK</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

5、页面统一编码为GBK

在common/meta.jsp中

<meta http-equiv=”Cache-Control” content=”no-store”/>
<!– HTTP 1.0 –>
<meta http-equiv=”Pragma” content=”no-cache”/>
<!– Prevents caching at the Proxy Server –>
<meta http-equiv=”Expires” content=”0″/>
<meta http-equiv=”Content-Type” content=”text/html; charset=GBK”/>
<meta name=”generator” content=”Mobile-soft.cn” />
<meta http-equiv=”keywords” content=”mobile,payment,telecommunication,internet”>
<meta http-equiv=”description” content=”mobile-soft”>
同时在各页面中包含meta.jsp页面及设定pageEncoding:

<%@ include file=”/common/meta.jsp” %>

<%@ page language=”java” errorPage=”/error.jsp” pageEncoding=”GBK” contentType=”text/html;charset=GBK” %>

6、数据库编码

数据库建库时候字符集编码采用GBK

在applicationContext-resources.xml中,mysql的配置

    <bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource”
        destroy-method=”close”>
        <property name=”driverClassName” value=”com.mysql.jdbc.Driver”/>
        <property name=”url”
            value=”jdbc:mysql://localhost/mysql?useUnicode=true&amp;characterEncoding=gbk”/>
        <property name=”username” value=”root”/>
        <property name=”password” value=””/>
        <property name=”maxActive” value=”100″/>
        <property name=”maxIdle” value=”30″/>
        <property name=”maxWait” value=”1000″/>
        <property name=”defaultAutoCommit” value=”true”/>
        <property name=”removeAbandoned” value=”true”/>
        <property name=”removeAbandonedTimeout” value=”60″/>
        <property name=”validationQuery” value=”SELECT 1″/>
        <property name=”testOnBorrow” value=”true”/>
    </bean>

    采用以上步骤基本上搞定了乱码问题,但在使用Struts2的datetimepicker控件时候出现了乱码问题,Struts2的javascript标签库缺省是采用庞大的dojo库(为何不采用jquery这样清爽的javascript库),因此怀疑是dojo的i18n问题,解决步骤如下:

1、修改struts.serve.static的缺省配置

修改struts.mxl,增加如下内容。

<constant name=”struts.serve.static” value=”false” />

在struts2-core-2.0.11.jar/org/apache/struts2/default.properties中,struts.serve.static缺省值为true,也即从struts2-core-2.0.11.jar查找Struts2的静态文件,这参数的命名太晦涩。

### Used by FilterDispatcher
### If true then Struts serves static content from inside its jar.
### If false then the static content must be available at <context_path>/struts
struts.serve.static=true

注意这里的<context_path>/struts实际指的就是jsp页面所在目录。

2、覆盖缺省的静态文件

    在resource目录(与WEB-INF同级或WEB-INF下)创建struts目录,并:

    解压struts2-core-2.0.11.jar:/org/apache/struts2/static/ to /struts/

    解压struts2-core-2.0.11.jar:/template/simple/dojoRequire.js to /struts/simple/

    解压struts2-core-2.0.11.jar:/template/xhtml/styles.css to /struts/xhtml/

    解压struts2-core-2.0.11.jar:/template/xhtml/validation.js to /struts/xhtml/

    解压struts2-core-2.0.11.jar:/template/css_xhtml/styles.css to /struts/css_xhtml/

    解压struts2-core-2.0.11.jar:/template/css_xhtml/validation.js to /struts/css_xhtml/

    解压struts2-core-2.0.11.jar:/template/ajax/dojoRequire.js to /struts/ajax/

    解压struts2-core-2.0.11.jar:/template/ajax/validation.js to /struts/ajax/

    注意:以上所说的解压并不是把整个包都解压,只是把需要的内容勇winrar直接拽出来,对于struts2-core-2.0.11.jar包还是保持原状,不要做任何改动,以方便后期的升级。

3、修改js文件的编码方式

    用记事本打开struts/dojo/src/i18n/calendar/nls/zh/gregorian.js并以ASSI格式另存为gregorian.js

    用记事本打开struts/dojo/src/i18n/calendar/nls/zh/gregorianExtras.js并以ASSI格式另存为gregorianExtras.js

    用记事本打开struts/dojo/src/i18n/calendar/nls/zh-cn/gregorian.js并以ASSI格式另存为gregorian.js

  以上几个文件,原来的格式是UTF-8的

4、页面实例

<%@ taglib prefix=”s” uri=”/struts-tags” %>
<%@ page language=”java” errorPage=”/error.jsp” pageEncoding=”GBK” contentType=”text/html;charset=GBK” %>
<html>
<head>

 <title>tag list</title>

 <%@ include file=”/common/meta.jsp” %>

 <s:head/>

</head>
<body>
 <s:form name=”form1″ action=”test2″>
 <s:datetimepicker name=”start_date” language=”zh-cn”  label=”计费开始时间” displayFormat=”yyyy-MM-dd” />
 <s:submit/>
</s:form>

</body>
</html>

注意这里language为zh-cn(也可以为zh),而不是zh_CN,这与java中不同。dojo官方在Internationalization (i18n)文档中强调:

Locale

dojo.locale

The locale is a short string, defined by the host environment, which conforms to RFC 3066 used in the HTML specification. It consists of short identifiers, typically two characters long which are case-insensitive. Note that Dojo uses dash separators, not underscores like Java (e.g. “en-us”, not “en_US”). Typically country codes are used in the optional second identifier, and additional variants may be specified. For example, Japanese is “ja”; Japanese in Japan is “ja-jp”. Notice that the lower case is intentional — while Dojo will often convert all locales to lowercase to normalize them, it is the lowercase that must be used when defining your resources.

The locale in the browser is typically set during install and is not easily configurable. Note that this is not the same locale in the preferences dialog which can be used to accompany HTTP requests; there is unfortunately no way to access that locale from the client without a server round-trip.

The locale Dojo uses on a page may be overridden by setting djConfig.locale. This may be done to accomodate applications with a known user profile or server pages which do manual assembly and assume a certain locale. You may also set djConfig.extraLocale to load localizations in addition to your own, in case you want to specify a particular translation or have multiple languages appear on your page.

5、参考文档

http://cwiki.apache.org/S2WIKI/creating-a-custom-dojo-profile-for-struts-20x.html

http://dojo.jot.com/InternationalizationOverview

http://cwiki.apache.org/WW/datetimepicker.html

 

No Responses Yet

Subscribe to comments with RSS.

  1. […]    项目采用UTF-8作为统一的编码(JVM中缺省字符集、Tomcat的URIEncoding字符集、Struts2的Locale字符集配置、过滤器字符集、页面统一编码、数据库编码、mysql数据库连接字符集等),基本上排除了日常开发的中文乱码问题,具体可以参考Struts2中Datetimepicker控件的中文问题。 […]

  2. […] JVM、Tomcat、数据库等相关的字符集(参考Struts2中Datetimepicker控件的中文问题):UTF-8 […]


发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: