struts2 国际化语言转换

学习struts2,了解了使用struts2的配置文件可以走向国际化,实现页面的语言转换。我已中文和英文为例,简单的实现登录页面的国际化

废话不多说,上代码

一,login.jsp页面

使用s标签<s:text name="QQcode" />对需要的转化的字进行变量
文字为两个a标签。点击进入后台实现语言的转化
<%@ 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+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <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">
    -->
        <link rel="stylesheet" href="css/style_public.css" />
        <link rel="stylesheet" href="css/style_login.css" />
  <script type="text/javascript" src="js/jquery-1.8.3.js"></script>
  </head>
  
<body>
        ta<!--头部-->
        <header>
            <div class="btn"><a href="login.html"><s:text name="login" /></a>/
            <a href="registered.html"><s:text name="register" /></a></div>
        </header>
        <!--中间内容(主题内容)-->
        <div class="main">
            <div class="box">
            <a href="${pageContext.request.contextPath }/chang_change.action?lang=zh&local=CN" style="color: red">中文</a>
     <a href="${pageContext.request.contextPath }/chang_change.action?lang=en&local=US"  style="color: red">English</a>
                <p><a href="index.html"><s:text name="comeback" /></a></p>
                <form id="forml" action="user_loginString.action" method="post">
                
                <div class="content">
                    <p><s:text name="QQcode" /></p>
                    <input type="text" placeholder="请输入QQ号" id="input1" name="um.userid" />
                    <p><s:text name="loginuserpwd" /></p>
                    <input type="password" placeholder="请输入密码" id="input2" name="um.userpwd"/>
                    <div style="margin-top: 20px;color:red;" ><input id="remebers" type="checkbox"  name="remenber" value="1"/>
          <span ><s:text name="remoment" /></span></div>
                    <div class="btnss" onclick="sub()"><s:text name="login" /></div>
                </div>
                </form> 
                <span id="sp1"></span>
                <span id="sp2"></span>
            </div>
            
        </div>
        <!--尾部-->
        <footer>
            <p style="margin-top: 0px;">网站链接:你猜网 www.xxxxxxxxx.com</p>
        </footer>
    </body>
</html>

二,ChangeLanguage类,放在action包下面

存入session作用域的 "WW_TRANS_I18N_LOCALE" 不能写错

package com.direct.action;

import java.util.Locale;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ChangeLanguage extends ActionSupport{

    private String local;
    private String lang;
    
    public String change(){
        Locale le = new Locale(lang, local);
        //  存入session作用域
        ServletActionContext.getRequest().getSession().setAttribute("WW_TRANS_I18N_LOCALE", le);
        ServletActionContext.getContext().setLocale(le);  //  启用
        return "change";
    }

    public String getLocal() {
        return local;
    }
    public void setLocal(String local) {
        this.local = local;
    }
    public String getLang() {
        return lang;
    }
    public void setLang(String lang) {
        this.lang = lang;
    }
    
}

三,struts2.xml

<constant name="struts.custom.i18n.resources" value="Myresource"></constant>
一定需要,是一个常量,设置国际化
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<constant name="struts.custom.i18n.resources" value="Myresource"></constant>
    <package name="us" extends="struts-default,json-default" namespace="/">
      
        <action name="chang_*" class="com.direct.action.ChangeLanguage" method="{1}">
            <result name="change">/login.jsp</result>
          </action>
          
    </package>

</struts>    

四,Myresource_en_US.properties  和 Myresource_zh_CN.properties 两个放在src下的配置文件(与struts2.xml 同一位置),命名规则必须这样,不可改

Myresource_en_US.properties

login=login
register=register
comeback=comeback
loginuserpwd=loginuserpwd
remoment=remoment
QQcode=QQcode
loginbutton=login

Myresource_zh_CN.properties

loginusername=u767Bu5F55u540D
loginuserpwd=u5BC6u7801
remoment=u8BB0u4F4Fu7528u6237u540Du548Cu5BC6u7801
login=u767Bu5F55
register=u6CE8u518C
comeback=u56DEu5230u9996u9875
QQcode=QQu53F7

五,最后的开启项目,在浏览器中访问了。直接访问login.jsp页面就可以。

点击两个a标签时,就会吧页面的相应字改变语言。很easy把

原文地址:https://www.cnblogs.com/nn369/p/7793437.html