企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 一、概念 国际化:界面上的语言可以根据用户所在的地区改变显示语言。 如图: ![](https://box.kancloud.cn/2016-02-22_56caddfa3150a.jpg) ## 二、实例 下面就一步一步的教大家利用Struts实现国际化。 ### 1、编写资源文件 这个资源文件就是界面上显示的字符,资源文件里面包含英文和中文的资源文件这样我们就可以转换资源文件来实现把界面上的中文、英文互相转化。 [这里下载资源文件>>](http://download.csdn.net/detail/gwblue/7067467) ### 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> ~~~ ##三、感想。 国际化用到的地方很多,大型网站都有国际化文件。