一、概念

国际化:界面上的语言可以根据用户所在的地区改变显示语言。

如图:

二、实例

下面就一步一步的教大家利用Struts实现国际化。

1、编写资源文件

这个资源文件就是界面上显示的字符,资源文件里面包含英文和中文的资源文件这样我们就可以转换资源文件来实现把界面上的中文、英文互相转化。

这里下载资源文件>>

2、编写相关界面。

Index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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 'index.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>
<a href="login.jsp">登录</a> <br>
<a href="changeLang.do?lang=zh">中文</a>    <a href="changeLang.do?lang=en">英文</a>
<p> </body>
</html>

login.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<%--
<font color="red">
<html:messages id="msg" property="error_1">
<bean:write name="msg"/>
</html:messages>
</font>
<font color="blue">
<html:messages id="msg" property="error_2">
<bean:write name="msg"/>
</html:messages>
</font>
--%>
<html:errors/>
<form action="login.do" method="post">
<bean:message key="login.form.field.username"/>:<input type="text" name="username"><br>
<bean:message key="login.form.field.password"/>:<input type="password" name="password"></br>
<input type="submit" value="<bean:message key="login.form.button.login"/>">
</form>
</body>
</html>

login_success.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<html:messages id="msg" message="true">
<bean:write name="msg"/>
</html:messages>
</body>
</html>

login_error.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<font color="red">
<html:messages id="msg" property="error_1">
<bean:write name="msg"/>
</html:messages>
</font>
<font color="blue">
<html:messages id="msg" property="error_2">
<bean:write name="msg"/>
</html:messages>
</font> </body>
</html>

3、编写相关的类

ChangeLanguageAction.java

package com.bjpowernode.struts;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping; /**
* 完成语言的手动切换
* @author Administrator
*
*/
public class ChangeLanguageAction extends Action { @Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String lang = request.getParameter("lang");
Locale locale = Locale.getDefault();
if ("zh".equals(lang)) {
locale = new Locale("zh", "CN");
}else if ("en".equals(lang)) {
locale = new Locale("en", "US");
}
//将Locale设置到session中
//request.getSession().setAttribute(Globals.LOCALE_KEY, locale);
this.setLocale(request, locale);
return mapping.findForward("index");
} }

LoginAction.java

package com.bjpowernode.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages; /**
* 登录Action
* 负责取得表单数据、调用业务逻辑、返回转向信息
*
* @author Administrator
*
*/
public class LoginAction extends Action { @Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception { LoginActionForm laf = (LoginActionForm)form;
String username = laf.getUsername();
String password = laf.getPassword(); UserManager userManager = new UserManager();
ActionMessages messages = new ActionMessages();
try {
userManager.login(username, password); //创建国际化消息文本 ActionMessage message = new ActionMessage("login.success", username);
messages.add("login_success_1", message); //传递国际化消息
this.saveMessages(request, messages);
return mapping.findForward("success");
}catch(UserNotFoundException e) {
e.printStackTrace();
//创建国际化消息文本
ActionMessage error = new ActionMessage("login.user.not.found", username);
messages.add("error_1", error); //传递国际化消息
this.saveErrors(request, messages);
}catch(PasswordErrorException e) {
e.printStackTrace();
//创建国际化消息文本
ActionMessage error = new ActionMessage("login.password.error");
messages.add("error_2", error); //传递国际化消息
this.saveErrors(request, messages);
}
return mapping.findForward("error");
} }

LoginActionForm.java

package com.bjpowernode.struts;

import org.apache.struts.action.ActionForm;

