您的位置:首页 > 编程语言 > Java开发

Struts2的国际化

2010-11-23 14:45 78 查看
Struts2的国际化:

     1:类的国际化 :类级别的文件命名规则为:className_zh_CN.properties等,className为要国际化的类名

     2:包的国际化: 包级别的文件命名规则为:package_zh_CN.properties等,所有的包级别的国际化资源文件都叫这个名字

     3:全局国际化: 文件名_语言_地区.properties

 

    演示国际化的步骤:

      1:配置struts.xml文件

           <struts>

       

                      <constant name="struts.i18n.encoding" value="gbk"></constant>

                     <constant name="struts.devMode" value="true"></constant>

                     <!--value是文件名-->

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

       

                    <package name="default" extends="struts-default">

       

                      <action name="login" class="com.ambow.struts2.action.UserAction">

                       <result name="success">/success.jsp</result>

                        <result name="error">/login.jsp</result>

                     </action>

                 </package>

       </struts>

      2:拟写Entity代码

          public class User {

        private int id;

       

        private String name;

       

        private String password;

        public int getId() {

            return id;

        }

        public void setId(int id) {

            this.id = id;

        }

        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;

        }

       

       

       

      }

     3:拟写Action代码:

 

 

          

public class UserAction extends ActionSupport implements SessionAware{

       

    public Map session;

   

    public User user;

    public void setSession(Map session) {

       

            this.session = session;

       

    }

       

   

    public User getUser() {

        return user;

    }

    public void setUser(User user) {

        this.user = user;

    }

    public Map getSession() {

        return session;

    }

    public String execute(){

       

        if("admin".equals(user.getName()) && "admin".equals(user.getPassword())){

           

            session.put("user", user);

           <!--这里可以得到资源文件的国际化的值,然后显示到界面  action里面访问资源文件-->

            String mess = getText("hello");

           

            session.put("mes", mess);

           

            return "success";

        }

        return "error";

    }

       

}

 

4:拟写jsp:

 

        <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<%@ taglib  uri="/struts-tags" prefix="s"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>My JSP 'login.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:form action="login" method="post">

            <s:textfield name="user.name" key="userName"></s:textfield><br/>

               

            <s:password name="user.password" key="password"></s:password><br/>

               

            <s:submit key="login"></s:submit></br>

               

               

               

            </s:form>

  </body>

</html>

5:拟写成功界面:

 

       <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<%@ taglib  uri="/struts-tags" prefix="s"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

   

    <title>My JSP 'login.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">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

  </head>

 

  <body>

            <s:text name="messagesuccess"></s:text><br/>

            <!--jsp页面也可以访问资源文件-->

            <s:property value="%{getText('messagesuccess')}"/></br>

           

            <s:property value="#session.mes"/></br>

  </body>

</html>

 

资源文件:

        1:ActionMessage_en_US.properties类容

                     userName=userName

                      password=userPassword

                       login=loginUser

                     messagesuccess=struts internation

            
4000
         hello=welcome  allan

 

       2:ActionMessage_zh_CN.properties

 

                    userName=/u7528/u6237/u540D

                    password=/u5BC6/u7801

                    login=/u767B/u9646

                   messagesuccess=/u6B22/u8FCE/u767B/u9646struts2/u56FD/u9645/u5316

                  hello=/u6B22/u8FCEallan/u767B/u9646

然后测试显示成功。

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息