学习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=\u767B\u5F55\u540D
loginuserpwd=\u5BC6\u7801
remoment=\u8BB0\u4F4F\u7528\u6237\u540D\u548C\u5BC6\u7801
login=\u767B\u5F55
register=\u6CE8\u518C
comeback=\u56DE\u5230\u9996\u9875
QQcode=QQ\u53F7

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

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

struts2 国际化语言转换的更多相关文章

  1. 菜鸟学SSH(三)——Struts2国际化自动检测浏览器语言版

    前几天发了一篇Struts国际化的博客——<菜鸟学习SSH(二)——Struts2国际化手动切换版>,有网友提了一个意见,见下图: 于是就有了下面修改的版本: web.xml <?x ...

  2. struts2国际化

    struts2国际化 1:什么是国际化? 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有的与语言,国家/地区和文化相关的元素 ...

  3. 【转】Struts2国际化

    原文章:http://www.cnblogs.com/hellokitty1/p/5083663.html 简单理解     国际化简称i18n,其来源是英文单词 internationalizati ...

  4. (六)Struts2 国际化

    所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:国际化简介 国际化(Internationlization),通俗地讲, ...

  5. struts2国际化全例 错误解决

    在struts2中需要做国际化的有: jsp页面的国际化,action错误信息的国际化,转换错误信息的国际化,校验错误信息的国际化 在之前的例子中已经做过和国际化相关的例子了,在struts.xml中 ...

  6. Struts2国际化-getText()方法

    转自https://blog.csdn.net/qq_43560838/article/details/83747604 一:简单理解 国际化简称i18n,其来源是英文单词 international ...

  7. struts2系列(四):struts2国际化的多种方式

    一.struts2国际化原理 根据不同的Locale读取不同的文本. 例如有两个资源文件: 第一个:message_zh_CN.properties 第二个:message_en_US.propert ...

  8. struts2学习(10)struts2国际化

    一.国际化简介: 二.struts2国际化设置: struts.xml: <?xml version="1.0" encoding="UTF-8" ?&g ...

  9. [JavaWeb基础] 016.Struts2 国际化配置

    如果一个软件想要让其受众是全球或者是几个国家的人,那么这个软件就需要支持多种语言,那么我们就需要软件的国际化去对一些文字信息进行国际化处理.web也一样,当外国人打开我们的网站,要是看到满屏幕的中文, ...

随机推荐

  1. P2057 [SHOI2007]善意的投票 最小割

    $ \color{#0066ff}{ 题目描述 }$ 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照 ...

  2. luogu4383 [八省联考2018]林克卡特树(带权二分+dp)

    link 题目大意:给定你 n 个点的一棵树 (边有边权,边权有正负) 你需要移除 k 条边,并连接 k 条权值为 0 的边,使得连接之后树的直径最大 题解: 根据 [POI2015]MOD 那道题, ...

  3. 使用Pytesseract+Tesseract-OCR识别图片的简单步骤

    1.首先安装Pytesseract,这个很简单,直接输入命令 pip install pytesseract即可 2.Tesseract-OCR https://pan.baidu.com/s/1sV ...

  4. 40个迹象表明你还是PHP菜鸟

    你是PHP菜鸟,如果你: 1. 不会利用如phpDoc这样的工具来恰当地注释你的代码2. 对优秀的集成开发环境如Zend Studio或Eclipse PDT视而不见3. 从未用过任何形式的版本控制系 ...

  5. selenium+python+unittest:一个类中只执行一次setUpClass和tearDownClass里面的内容(可解决重复打开浏览器和关闭浏览器,或重复登录等问题)

    unittest框架是python自带的,所以直接import unittest即可,定义测试类时,父类是unittest.TestCase. 可实现执行测试前置条件.测试后置条件,对比预期结果和实际 ...

  6. [Alpha]Scrum Meeting#4

    github 本次会议项目由PM召开,时间为4月5日晚上10点30分 时长20分钟 任务表格 人员 昨日工作 下一步工作 木鬼 调整&分配工作 撰写博客目录 SiMrua 手工建立再训练数据( ...

  7. [转] 打造基于CentOS7的xfce最简工作环境[转自smstong,在此记录一下]

    [From]https://blog.csdn.net/hejianlz/article/details/78976013 3 安装步骤 3.1 执行CentOS7 最小安装 去官网下载CentOS- ...

  8. hive Getting Started

    Apache HiveThe Apache Hive™ data warehouse software facilitates reading, writing, and managing large ...

  9. Linux工具安装配置之Oracle

    对于Oracle的安装真的是心生恐惧,特别是一堆堆的依赖包.趁这次DBA在南京,实际操作一番. 两种方式,1.全新安装:2.硬拷贝 全新安装,参考下面这个介绍 http://www.cnblogs.c ...

  10. mysql 02

    CREATE TABLE emp(eid INT,ename VARCHAR(20),egender CHAR(2),ebirthday DATE,eemail CHAR(10),eramark VA ...