/**
* 登录ActionForm,负责表单收集数据
* 表单的属性必须和ActionForm中的get和set的属性一致
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class LoginActionForm extends ActionForm { private String username; private String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

PasswordErrorException.java

package com.bjpowernode.struts;

public class PasswordErrorException extends RuntimeException {

	public PasswordErrorException() {
// TODO Auto-generated constructor stub
} public PasswordErrorException(String message) {
super(message);
// TODO Auto-generated constructor stub
} public PasswordErrorException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
} public PasswordErrorException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
} }

UserManager.java

package com.bjpowernode.struts;

public class UserManager {

	public void login(String username, String password) {
if (!"admin".equals(username)) {
throw new UserNotFoundException();
} if (!"admin".equals(password)) {
throw new PasswordErrorException();
} }
}

UserNotFoundException.java

package com.bjpowernode.struts;

public class UserNotFoundException extends RuntimeException {

	public UserNotFoundException() {
// TODO Auto-generated constructor stub
} public UserNotFoundException(String message) {
super(message);
// TODO Auto-generated constructor stub
} public UserNotFoundException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
} public UserNotFoundException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
} }

4、对struts-config.xml的配置。

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config>
<form-beans>
<form-bean name="loginForm" type="com.bjpowernode.struts.LoginActionForm"/>
</form-beans> <action-mappings>
<action path="/login"
type="com.bjpowernode.struts.LoginAction"
name="loginForm"
scope="request"
>
<forward name="success" path="/login_success.jsp" /> <forward name="error" path="/login.jsp"/>
</action> <action path="/changeLang"
type="com.bjpowernode.struts.ChangeLanguageAction"
>
<forward name="index" path="/index.jsp"/>
</action>
</action-mappings> <message-resources parameter="resources.MessageResources" />
</struts-config>

三、感想。

国际化用到的地方很多,大型网站都有国际化文件。

菜鸟学习Struts——国际化的更多相关文章

  1. 菜鸟学习Struts——简易计算器

    这是学习Struts的一个简单的例子文件结构如下: 1.配置Struts环境 2.新建input.jsp,success.jsp,error.jsp input.jsp代码如下: <%@ pag ...

  2. 菜鸟学习Struts——配置Struts环境

    刚开始学习Struts,它通过采用JavaServlet/JSP技术,实现了基于Java EEWeb应用的MVC设计模式的应用框架,是MVC经典设计模式中的一个经典产品. 要用到Struts就要学会配 ...

  3. 菜鸟学习Struts——总结

    一.原理 客户端请求到ActionSeverlet,ActionSeverlet负责截URL进行分发分发到每一个Action上,Action负责和Model打交道然后把相关信息返回到ActionSev ...

  4. 菜鸟学习Struts——Scope属性

    一.概念. 在Action映射配置中,Scope属性可以取值为:request或session.Scope属性表示:Struts框架在将     ActionForm对象(与目标Action匹配的Ac ...

  5. 菜鸟学习Struts——bean标签库

    一.Struts标签库. Struts实际上包含了4个标签库:bean,logic,html,tiles bean:用来在属性范围中定义或取得属性的,同时可以读取资源文件信息 logic:替代JSTL ...

  6. 菜鸟学习SSH——目录

    菜鸟学习Struts--配置Struts环境 菜鸟学习Struts--简易计算器 菜鸟学习Struts--bean标签库 菜鸟学习Struts--Scope属性 菜鸟学习Struts--国际化 菜鸟学 ...

  7. 【菜鸟学习jquery源码】数据缓存与data()

    前言 最近比较烦,深圳的工作还没着落,论文不想弄,烦.....今天看了下jquery的数据缓存的代码,参考着Aaron的源码分析,自己有点理解了,和大家分享下.以后也打算把自己的jquery的学习心得 ...

  8. 菜鸟学习Andriod-弹窗

    菜鸟学习Andriod-弹窗 return new AlertDialog.Builder(ZyScreenSaver.this).setIcon( R.drawable.ic_launcher).s ...

  9. 菜鸟学习Spring——60s配置XML方法实现简单AOP

    一.概述. 上一篇博客讲述了用注解的形式实现AOP现在讲述另外一种AOP实现的方式利用XML来实现AOP. 二.代码演示. 准备工作参照上一篇博客<菜鸟学习Spring--60s使用annota ...

随机推荐

  1. 翻译「C++ Rvalue References Explained」C++右值引用详解 Part6:Move语义和编译器优化

    本文为第六部分,目录请参阅概述部分:http://www.cnblogs.com/harrywong/p/cpp-rvalue-references-explained-introduction.ht ...

  2. c语言描述简单的线性表,获取元素,删除元素,

    //定义线性表 #define MAXSIZE 20 typedef int ElemType; typedef struct { ElemType data[MAXSIZE]; //这是数组的长度, ...

  3. 下雪了-js下雪效果

    Jingle Bells,Jingle Bells,圣诞来临,做了一个下雪的小程序玩.有大雪花和小雪花. <!DOCTYPE html PUBliC "-//W3C//DTD XHTM ...

  4. Linux下搭建Lotus Domino集群

    Linux下搭建Lotus Domino 集群 本文内容是Linux平台下Lotus Domino服务器部署案例(http://chenguang.blog.51cto.com/350944/1334 ...

  5. leetcode022. Generate Parentheses

    leetcode 022. Generate Parentheses Concise recursive C++ solution class Solution { public: vector< ...

  6. linux程序调试常用命令

    1 调用跟踪 跟踪系统调用 strace ls –l 跟踪库调用  ltrace 2 lsof(list open file) 查看程序命令打开了哪些文件  lsof –p PID; lsof –c ...

  7. 公司内部openStack环境信息

    公司内部openStack环境信息 yrzl内部openStack云平台平台访问地址,openStack云平台版本为Juno版本内网地址:  http://192.168.3.5:9090/horiz ...

  8. 理解Linux启动过程

    传统的Linux系统启动过程主要由著名的init进程(也被称为SysV init启动系统)处理,而基于init的启动系统被认为有效率不足的问题,systemd是Linux系统机器的另一种启动方式,宣称 ...

  9. 简单的使用AngularJS的解析JSON

    使用AngularJS+Struts2进行前后台的数据交互与显示. struts.xml 配置文件需要将设置extends="json-default"  type="j ...

  10. C# 多线程传参

    using System; using System.Threading; //多线程调试: 2013.10.08 namespace ThreadExample { class App { publ ...