当前位置: 代码迷 >> 综合 >> Struts(14)本地化/国际化(i18n)
  详细解决方案

Struts(14)本地化/国际化(i18n)

热度:65   发布时间:2024-01-10 02:31:10.0
国际化(i18n)是规划和实施的产品和服务,使他们能很容易地适应特定的本地语言和文化的过程中,这个过程被称为本地化。国际化的过程有时也被称为翻译或本地化启用。国际化是缩写i18n,因为我和两端用n字打头,并有18个字符之间的第i个和最后n。

Struts2提供本地化,即,国际化(i18n)支持,通过资源包,拦截器和标签库在以下地方:

  • UI 标签

  • 消息和错误

  • 动作类

资源包:

Struts2 使用资源包来提供Web应用程序的用户多语言和区域选项。不必担心在不同的语言编写的网页。所有必须做的是创造一个资源包为每个想要的语言。资源包将包含标题,消息和其他文本的语言用户。资源包的文件,该文件包含键/值对您的应用程序的默认语言。

简单的命名格式的资源文件是:

<span class="pln" style="margin: 0px; padding: 0px;">bundlename_language_country</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">properties</span>

这里,软件包可以ActionClass,接口,超类,型号,封装,全球资源属性。接下来的部分 language_country ,En_US的等在这里,可以跳过这是可选的全国部分区域表示es_ES和英语(美国),西班牙语(西班牙)表示语言环境的语言环境,例如代表国家。 

当引用消息元素,其关键,按照下列顺序进行相应的消息包的Struts框架搜索:

  • ActionClass.properties

  • Interface.properties

  • SuperClass.properties

  • model.properties

  • package.properties

  • struts.properties

  • global.properties

多语言开发应用程序,就必须保持相应的到那些语言/区域设置多个属性文件定义的键/值对中的所有内容。例如,如果要开发应用程序(默认)为美国英语,西班牙语,和法语就必须创建三个属性文件。在这里,我将使用只global.properties文件,你可以利用不同的属性文件来隔离不同类型的消息。

  • global.properties: 默认情况下,英语(美国)将被应用

  • global_fr.properties: 这将是法语环境中使用。

  • global_es.properties: 这将被用于西班牙语言环境。

访问消息:

有几种方法可以访问的信息资源,包括gettext的,文本标签,UI标签的关键属性,国际化标签。让我们来看看他们简单:

要显示i18n的文本,使用的调用属性标记gettext,或其他任何标记,例如UI标签如下:

<span class="tag" style="margin: 0px; padding: 0px;"><s:property</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"getText('some.key')"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span>

文本标记检索从默认的资源包,即一个消息 struts.properties

<span class="tag" style="margin: 0px; padding: 0px;"><s:text</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"some.key"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span>

i18n标签推值栈上的任意资源束。 i18n标签范围内的其他标签可以显示该资源包的消息:

<span class="tag" style="margin: 0px; padding: 0px;"><s:i18n</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"some.package.bundle"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><s:text</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"some.key"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></s:i18n></span>

大多数UI标签的键属性,可以用来检索的消息,从一个资源包:

<span class="tag" style="margin: 0px; padding: 0px;"><s:textfield</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">key</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"some.key"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"textfieldName"</span><span class="tag" style="margin: 0px; padding: 0px;">/></span>

Localization 例子:

创建的index.jsp从前一章到多种语言。相同的文件将被写入,如下所示:

