login.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% 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> <s:text name="tip"/><br/> <s:form action="login.action"> <s:textfield key="username" name="username"/><br/> <s:password key="password" name="password"/><br/> <s:submit key="submit"/> <s:reset key="reset"/> </s:form><br/> <a href="tochn.action"><s:text name="tochn"/></a> <a href="toeng.action"><s:text name="toeng"/></a> </body> </html>
welcome.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% 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 'welcome.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="sucinfo"/> </body> </html>
error.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% 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 'welcome.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="failinfo"/> </body> </html>
LoginAction package com.etc.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { 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; } public String execute() { //java代码中的国际化 System.out.println(this.getText("username")); if("123".equals(password)&&"admin".equals(username)) return "success"; return "fail"; } }
LanguageAction package com.etc.action; import java.util.Locale; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LanguageAction extends ActionSupport { //转化成中文 public String tochn() { Locale loc = new Locale("zh","CN"); //修改当前action上下文的语言配置 ActionContext.getContext().setLocale(loc); //通过session配置,让新的语言对本会话生效 ServletActionContext.getRequest().getSession() .setAttribute("WW_TRANS_I18N_LOCALE",loc); return "success"; } //转化成英文 public String toeng() { Locale loc = new Locale("en","US"); //修改当前action上下文的语言配置 ActionContext.getContext().setLocale(loc); //通过session配置,让新的语言对本会话生效 ServletActionContext.getRequest().getSession() .setAttribute("WW_TRANS_I18N_LOCALE",loc); return "success"; } }
<?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.custom.i18n.resources" value="mess"/> <constant name="struts.devMode" value="true"/> <package name="p" extends="struts-default" namespace="/"> <action name="login" class="com.etc.action.LoginAction"> <result name="success"> /welcome.jsp </result> <result name="fail"> /error.jsp </result> </action> <action name="*" class="com.etc.action.LanguageAction" method="{1}"> <result> /login.jsp </result> </action> </package> </struts>
mess_en_US.properties 英文文件 文件命名一定要注意:xxx_en_US.properties tip=Please input your name and password username=User Name password=Your password submit=Login reset=Reset sucinfo=Login ok.You are Welcome. failinfo=Login fail. tochn=Chinese Ver. toeng=English Ver.
mess_zh_CN.properties 中文文件 tip=u8BF7u8F93u5165u7528u6237u540Du548Cu5BC6u7801 username=u7528u6237u540D password=u5BC6u7801 submit=u767Bu5F55 reset=u91CDu7F6E sucinfo=u767Bu5F55u6210u529FuFF01u6B22u8FCEu4F7Fu7528u3002 failinfo=u7528u6237u540Du6216u5BC6u7801u9519u8BEF tochn=u4E2Du6587u7248 toeng=u82F1u6587u7248