zoukankan      html  css  js  c++  java
  • struts2国际化

    在src目录下新建.properties文件如

    struts2.xml文件

    <!-- 配置国际化资源文件 -->
        <constant name="struts.custom.i18n.resources" value="i18n"></constant>
    //内容英文
    
    username=UserName
    passwword=Password
    submit=Submit
    
    time=Time:{0}//带占位符
    
    //中文
    username=u7528u6237u540D
    password=u5BC6u7801
    submit=u63D0u4EA4
    
    time=u65F6u95F4:{0}
    
    //默认
    username=UserName
    password=Password
    submit=Submit
    
    time=Time:{0}

    Action

    package com.i18n;
    
    import java.util.Arrays;
    import java.util.Date;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class TestI18nAction extends ActionSupport {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        
        private Date date = null;
        
        public Date getDate() {
            return date;
        }
    
        public void setDate(Date date) {
            this.date = date;
        }
    
        @Override
        public String execute() throws Exception {
            
            date = new Date();
            
            //1. 在 Action 中访问国际化资源文件的 value 值
            String username = getText("username");
            System.out.println(username);
            
            //2. 带占位符的
            String time = getText("time", Arrays.asList(date));
            System.out.println(time);
            
            return SUCCESS;
        }
    }
    <%@page import="java.util.Date"%>
    <%@ 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">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        
        
        
    
        <a href="I18n?request_locale=en_US">English</a>
        <a href="I18n?request_locale=zh_CN">中文</a>
        <br><br>
        
        
        <br><br>
    
           <!--通过s:text拿到国际化文件内容-->
        <s:text name="time">
            <s:param value="date"></s:param>
        </s:text>
        
        
    
        
        <br><br>
        
        
        <s:form action="" theme="simple">
            
            <!--通过强制OGNL解析-->
            <s:text name="username"/>:<s:textfield name="username" label="%{getText('username')}"></s:textfield>
            
            <!-- key 的方式是直接上资源文件中获取 value 值 -->
            <s:text name="username"/>:<s:textfield name="username" key="username"></s:textfield>
            <s:text name="password"/>:<s:password name="password" key="password"></s:password>
            <s:submit key="submit" value="%{getText('submit')}"></s:submit>
                
        </s:form>
        
    </body>
    </html>

     

  • 相关阅读:
    前端使用crypto.js进行加密
    C#编程总结(七)数据加密——附源码
    PID file /run/zabbix/zabbix_server.pid not readable (yet?) after start. 报错解决
    TNS-12560: Message 12560 not found; No message file for product=network, facility=TNS报错
    oracle无法启动asm实例记录
    linux添加硬盘分区挂载教程
    Oracle Database 12c Release 2安装过程实录
    Centos6.9minimal版系统安装图形化界面
    扫描工具nmap介绍
    Zabbix系列之六——添加web监测
  • 原文地址:https://www.cnblogs.com/lusufei/p/7298374.html
Copyright © 2011-2022 走看看