<span class="pun" style="margin: 0px; padding: 0px;"><%@</span><span class="pln" style="margin: 0px; padding: 0px;"> page language</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"java"</span><span class="pln" style="margin: 0px; padding: 0px;"> contentType</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"text/html; charset=ISO-8859-1"</span><span class="pln" style="margin: 0px; padding: 0px;">pageEncoding</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"ISO-8859-1"</span><span class="pln" style="margin: 0px; padding: 0px;">%>
</span><span class="pun" style="margin: 0px; padding: 0px;"><%@</span><span class="pln" style="margin: 0px; padding: 0px;"> taglib prefix</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"s"</span><span class="pln" style="margin: 0px; padding: 0px;"> uri</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"/struts-tags"</span><span class="pln" style="margin: 0px; padding: 0px;">%>
</span><span class="dec" style="margin: 0px; padding: 0px;"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><html></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><head></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><title></span><span class="pln" style="margin: 0px; padding: 0px;">Employee Form with Multilingual Support</span><span class="tag" style="margin: 0px; padding: 0px;"></title></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></head></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><body></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><h1><s:text</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"global.heading"</span><span class="tag" style="margin: 0px; padding: 0px;">/></h1></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><s:url</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">id</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"indexEN"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">namespace</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"/"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">action</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"locale"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><s:param</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"request_locale"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">en</span><span class="tag" style="margin: 0px; padding: 0px;"></s:param></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"></s:url></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><s:url</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">id</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"indexES"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">namespace</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"/"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">action</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"locale"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><s:param</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"request_locale"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">es</span><span class="tag" style="margin: 0px; padding: 0px;"></s:param></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"></s:url></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><s:url</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">id</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"indexFR"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">namespace</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"/"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">action</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"locale"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><s:param</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"request_locale"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">fr</span><span class="tag" style="margin: 0px; padding: 0px;"></s:param></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"></s:url></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><s:a</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">href</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"%{indexEN}"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">English</span><span class="tag" style="margin: 0px; padding: 0px;"></s:a></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><s:a</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">href</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"%{indexES}"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">Spanish</span><span class="tag" style="margin: 0px; padding: 0px;"></s:a></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><s:a</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">href</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"%{indexFR}"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">France</span><span class="tag" style="margin: 0px; padding: 0px;"></s:a></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><s:form</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">action</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"empinfo"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">method</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"post"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">namespace</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"/"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><s:textfield</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"name"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">key</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"global.name"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">size</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"20"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><s:textfield</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"age"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">key</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"global.age"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">size</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"20"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><s:submit</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"submit"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">key</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"global.submit"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"></s:form></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"></body></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></html></span>

我们将创建的success.jsp文件,该文件将被调用的情况下定义的动作返回SUCCESS。

<span class="pun" style="margin: 0px; padding: 0px;"><%@</span><span class="pln" style="margin: 0px; padding: 0px;"> page language</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"java"</span><span class="pln" style="margin: 0px; padding: 0px;"> contentType</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"text/html; charset=ISO-8859-1"</span><span class="pln" style="margin: 0px; padding: 0px;">pageEncoding</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"ISO-8859-1"</span><span class="pln" style="margin: 0px; padding: 0px;">%>
</span><span class="pun" style="margin: 0px; padding: 0px;"><%@</span><span class="pln" style="margin: 0px; padding: 0px;"> taglib prefix</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"s"</span><span class="pln" style="margin: 0px; padding: 0px;"> uri</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"/struts-tags"</span><span class="pln" style="margin: 0px; padding: 0px;">%>
</span><span class="dec" style="margin: 0px; padding: 0px;"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><html></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><head></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><title></span><span class="pln" style="margin: 0px; padding: 0px;">Success</span><span class="tag" style="margin: 0px; padding: 0px;"></title></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></head></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><body></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><s:property</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"getText('global.success')"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></body></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></html></span>

在这里,我们需要创建两个动作。 (一)第一个动作一个Locale和照顾,用不同的语言显示相同的index.jsp文件(二)另一项行动是为了照顾提交表单本身。的动作都将返回SUCCESS,但我们会采取不同的动作,返回值的基础上,因为我们的目的是不同的两个动作:

动作处理locale:

<span class="kwd" style="margin: 0px; padding: 0px;">package</span><span class="pln" style="margin: 0px; padding: 0px;"> com</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">yiibai</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">struts2</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">import</span><span class="pln" style="margin: 0px; padding: 0px;"> com</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">opensymphony</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">xwork2</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="typ" style="margin: 0px; padding: 0px;">ActionSupport</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">class</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Locale</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">extends</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">ActionSupport</span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> execute</span><span class="pun" style="margin: 0px; padding: 0px;">()</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">return</span><span class="pln" style="margin: 0px; padding: 0px;"> SUCCESS</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="pun" style="margin: 0px; padding: 0px;">}</span>

提交表单处理动作:

