• 菜鸟学SSH(二)——Struts2国际化手动切换版


    国际化(internationalization,i18n)和本地化(localization,l10n)指让产品(出版物,软件,硬件等)能够适应非本地环境,特别是其他的语言和文化。程序在不修改内部代码的情况下,能根据不同语言及地区显示相应的界面。


    国际化原理:

    国际化资源文件:用不同国家的语言描述相同的信息,并放在各自对应的.properties属性文件中,程序根据运行时环境决定加载哪个文件。
    国际化主要通过以下类完成: 
    java.util.Locale:对应一个特定的国家/区域、语言环境。 
    java.util.ResourceBundle:用于加载一个资源包。 
    I18nInterceptor:struts2所提供的国际化拦截器,负责处理Locale相关信息。

    国际化流程:程序得到当前运行环境的国家/区域、语言环境并存放于Locale,ResourceBundle根据Locale中信息自动搜索对应的国际化资源文件并加载。当某个Action被执行前,I18nInterceptor负责检测Locale相关信息来寻找对应的国际化资源


    先来看一下实例的效果图:


    默认中文:


    点击英文,页面变成英文显示:



    接下来看具体的实现:


    LoginAction

    package com.lsj.action;
    
    import java.util.Locale;
    import org.apache.struts2.ServletActionContext;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction extends ActionSupport {
    
        private static final long serialVersionUID = 1L;
        
        
        
        private String flag;
        public String getFlag() {
            return flag;
        }
        public void setFlag(String flag) {
            this.flag = flag;
        }
        
        public String ha() throws Exception {
            this.flag=ServletActionContext.getRequest().getParameter("flag");
                    Locale l = Locale.getDefault();   
                    if(this.flag==null){   
                       l = new Locale("zh", "CN");   
                    }else if (this.flag.equals("2")) {   
                       l = new Locale("zh", "CN");   
                    } else if (this.flag.equals("1")) {   
                       l = new Locale("en", "US");   
                    }    
                  ActionContext.getContext().setLocale(l);   
                  return "success";   
     
        }    
    
    }


    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
         <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>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    
    


    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.i18n.encoding" value="GBK"/>
        <!-- 配置i18n -->
        <constant name="struts.custom.i18n.resources" value="message"></constant>
    
           
       <!-- i18n控制 -->
       <package name="i18n" namespace="/"  extends="struts-default">
            <action name="i18n" class="com.lsj.action.LoginAction" method="ha">
                <result name="success">
                   /i18nlogin.jsp
                </result>
                <result name="input">
                   /i18nlogin.jsp
                </result>
            </action>
        </package>    
    
    </struts>
    
    


    login.jsp

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <html>
      <head>
        <base href="<%=basePath%>">    
        <title>My JSP 'i18nlogin.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:i18n name="local.message">
        <s:form action="/i18n.action" method="post">
          <s:textfield name="user.userName" label="%{getText('login.name')}"/>
          <s:password name="user.userPassword" label="%{getText('login.pass')}"/>
          <s:submit value="%{getText('login.submit')}"></s:submit>
           <a href="i18n.action?flag=1">英文</a>
           <a href="i18n.action?flag=2">中文</a>
        </s:form></s:i18n>
      </body>
    </html>


    需要引入的jar包:



    OK到这里就实现了一个简单的国际化的小实例,有的说弄国际化没多大必要,因为咱们做的东西几乎都只是用于国内。我想说的是:为什么就觉得我们做的东西只会给我们自己用?为什么就不会觉得随着我们不断的努力,我们做的东西会越来越好,然后我们的东西会逐渐打入国际市场,让老外用我们做的东西呢?你们有信心吗?



  • 相关阅读:
    vfs:结构体对象
    vfs:open.c 源码学习
    Linux direct io使用例子
    GPU安装小结
    tensorflow scope的作用
    tensorflow 一维卷积 tf.layers.conv1()使用
    tensorflow 的tf.where详解
    tensorflow 的tf.split函数的用法
    tensorflow 添加一个全连接层
    tensorflow 计算均值和方差
  • 原文地址:https://www.cnblogs.com/liushuijinger/p/3432563.html
Copyright © 2020-2023  润新知