Struts2 (13) internationalization-internationalization, struts2 internationalization

Source: Internet
Author: User
Tags i18n

Struts2 (13) internationalization-internationalization, struts2 internationalization
I. What is internationalization-I18N

Internationalization

There are 18 letters between the first letter I-ending letter n

Feature: when the program is not modified, the corresponding content can be displayed according to different language environments.

Ii. Java built-in Internationalization

 

Message_en_US.properties

demo = hello world!userName = user namepassword = passwordwelcome = welcome!

Message_zh_CN.properties

demo = \u60A8\u597D\u4E16\u754C!userName = \u7528\u6237\u540Dpassword = \u5BC6\u7801welcome = \u6B22\u8FCE!

Message. properties

demo = \u60A8\u597D\u4E16\u754C!userName = \u7528\u6237\u540Dpassword = \u5BC6\u7801welcome = \u6B22\u8FCE!

Implementation Code

<% @ Page import = "java. util. resourceBundle "%> <% @ page import =" java. util. locale "%> <% @ page language =" java "contentType =" text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> Iii. Implementation of Struts2

Prepare a global resource file: usually at least three files

Naming rules: prefix name_ _ country. properties

Content format: key = value

Specify resource files (set constants)

1. In the struts. xml file:

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

2. struts. properties:

struts.custom.i18n.resources=message

 

<s:text name="demo"></s:text><s:text name="userName" /><s:text name="password" /><s:text name="welcome" />

Change resource file

Message. properites

Demo = Hello world! UserName = userName password = password welcome = welcome! Submit = submit reset = reset

Message_zh_CN.properties

Demo = Hello world! UserName = userName password = password welcome = welcome! Submit = submit reset = reset

Message_en_US.properties

demo = hello world!userName = user namepassword = passwordwelcome = welcome!submit =submitreset=reset
<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" s "uri ="/struts-tags "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

Iv. Internationalization of Action

Message. properties

Demo = Hello world! UserName = userName password = password welcome = {0} welcome! {1} submit = submit reset = reset fail = Logon Failed usermsg = username cannot be blank usermsglength = username length is 2-16 bits pwdmsg = password cannot be blank pwdmsglength = password length is 2-16 bit

Message_zh_CN.properties is the same as above

Message_en_US.properties

demo = hello world!userName = user namepassword = passwordwelcome = {0}welcome!{1}submit =submitreset=resetfail=login failedusermsg=user name do not emptyusermsglength=user name length is 2 to 16!pwdmsg=password do not emptypwdmsglength=password length is 2 to 16!

Struts. xml

<struts>    <constant name="struts.enable.DynamicMethodInvocation" value="false" />    <constant name="struts.devMode" value="true" />    <constant name="struts.custom.i18n.resources" value="message" />    <package name="default" namespace="/" extends="struts-default">    <action name="login" class="action.LoginAction" method="login">    <result name="success">    /demo.jsp    </result>    <result name="input">    /demo.jsp    </result>    </action> </package></struts>

Demo. jsp

<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" s "uri ="/struts-tags "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

LogionAction

package action;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {    private String username;    private String password;    public String login() {        if ("admin".equalsIgnoreCase(username)                && "admin".equalsIgnoreCase(password)) {            this.addActionMessage(this.getText("welcome", new String[] {                    username,password}));            return SUCCESS;        }        this.addActionMessage(this.getText("fail"));        return INPUT;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}

LogionAction. login () authentication File

LogionAction-login-validation.xml

 

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"><validators><field name="username"><field-validator type="requiredstring"><param name="trim">true</param><message>${getText("usermsg")}</message></field-validator><field-validator type="stringlength"><param name="trim">true</param><param name="minLength">2</param><param name="maxLength">16</param><message>${getText("usermsglength")}</message></field-validator></field><field name="password"><field-validator type="requiredstring"><param name="trim">true</param><message>${getText("pwdmsg")}</message></field-validator><field-validator type="stringlength"><param name="trim">true</param><param name="minLength">2</param><param name="maxLength">16</param><message>${getText("pwdmsglength")}</message></field-validator></field></validators>

 

V. Jsp page international parameter transfer
<!-- First Example --><s:i18n name="struts.action.test.i18n.Shop">    <s:text name="main.title"/></s:i18n><!-- Second Example --><s:text name="main.title" /><!-- Third Examlpe --><s:text name="i18n.label.greetings">   <s:param >Mr Smith</s:param></s:text>

 

<s:text name="welcome"><s:param>admin</s:param><s:param>ffff</s:param></s:text>

6. resource search sequence

7. Use internationalization in JSP
When using the UI form flag, getText can be used to set the label attribute, for example, <s: textfield name = "name" label = "% {getText ('username')}"/>
8. Use OGNL expressions in resource files

In many cases, we need to insert some parameters for international characters in the dynamic runtime, for example, when entering the verification prompt information. In Struts 2.0, we can use ognl in the international string of the resource file to do this: The format is $ {expression}, for example:

validation.require=${fileName} is required
9. Resource file search sequence

 

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.