<span class="kwd" style="margin: 0px; padding: 0px;">package</span><span class="pln" style="margin: 0px; padding: 0px;"> com</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">yiibai</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">struts2</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">import</span><span class="pln" style="margin: 0px; padding: 0px;"> com</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">opensymphony</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">xwork2</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="typ" style="margin: 0px; padding: 0px;">ActionSupport</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">class</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Employee</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">extends</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">ActionSupport</span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">private</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">private</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">int</span><span class="pln" style="margin: 0px; padding: 0px;"> age</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> execute</span><span class="pun" style="margin: 0px; padding: 0px;">()</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">return</span><span class="pln" style="margin: 0px; padding: 0px;"> SUCCESS</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> getName</span><span class="pun" style="margin: 0px; padding: 0px;">()</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">return</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">void</span><span class="pln" style="margin: 0px; padding: 0px;"> setName</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="typ" style="margin: 0px; padding: 0px;">String</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">)</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">this</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">name </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> name</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">int</span><span class="pln" style="margin: 0px; padding: 0px;"> getAge</span><span class="pun" style="margin: 0px; padding: 0px;">()</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">return</span><span class="pln" style="margin: 0px; padding: 0px;"> age</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">public</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="kwd" style="margin: 0px; padding: 0px;">void</span><span class="pln" style="margin: 0px; padding: 0px;"> setAge</span><span class="pun" style="margin: 0px; padding: 0px;">(</span><span class="kwd" style="margin: 0px; padding: 0px;">int</span><span class="pln" style="margin: 0px; padding: 0px;"> age</span><span class="pun" style="margin: 0px; padding: 0px;">)</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="kwd" style="margin: 0px; padding: 0px;">this</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">age </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> age</span><span class="pun" style="margin: 0px; padding: 0px;">;</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="pun" style="margin: 0px; padding: 0px;">}</span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="pun" style="margin: 0px; padding: 0px;">}</span>

现在。让我们创建以下三个global.properties文件放在CLASSPATH中:

GLOBAL.PROPERTIES:

<span class="kwd" style="margin: 0px; padding: 0px;">global</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">name </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Name</span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="kwd" style="margin: 0px; padding: 0px;">global</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">age </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Age</span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="kwd" style="margin: 0px; padding: 0px;">global</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">submit </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Submit</span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="kwd" style="margin: 0px; padding: 0px;">global</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">heading </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Select</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Locale</span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="kwd" style="margin: 0px; padding: 0px;">global</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">success </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Successfully</span><span class="pln" style="margin: 0px; padding: 0px;"> authenticated</span>

GLOBAL_FR.PROPERTIES:

<span class="kwd" style="margin: 0px; padding: 0px;">global</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">name </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Nom</span><span class="pln" style="margin: 0px; padding: 0px;"> d</span><span class="str" style="margin: 0px; padding: 0px;">'utilisateur 
global.age = l'</span><span class="pun" style="margin: 0px; padding: 0px;">?</span><span class="pln" style="margin: 0px; padding: 0px;">ge
</span><span class="kwd" style="margin: 0px; padding: 0px;">global</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">submit </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Soumettre</span><span class="pln" style="margin: 0px; padding: 0px;"> des
</span><span class="kwd" style="margin: 0px; padding: 0px;">global</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">heading </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> S</span><span class="pun" style="margin: 0px; padding: 0px;">é</span><span class="pln" style="margin: 0px; padding: 0px;"> lectionnez </span><span class="typ" style="margin: 0px; padding: 0px;">Local</span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="kwd" style="margin: 0px; padding: 0px;">global</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">success </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Authentifi</span><span class="pln" style="margin: 0px; padding: 0px;">	</span><span class="pun" style="margin: 0px; padding: 0px;">é</span><span class="pln" style="margin: 0px; padding: 0px;">  avec succ</span><span class="pun" style="margin: 0px; padding: 0px;">è</span><span class="pln" style="margin: 0px; padding: 0px;">s</span>

GLOBAL_ES.PROPERTIES:

<span class="kwd" style="margin: 0px; padding: 0px;">global</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">name </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Nombre</span><span class="pln" style="margin: 0px; padding: 0px;"> de usuario
</span><span class="kwd" style="margin: 0px; padding: 0px;">global</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">age </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Edad</span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="kwd" style="margin: 0px; padding: 0px;">global</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">submit </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Presentar</span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="kwd" style="margin: 0px; padding: 0px;">global</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">heading </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> seleccionar la configuracion regional
</span><span class="kwd" style="margin: 0px; padding: 0px;">global</span><span class="pun" style="margin: 0px; padding: 0px;">.</span><span class="pln" style="margin: 0px; padding: 0px;">success </span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="typ" style="margin: 0px; padding: 0px;">Autenticado</span><span class="pln" style="margin: 0px; padding: 0px;"> correctamente</span>

我们将创建struts.xml中两个动作如下:

