乐者为王

Do one thing, and do it well.

WebWork中如何实现i18n

ognl-2.6.5.jar + oscore-2.2.4.jar + webwork-2.1.7.jar + xwork-1.0.5.jar

web.xml配置文件的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
    <display-name>i18n</display-name>

    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <taglib>
        <taglib-uri>webwork</taglib-uri>
        <taglib-location>/WEB-INF/webwork.tld</taglib-location>
    </taglib>
</web-app>

webwork.properties配置文件:

1
2
3
4
5
6
7
### This can be used to set your locale and encoding scheme
#webwork.locale=en_US    <- 这行一定要注释掉,因为WebWork首先判断webwork.locale有没有被设置,
                            如有则始终以该locale为准,忽略浏览器的locale
webwork.i18n.encoding=utf-8

### Load custom default resource bundles
webwork.custom.i18n.resources=ApplicationResources

xwork.xml配置文件的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xwork PUBLIC
    "-//OpenSymphony Group//XWork 1.0//EN"
    "http://www.opensymphony.com/xwork/xwork-1.0.dtd">

<xwork>
    <include file="webwork-default.xml" />

    <package name="default" extends="webwork-default">
        <action name="hello" class="com.codemany.i18n.action.HelloAction">
            <result name="success" type="dispatcher">/i18n.jsp</result>
        </action>
    </package>
</xwork>

HelloAction.java实现代码:

1
2
3
4
5
6
7
8
import com.opensymphony.xwork.ActionSupport;

public class HelloAction extends ActionSupport {

    public String execute() throws Exception {
        return SUCCESS;
    }
}

i18n.jsp的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page pageEncoding="utf-8" contentType="text/html; charset=utf-8" %>

<%@ taglib uri="webwork" prefix="ww" %>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>
    <ww:property value="getText('i18n.value')" />
</body>
</html>

ApplicationResources_zh_CN.properties的内容:

1
i18n.value=国际化

ApplicationResources_en_US.properties的内容:

1
i18n.value=internationalization

Comments