Chinese and English support-did your program do it?---not outdated application of struts 2 internationalization support features (7)

Source: Internet
Author: User
Tags constant gettext i18n locale ole

4.3   Struts 2 internationalization support

java the The principle and method of internationalization, readers know, The internationalization of the Java locale the

struts 2 java getbundle () locale Span lang= "en-US" >key the

struts 2 java internationalization is consistent, as a good mvc the frame, struts 2 java

4.3.1 Configuring resource Files

Struts 2 The emphasis is on loose coupling between components, and each component is interconnected and interacting through configuration files. So is the internationalization of the Struts 2 framework.

struts 2 struts 2 struts 2 framework Default profile

<!- Defining Interceptors -->

<interceptors>

<!- Defining Interceptors alias-->

<interceptor name= "Alias" class= "Com.opensymphony.xwork2.interceptor.AliasInterceptor"/>

<!- Defining Interceptors autowiring-->

<interceptor name= "autowiring" class= "Com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor" />

<interceptor name= "Chain" class= "Com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>

<interceptor name= "Conversionerror" class= "Org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/ >

<interceptor name= "Cookie" class= "Org.apache.struts2.interceptor.CookieInterceptor"/>

<interceptor name= "CreateSession" class= "Org.apache.struts2.interceptor.CreateSessionInterceptor"/>

<interceptor name= "Debugging" class= "Org.apache.struts2.interceptor.debugging.DebuggingInterceptor"/>

<interceptor name= "Externalref" class= "Com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/ >

<interceptor name= "execandwait" class= "Org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>

<!- defining an exception interceptor -->

<interceptor name= "Exception" class= "Com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>

<!- definition File Upload Interceptor -->

<interceptor name= "FileUpload" class= "Org.apache.struts2.interceptor.FileUploadInterceptor"/>

<!- Defining International Interceptors -->

<interceptor name= "i18n" class= "Com.opensymphony.xwork2.interceptor.I18nInterceptor"/>

<!- Define Log Interceptor -->

<interceptor name= "Logger" class= "Com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>

<interceptor name= "Modeldriven" class= "Com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>

... ...

visible, the interceptor is i18n , the corresponding class for com.opensymphony.xwork2.interceptor.I18n Interceptor , interested readers can refer to their API document.

I18ninterceptoris an interceptor class, the InterceptorActionExecutes before processing, which governs the current user request Sessionin theLocalerelated data. The interceptor looks for a special parameter value in the user's parameter, which is used to set theLocaleinformation, which means that the developer can dynamically modify the current Sessionin theLocalevalue, which is very useful in developing internationalized applications, developers can change any node in the process of processing a user requestLocalevalue, so it can dynamically change the language of the program and the relevant information of the region, and realize the perfect internationalization function.

For example, using the default interceptor parameter, suppose a user requests a Foo.action?request_locale=en_us , then English (language) and the United States (country) will be saved to the requested Session , which is used in subsequent request processing. The interceptor has the following two parameters:

parametername : Optional parameters, that is, the user request parameter contains the locale the request _locale

attributename : Optional parameters, session the locale value < Font face= "Times New Roman" >key The default is ww_trans_i18n_locale

Config resource file constant, That is, configure the struts 2 framework constants , the constant defines the struts 2 Framework Global internationalized resource file

If developers need to provide international work in project applications Yes, you need to specify the struts.custom.i18n.resources constant value. Configure struts.custom.i18n.resources constants, can be in the properties file Struts.properties or in the configuration file struts.xml or web.xml

Suppose you want to define a basename the globalmessages < of struts.custom.i18n.resources constant, can be in Struts.properties file is configured as follows:

# defined in the properties file basename

Struts.custom.i18n.resources=globalmessages

can also be in Struts.xml Configuration in Files basename :

i18n resource file is globalmessages

<constant name= "struts.custom.i18n.resources" value= "Globalmessages"/>

Similarly, it is possible to Web.xml defined in the file:

... ...

<!-- definition struts.custom.i18n.resources Constants -->

<init-param>

<param-name>struts.custom.i18n.resources</param-name>

<param-value>globalMessages</param-value>

</init-param>

... ...

struts 2 basename basename_language_country.properties native2ascii conversion tool to unicode the code can be.

4.3.2 Struts 2 International Applications

Once the developer has specified struts.custom.i18n.resources constant, that is, the that specifies the internationalized resource file. basename the struts 2 action diagram 4.6 The

< c16>

Figure 4.6 Registering Applications Action Relationship Diagram

(1) establishment of Chinese and English resource files,globalmessages_en_us.properties content such as code 4.7 as shown.




Code 4.7 English resource file globalmessages_en_us.properties

# English resource file content

Helloworld=hello world!

User=username

Pass=password

Username=your Name

Password1=password

Password2=confirm Password

Birthday=birthday

Regpage=reg Page

Errpage=error Page

Successlogin=welcom

falselogin=sorry! You can "T log in

Regsuccess=ok,you Reg success!

regfalse=sorry! You Reg false!

regbutton=reg!

(2) globalmessages_zh_cn.properties content as shown in the Code 4.8.




