zoukankan      html  css  js  c++  java
  • Struts2>I18N

    一、国际化原理


     首先指定全局的国际化资源文件:在配置文件struts.xml中引入 

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

    或在src下面新建struts.properties文件中指定如下一行:struts.custom.i18n.resources=app
     指定的国家化资源文件即为 xxx_语言_国家.properties,如上例为app_en_US.properties  app_zh_CN.properties
    里面的内容分别为

    welcome.msg=hello,sir
    welcome.msg=\u6B22\u8FCE\u4F60\uFF01
    ResourceBundle rs=ResourceBundle.getBundle("app",Locale.CHINA);
    ResourceBundle rs=ResourceBundle.getBundle("app",Locale.US);
    String welcome=rs.getString("welcome.msg");

    二、struts2的国际化 

    Struts2的国际化分三种情况:前台页面的国际化,Action类中的国际化,配置文件的国际化。
    2.1前台页面的国际化

    JSP页面上的国际化(使用struts2的<s:text .../>):

    <s:i18n name="app">
       <s:text name="hello">
         <s:param>${username}</s:param>
       </s:text>
     </s:i18n>

    表单元素的Label国际化,使用key:,值为properties文件中的key标签中key大多是和国际化相关的

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

    2.2Action中的国际化

    String message=getText("password.error");
    this.addFieldError("password", message);

    (3)配置文件的国际化

    Struts2基于XML配置方式实现对Action方法进行校验,并对校验信息国际化。参照http://blog.csdn.net/furongkang/article/details/6922046

    三、动态语言切换

    传递一个参数request_locale即可

    <s:url id="url" action="lang">
    			<s:param name="request_locale">en_US</s:param>
    		</s:url>
    		<s:a href="%{url}" key="english">
    			<s:text name="english" />
    		</s:a>
    
    		<s:url id="url1" action="lang">
    			<s:param name="request_locale">ch_CN</s:param>
    		</s:url>
    		<s:a href="%{url1}">
    			<s:text name="chinese" />
    		</s:a>
    
    
    
    		<a href="lang?request_locale=en_US"><s:text name="english"></s:text>
    		</a>
    		<a href="lang?request_locale=ch_CN"><s:text name="chinese"></s:text>

    Struts.xml配置action为lang时候跳到自身

    	<action name="lang">
    			<result>/add.jsp</result>
    		</action>

    四、国际化资源文件的分类

    当应用程序很大时,需要国际化的东西会很多,因此需要将国际化资源文件进行分类。在src中的properties文件是全局资源文件,另外还可以分为包级别的和类级别

    4.1全局资源文件

    在src中的properties文件是全局资源文件

    4.1包级别

    命名规则为package_language_country.properties  如package_en_US.properties

    4.2类级别

    如RegisterAction_en_US.properties

    国际化资源文件的优先级

    全局<包级别<类级别

    另外要进行表单的国际化时,要去掉theme="simple"

                                                                                                                                                                               

    例子:

    login.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
     String path = request.getContextPath();
     String basePath = request.getScheme() + "://"
       + request.getServerName() + ":" + request.getServerPort()
       + path + "/";
    %>
    <%@taglib uri="/struts-tags" prefix="s"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
     <head>
      <base href="<%=basePath%>">
    
      <title>My JSP 'index.jsp' starting page</title>
      <meta http-equiv="pragma" content="no-cache">
      <meta http-equiv="cache-control" content="no-cache">
      <meta http-equiv="expires" content="0">
      <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
      <meta http-equiv="description" content="This is my page">
     </head>
    
     <body>
     <s:fielderror/>
      <s:form id="login" action="user!login">
       <s:text name="name" />
       <input name="name" />
       <br />
       <s:text name="password" />
       <input name="password" type="password" />
       <br />
       <s:submit key="login">
       </s:submit>
      </s:form>
    
      <s:url id="url" action="lang">
       <s:param name="request_locale">en_US</s:param>
      </s:url>
      <s:a href="%{url}" key="english">
       <s:text name="english" />
      </s:a>
    
      <s:url id="url1" action="lang">
       <s:param name="request_locale">ch_CN</s:param>
      </s:url>
      <s:a href="%{url1}">
       <s:text name="chinese" />
      </s:a>
    
     
    
      <a href="lang?request_locale=en_US"><s:text name="english"></s:text>
      </a>
      <a href="lang?request_locale=ch_CN"><s:text name="chinese"></s:text>
      </a>
     </body>
    </html>
    

    login_success.jsp

    package com.ncepu.struts2;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction extends ActionSupport {
    
     private static final long serialVersionUID = 1L;
     private String name;
     private String password; 
    
     
    
     public String getName() {
      return name;
     }
    
     public void setName(String name) {
      this.name = name;
     }
    
     public String getPassword() {
      return password;
     }
    
     public void setPassword(String password) {
      this.password = password;
     }
    
     public String login() {
      if ("".equals(name)) {
       String message=getText("username.error");
       this.addFieldError("name", message);
       return "error";
      }
      if ("".equals(password)) {
       String message=getText("password.error");
       this.addFieldError("password", message);
       return "error";
      }
      System.out.println("name=" + name + "password=" + password);
      return ("success");
     }
    
    }
    

    LoginAction.java

    package com.ncepu.struts2;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction extends ActionSupport {
    
    	private static final long serialVersionUID = 1L;
    	private String name;
    	private String password; 
    
    	
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    
    	public String login() {
    		if ("".equals(name)) {
    			String message=getText("username.error");
    			this.addFieldError("name", message);
    			return "error";
    		}
    		if ("".equals(password)) {
    			String message=getText("password.error");
    			this.addFieldError("password", message);
    			return "error";
    		}
    		System.out.println("name=" + name + "password=" + password);
    		return ("success");
    	}
    
    }
    

    struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
    
    	<constant name="struts.devMode" value="true" />
    	<constant name="struts.custom.i18n.resources" value="message"></constant>
    
    	<package name="default" namespace="/" extends="struts-default">
    
    		<action name="user" class="com.ncepu.struts2.LoginAction">
    			<result name="success">
    				/login_success.jsp
                </result>
    
    			<result name="error">
    				/login.jsp
                </result>
    		</action>
    
    		<action name="lang">
    			<result>/login.jsp</result>
    		</action>
    
    	</package>
    </struts>
    

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
        <display-name>Struts Blank</display-name>
    
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <welcome-file-list>
            <welcome-file>login.jsp</welcome-file>
        </welcome-file-list>
    
    </web-app>
    

    message_en_US.properties

    name=username
    password=password
    login=login
    username.error = the username error !
    password.error = the password error!
    chinese=chinese
    english=english
    name.empty = the username should not be empty \!  
    name.size = the size of username should be between 6 and 12 \! 
    login.success=login success\!
    

    message_zh_CN.properties

    name=\u7528\u6237\u540D
    password=\u5BC6\u7801
    login=\u767B\u9646
    username.error = \u7528\u6237\u540D\u9519\u8BEF
    password.error = \u5BC6\u7801\u9519\u8BEF
    english=\u82F1\u8BED
    chinese=\u4E2D\u6587
    name.empty=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A
    name.size=\u7528\u6237\u540D\u5FC5\u987B\u4E3A6-12\u4F4D
    login.success=\u767B\u9646\u6210\u529F
    

    ps:   jsp页面少了<%@taglib uri="/struts-tags" prefix="s"%>,会导致国际化显示错误。

     

    文章:  http://www.blogjava.net/toby/archive/2009/03/19/260918.html

            http://www.cnblogs.com/langlang/archive/2010/01/14/1647627.html

            http://blog.csdn.net/furongkang/article/details/6922046

     

     

     

  • 相关阅读:
    Word 转换为 PDf 的技术方案
    [转载]sql server 常用存储过程
    Redmine 初体验
    Quartz.net Tutorial Lesson 1
    [转载]sql server TSQL 区分字符串大小写 的两种方法
    [原创]sql server inner join 效率测试
    java实现树的一般操作
    【转】Mybatis中进行批量更新
    【转载】单例模式的7种写法(整合自两篇文章)
    mybtis批量insert传入参数为list
  • 原文地址:https://www.cnblogs.com/xqzt/p/5637204.html
Copyright © 2011-2022 走看看