How struts can be internationalized)

Source: Internet
Author: User

StrutsInternational

 

1.ConfigurationStruts-config.xmlFile:

<Message-resources parameter = "res. messageresources"/>

To define the path of the resource file

 

The"Res. messageresources"We are hereSRCPath of the international configuration file created under the Directory

Res: Directory Name

Messageresources:Basename

 

2.Provides international resource files of different versions.Native2asciiConvertUnicode

 

3.InJSPIn use<Bean: Message>Label to read international message text

 

To use this label, we need to useStrutsDefined tag

<% @ Taglib prefix = "Bean" uri = "http://struts.apache.org/tags-bean" %>

In this way, we use<Bean: Message key = "user. Title"/>You can readKeyValue

 

WhenHTTPWhen the request is sent,StrutsBy defaultSessionCheck whether there is a correspondingLocaleObject.StrutsOfProcessIn the request header file.LocaleObject, and place the objectSessionMedium

 

BecauseLocaleThe information is stored inSessionSo we can defineLocaleObject, andSetToSessionTo implement manual language switching.

 

4Understanding usageStrutsBy defaultLocalePut inSessionTo complete the language switching settings in programming mode.

 

The specific implementation method is as follows:

 

1.Write a hyperlink by yourself,URLPoint to a customAction, Followed by a language parameter. For example,ZH".

 

2.In our own definedActionObtain the language parameters, and create the correspondingLocaleObjectSession.

 

 ExampleCode

 

For example, the hyperlink is like this.

 

<A href = "changelang. do? Lang = ZH ">Chinese</A>

 

Create by yourselfChangelangOfAction

 

Public actionforward execute (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception {

 

Obtain the language information transmitted from the page

 

String lang = request. getparameter ("Lang ");

 

Locale currentlocale = locale. getdefault ();

 

If ("ZH". Equals (Lang )){

 

Currentlocale = new locale ("ZH", "cn ");

 

} Else if ("en". Equals (Lang )){

 

Currentlocale = new locale ("en", "us ");

 

}

 

// Request. getsession (). setattribute (Globals. locale_key, Currentlocale );

 

Called hereStrutsWe have defined our own internal methods and defined our ownCurrentlocalePlace the objectSession. The functions of the Code commented out above are the same as those described above.

 

This. setlocale (request, currentlocale );

 

Return Mapping. findforward ("Index ");

}

First, load the attribute file according to the locale of the session. If no, read the loacle of the HTTP header file. When the loacle of the HTTP header file does not match the corresponding attribute file, what language will it display? The answer is: load the relevant attribute files based on the server-side region and language settings, and then load the default attribute files based on the group. For example, you have two Property Files, Chinese and English, and one default property file. The region is set to Chinese. The browser uploads an Arabic locale, and no corresponding Arabic attribute file exists. In this case, the attribute file of Chinese will be read according to the Chinese characters in the region, if the region is Russian or other, the default attribute file will be read. (During testing, restart Tomcat when the region settings change)

Dynamic filling prompt information

 

1.Configure the corresponding information in the corresponding international File

For example, the following information is added:

User. login. Success = {0 },Logon successful

 

Now we wantProgramDynamically fill in this information, as long as the following is OK

1.Create an international message text set

Actionmessages messages = new actionmessages ();

2.Create international message text

Actionmessage message = new actionmessage ("user. login. Success", new object [] {username })

Actionmessage: OneActionmessageAn object corresponds to a message in an international file.

User. login. Success: Corresponding messageKey

New object [] {username}: The array corresponds to the placeholder in the message by subscript.

3.Add the message text to the text set created earlier

Messages. Add ("loginsuccess2", message );

4.This text set is passed toRequestObject

This. savemessages (request, messages );

Note:SavemessagesMethodActionObject. The internal source code is actually to place the set of text we createdRequestIn an attribute

 

The internal source code is as follows:

Request. setattribute (Globals. message_key, Messages );

We can useStrutsTake out the provided tag.

 

However, if the above message is an error message, you need

This. saveerrors (request, messages );

 

At this time, the text set is actually placed inRequestAttribute,KeyThe source code is like this.

Request. setattribute (Globals. error_key, Errors );

 

StrutsDistinguish between common information and error information on the page is differentKeyToRequest.

 

If you want to retrieve the error information we put on the page, first load the correspondingStrutsTag Library

 

<% @ Taglib prefix = "Bean" uri = "http://struts.apache.org/tags-bean" %>

 

<% @ Taglib prefix = "html" uri = "http://struts.apache.org/tags-html" %>

 

 Then we can use the following method to readMessagesData in

 

<HTML: messages id = "MSG" message = "true" property = "loginsuccess1">

<Bean: write name = "MSG"/>

</Html: messages>

 

This label will be stored cyclicallyMessagesBy default, only the parts marked as error are read.

In this labelMessageThe default value is"False", At this time, this label is read-only to us.SaveerrorsIf you want to read common prompts, you must setMessage = "true"

 

If we do not setPropertyAttribute, which loops out allMessagesBut if we setProperty = "loginsuccess2", Then we can only read the frontMessages. Add ("loginsuccess2", message );TheMessage.

 

You can also use<HTML: errors>Displays messages, but this method only displays error messages.

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.