Code 4.8 Chinese resource file globalmessages_zh_cn.properties

# Simplified Chinese resource file content

helloworld= Hello, world!

name= User name

pass= User Password

username= Register User name

password1= Password

password2= Confirm Password

birthday= Birthdays

regpage= Registration Interface

errpage= Error Interface

successlogin= Login Successful

falselogin= Login Failed

regsuccess= Registration Successful

regfalse= Sorry, registration failed!

regbutton= Register

  file is a Chinese resource file that must be used before using the native2ascii the conversion tool conversion. Next, establish the input interface reg.jsp 4.9 The




Code 4.9 Input Interface reg.jsp

<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>

<%@ taglib prefix= "s" uri= "/struts-tags"%>

<title><s:text name= "Regpage"/></title>

<s:head/>

<body>

<table>

<s:form id= "id" action= "Reg" >

<!- uses key to load internationalized resource information -->

<s:textfield name= "username" key= "username"/>

<s:password name= "Password1" key= "Password1"/>

<s:password name= "Password2" key= "Password2"/>

<s:datetimepicker name= "Birthday" key= "Birthday" displayformat= "Yyyy-mm-dd"/>

<s:submit key= "Regbutton"/>

</s:form>

</table>

</body>

reg.jsp key attribute to output internationalization information. form The label of the element can also use the key

(4) Similarly, success.jsp also uses a tag library, as shown in the Code 4.10 .




Code 4.10 Registration Successful Interface success.jsp

<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>

<%@ taglib prefix= "s" uri= "/struts-tags"%>

<title><s:text name= "Regsuccess"/></title>

<s:head/>

<body>

<table>

: <s:property value= "username"/>

: <s:property value= "Password1"/>

: <s:property value= "Birthday"/>

</table>

</body>

top two JSP The user view, all the display content uses the internationalization information, may display the corresponding internationalization content according to the user different language and the region configuration.

then, the user view can access the internationalized resources,Actioncan you access it in? The answer is yes, I've already introduced it before,Struts 2provides aActionsupportTool class to develop your ownAction, you just have to inherit the class. In this class ofAPIdocument, you can see that the class provides a getText(String atextname) method that is based on the method in the resource fileKeyvalue to return an internationalized resource information, if it is not found ;NULL.

(5) in this example,Action, you will call the getText(String atextname) method to obtain the internationalization information in the resource file and print it out in the console, such as the Code4.11as shown.




Code 4.11 International Business Controller Reg

Package CH4;

Import Java.util.Date;

Import Com.opensymphony.xwork2.ActionSupport;

public class Reg extends Actionsupport {

    // defining user Name Properties

private String username;

/ / Define processing information: Note that the msg name in http is different

private String mymsg;

    // Define Password Properties

private String Password1;

    // Define Confirmation Password

private String password2;

    // Define birthday Attributes

private Date birthday;

Public String execute () throws Exception {

        // determining user Input Parameters

if (username!= null && getPassword1 (). Equals (GetPassword2 ())

&&!getusername (). Trim (). Equals ("")) {

            // Printing Internationalization Information

System.out.println (GetText ("username") + ":" + username);

System.out.println (GetText ("Password1") + ":" + password1);

System.out.println (getText ("birthday") + ":" + birthday);

return SUCCESS;

} else {

return INPUT;

        }

    }

//getter and setter methods

public String getusername () {

return username;

    }

public void Setusername (String username) {

this.username = username;

    }

public String getmymsg () {

return mymsg;

    }

public void setmymsg (String mymsg) {

this.mymsg = mymsg;

    }

public String getPassword1 () {

return password1;

    }

public void SetPassword1 (String password1) {

this.password1 = Password1;

    }

public String getPassword2 () {

return password2;

    }

public void SetPassword2 (String password2) {

this.password2 = password2;

    }

public Date getbirthday () {

return birthday;

    }

public void Setbirthday (Date birthday) {

this.birthday = birthday;

    }

/span>

Font face= "Times New Roman" > , (6) to run the application, in the Chinese and English language and regional configuration environment, the registration interface is shown in Figure 4.7 Span lang= "en-US" >4.8 the tomcat

Register User name :p la

Password : 123456

Birthdays : Wed Nov 00:00:00 CST 2007

when you set the language and locale option to English-American, Tomcat the console will print out English information:

Information : Detected Annotationactionvalidatormanager, initializing it ...

Your Name:pla

password:123456

Birthday:sun Oct 00:00:00 CST 2007

Figure 4.7 Chinese Registration Interface         

Figure 4.8 English Registration Interface

under the Chinese and English configuration, enter the " PLA "And so on, click the Register button or" reg! button, as shown in the 4.9 and Figure 4.10 As shown, all the elements displayed by the interface use the internationalization information output.

           

Figure 4.9 Registration Successful Chinese interface

Figure 4.10 sign up for successful English interface

Actioncan be used inActionsupportclass provides theGetText (String atextname)method to obtain the internationalization information, which confirms that the Internationalized interceptor described in the preceding list of user request parametersLocalevalue is stored in the current Sessionin whichActionit was obtained withLocaleThe associated internationalization information resource.







</

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.