<span class="pun" style="margin: 0px; padding: 0px;"><?</span><span class="pln" style="margin: 0px; padding: 0px;">xml version</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"1.0"</span><span class="pln" style="margin: 0px; padding: 0px;"> encoding</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"UTF-8"</span><span class="pun" style="margin: 0px; padding: 0px;">?></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="dec" style="margin: 0px; padding: 0px;"><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><struts></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><constant</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"struts.devMode"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"true"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><constant</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"struts.custom.i18n.resources"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">value</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"global"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px;">/></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><package</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"helloworld"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">extends</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"struts-default"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">namespace</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"/"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><action</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"empinfo"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">class</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"com.yiibai.struts2.Employee"</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="atn" style="margin: 0px; padding: 0px;">method</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"execute"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><result</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"input"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">/index.jsp</span><span class="tag" style="margin: 0px; padding: 0px;"></result></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><result</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"success"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">/success.jsp</span><span class="tag" style="margin: 0px; padding: 0px;"></result></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"></action></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><action</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"locale"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">class</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"com.yiibai.struts2.Locale"</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="atn" style="margin: 0px; padding: 0px;">method</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"execute"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><result</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">name</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"success"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;">/index.jsp</span><span class="tag" style="margin: 0px; padding: 0px;"></result></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"></action></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"></package></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"></struts></span>

以下是web.xml文件中的内容:

<span class="pun" style="margin: 0px; padding: 0px;"><?</span><span class="pln" style="margin: 0px; padding: 0px;">xml version</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"1.0"</span><span class="pln" style="margin: 0px; padding: 0px;"> encoding</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="str" style="margin: 0px; padding: 0px;">"UTF-8"</span><span class="pun" style="margin: 0px; padding: 0px;">?></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"><web-app</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">xmlns:xsi</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"http://www.w3.org/2001/XMLSchema-instance"</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="atn" style="margin: 0px; padding: 0px;">xmlns</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"http://java.sun.com/xml/ns/javaee"</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="atn" style="margin: 0px; padding: 0px;">xmlns:web</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="atn" style="margin: 0px; padding: 0px;">xsi:schemaLocation</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"</span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="atn" style="margin: 0px; padding: 0px;">id</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"WebApp_ID"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px;">version</span><span class="pun" style="margin: 0px; padding: 0px;">=</span><span class="atv" style="margin: 0px; padding: 0px;">"3.0"</span><span class="tag" style="margin: 0px; padding: 0px;">></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><display-name></span><span class="pln" style="margin: 0px; padding: 0px;">Struts 2</span><span class="tag" style="margin: 0px; padding: 0px;"></display-name></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><welcome-file-list></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><welcome-file></span><span class="pln" style="margin: 0px; padding: 0px;">index.jsp</span><span class="tag" style="margin: 0px; padding: 0px;"></welcome-file></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"></welcome-file-list></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><filter></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><filter-name></span><span class="pln" style="margin: 0px; padding: 0px;">struts2</span><span class="tag" style="margin: 0px; padding: 0px;"></filter-name></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><filter-class></span><span class="pln" style="margin: 0px; padding: 0px;">org.apache.struts2.dispatcher.FilterDispatcher</span><span class="tag" style="margin: 0px; padding: 0px;"></filter-class></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"></filter></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><filter-mapping></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><filter-name></span><span class="pln" style="margin: 0px; padding: 0px;">struts2</span><span class="tag" style="margin: 0px; padding: 0px;"></filter-name></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"><url-pattern></span><span class="pln" style="margin: 0px; padding: 0px;">/*</span><span class="tag" style="margin: 0px; padding: 0px;"></url-pattern></span><span class="pln" style="margin: 0px; padding: 0px;"></span><span class="tag" style="margin: 0px; padding: 0px;"></filter-mapping></span><span class="pln" style="margin: 0px; padding: 0px;">
</span><span class="tag" style="margin: 0px; padding: 0px;"></web-app></span>

现在,右键点击项目名称,并单击 Export > WAR File创建一个WAR文件。然后部署此WAR在Tomcat的webapps目录下。最后,启动Tomcat服务器和尝试访问URL http://localhost:8080/HelloWorldStruts2/index.jsp。这会给出以下画面:

现在选择的任何一种语言,让我们说,我们选择西班牙语,这将显示以下结果:

您可以尝试用法语。最后,让我们尝试点击“Submit ”按钮,当我们在西班牙语言,它会显示以下画面: 

恭喜你,现在有一个多语种的网页,可以在全球范围内启动您的网站。

  相关解决方案