文件命名格式如: xxx_zh_CN.properties 中文
xxx_en_US.properties 英文
以一登陆界面index.jsp为例。
导入struts2常用包,案例struts包在这里
src下创建struts2.xml文件
WEB-INF创建拦截器web.xml
部分代码如下:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在jsp页面使用标签,用i18n替换原本的html
<%@ 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">
<s:i18n name="resource.login">
<html>
<head>
<base href="<%=basePath%>">
<title><s:text name="title"></s:text> </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:form action="">
<s:textfield name="username" key="user"></s:textfield>
<s:textfield name="password" key="pass"></s:textfield>
<s:submit key="submit"></s:submit>
</s:form>
</body>
</html>
</s:i18n>
创建src建包resource,创建特性文件
login_zh_CN.properties
login_en_US.properties
打开运行tomcat服务器,打开网页
更改浏览器为en-us语言
重新打开登陆页面