Struts2 learning (6) international configuration

Internationalization can make our website switch to Chinese, English or other national languages

The internationalization in Struts2 is i18n - Internationalization (in fact , there are 18 letters between the letters i and n )

The addition of internationalization makes the software have the function of switching the interface language and also greatly simplifies the realization of the internationalization function.

 

realization of internationalization

 

Just pass a simple configuration file and a Struts2 tag and a default i18n interceptor can be achieved.

Implementation steps:

Step 1: Add to the configuration file struts.xml

<!--Configure internationalization support-->
<constant name="struts.custom.i18n.resources" value="message"/>
Step 2: Create the following two files in the same directory as the struts.xml file.



 message _en_US. properties (configure English information , note that the red part is the same as the value configured in struts.xml )


message _zh_CN. properties (configure Chinese information , note that the red part is the same as the value configured in struts.xml )


Note: The Chinese information here must be configured using ASCII code

The key values ​​in the configured Chinese and English configuration files must be the same, and the value values ​​are written in different languages.

 

The third step: use the struts tag in jsp to complete all the content of the interface

<%--
  Created by IntelliJ IDEA.
  User: icarus
  Date: 2016/7/2
  Time: 11:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
    <title>index</title>
  </head>
  <body>
    <s:text name="login.title"/>
    <s:form action="ch06LoginAction" method="POST">
        <s:textfield name="username" key="login.username"/>
        <s:password name="password" key="login.password"/>
        <s:token></s:token>
        <%--The value of the value in submit here cannot be configured in the previous way
        This is a loophole in struts2 internationalization, you can't use key, but use value
        <s:submit key="login.submit"/>
        --%>
        <s:submit value="%{getText(login.submit)}"/>
    </s:form>
    <s:a href="ch06LoginAction.action?request_locale=zh_CN">中文</s:a>
    <s:a href="ch06LoginAction.action?request_locale=en_US">English</s:a>
  </body>
</html>

 

Note : When Struts2 is internationalized, the key cannot be used to specify the corresponding value in the submit submission form tag , but the value must be used . % is the reference to the string calculation value in the expression language .

 

After this configuration, run the project, you can choose a different language interface through the link.


Running interface:


When click on English , the corresponding request is:

http://localhost:8080/ch06LoginAction.action?request_locale=en_US



In this way, all the texts in the website are configured into different languages ​​in the configuration file, so that the switching of different languages ​​can be realized, or the interceptor can be used to obtain the lang value in the cookies to automatically set the language of the website.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325716733&siteId=291194637