[Development Framework] struts is an easy way to internationalize

Source: Internet
Author: User

1. Create a server

I used Tomcat5.5 as the test environment to establish the process (omitted );


2. Download struts

You can download it at http://jakarta.apache.org/struts/index.html.


3. Establish a project

Creating an international folder under the webapps directory and creating a WEB-INF folder and a WEB-INF/classes folder under the international Directory are required by a JSP project;


4. Added struts class

Create a lib subdirectory under the WEB-INF directory, copy all. jar files under the struts-exampleWEB-INFlib directory to this directory; these files are struts control class library and label class library;
Commons-beanutils.jar
Commons-collections.jar
Commons-digester.jar
Commons-fileupload.jar
Commons-lang.jar
Commons-logging.jar
Commons-validator.jar
Jakarta-oro.jar
Struts. jar


5. Add the struts tag definition file

Copy the. TLD file to the international WEB-INF directory under the struts-exampleWEB-INF directory, the definition files of these file tag libraries;
Struts-bean.tld
Struts-html.tld
Struts-logic.tld
Struts-nested.tld
Struts-template.tld
Struts-tiles.tld


6. Create a struts config File

Create the struts config file struts-config.xml, the content is as follows:

<? Xml version = "1.0" encoding = "ISO-8859-1"?>

<! DOCTYPE struts-config PUBLIC
"-// Apache Software Foundation // DTD Struts Configuration 1.1 // EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<Struts-config>

<Message-resources parameter = "resources. application"/>

</Struts-config>

The message-resources tag refers to the file of the message resource. It is the file that stores prompt information in multiple languages. resources. the application table is the resources directory under the classes directory to store resource files. The default language file name is application. properties: application_zh_CN.properties, which is similar to other languages;


7. Create a web. xml file

<? Xml version = "1.0" encoding = "ISO-8859-1"?>

<! DOCTYPE web-app
PUBLIC "-// Sun Microsystems, Inc. // DTD Web Application 2.2 // EN"
Http://java.sun.com/j2ee/dtds/web-app_2_2.dtd>

<Web-app>
<Display-name> test </display-name>

<Servlet>
<Servlet-name> action </servlet-name>
<Servlet-class> org. apache. struts. action. ActionServlet </servlet-class>
<Init-param>
<Param-name> config </param-name>
<Param-value>/WEB-INF/struts-config.xml </param-value>
</Init-param>
<Init-param>
<Param-name> debug </param-name>
<Param-value> 2 </param-value>
</Init-param>
<Init-param>
<Param-name> detail </param-name>
<Param-value> 2 </param-value>
</Init-param>
<Load-on-startup> 2 </load-on-startup>
</Servlet>

<Servlet-mapping>
<Servlet-name> action </servlet-name>
<Url-pattern> *. do </url-pattern>
</Servlet-mapping>

<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>

<Taglib>
<Taglib-uri>/tags/struts-bean </taglib-uri>
<Taglib-location>/WEB-INF/struts-bean.tld </taglib-location>
</Taglib>

<Taglib>
<Taglib-uri>/tags/struts-html </taglib-uri>
<Taglib-location>/WEB-INF/struts-html.tld </taglib-location>
</Taglib>

<Taglib>
<Taglib-uri>/tags/struts-logic </taglib-uri>
<Taglib-location>/WEB-INF/struts-logic.tld </taglib-location>
</Taglib>

<Taglib>
<Taglib-uri>/tags/struts-nested </taglib-uri>
<Taglib-location>/WEB-INF/struts-nested.tld </taglib-location>
</Taglib>

<Taglib>
<Taglib-uri>/tags/struts-tiles </taglib-uri>
<Taglib-location>/WEB-INF/struts-tiles.tld </taglib-location>
</Taglib>

</Web-app>

 

8. Create a resource file
 
Create a resources directory under the classes directory to store resource files;
First, create the default resource file application. properties and the resource file application_en_US.properties in English (U.S.). The content is:

# -- Test --
Test. title = international application test
Test. body = This is a international application test

First, create these two files, and then create a Chinese file.


9. Create a jsp file

In the international Directory, create the index. jsp file with the following content:

<% @ Page contentType = "text/html; charset = UTF-8" %>
<% @ Taglib uri = "/tags/struts-bean" prefix = "bean" %>
<% @ Taglib uri = "/tags/struts-html" prefix = "html" %>
<% @ Taglib uri = "/tags/struts-logic" prefix = "logic" %>
<Html: html locale = "true">
<Head>
<Title> </Title>
<Html: base/>
</Head>
<Body bgcolor = "white">

<P> <bean: message key = "test. body"/> </p>

</Body>
</Html: html>

Here The default region and language of the browser. <bean: message key = "test. title"/> indicates the content of the test. title Project in the corresponding resource file;
Start Tomcat and enter http: // localhost: 8080/international/in the browser to view the effect. If the browser title displays international application test, if This is a international application test is displayed on the page, it indicates that your program has succeeded. if you add a resource file, you can view it in a multi-language system;


10. Create a resource file in simplified Chinese

Create an application_cn.properties under the resources directory and enter the following content:

# -- International test --
Test. title = International Program test
Test. body = This is an international test example.

Because java internationalization is implemented through unicode code, you need to convert the code into unicode code. In Dos, go to the resources directory and execute:
Native2ascii-encoding gb2312 application_cn.properties application_zh_CN.properties
The converted application_zh_CN.properties file contains the following content:

# -- International test --
Test. title = u56fdu9645u5jwu7a0bu5e8fu6d4bu8bd5
Test. body = example

This is the Chinese unicode code above;
Restart Tomcat and enter http: // localhost: 8080/international/in the browser. You can see if the title and content have become Chinese;

 

11. Test multiple languages
Open IE's "Tools"-> "Internet Options" menu, "general" tab, click the "language" button to add "English (USA) -[en-us] language: delete other languages. After restarting IE, enter http: // localhost: 8080/international/index. jsp, you will find that the content has become English.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.