web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>
</web-app>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="app" />
<package name="default" namespace="/" extends="struts-default">
<action name="firstAction" class="com.huawei.s2.action.FirstAction" >
<result name="success" >/ok.jsp</result>
</action>
</package>
</struts>
src下的配置文件
app_en_US.properties:
welcome.msg=welcome to beijing
app_zh_CN.properties:
welcome.msg=\u5317\u4EAC\u6B22\u8FCE\u4F60
action:
import com.opensymphony.xwork2.ActionSupport;
//ActionSupport:资源国际化及表单验证类
public class FirstAction extends ActionSupport{
public String execute(){
String str = getText("welcome.msg");//会根据浏览器设置的语言来读取相应的信息
System.out.println(str+"============================");
return "success";
}
}
jsp:
1.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>struts2资源国际化</title>
</head>
<body>
<form action="firstAction">
<input name="uname" value="zhangsan" /><br/>
<input type="submit" value="提交" />
</form>
</body>
</html>
ok.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'ok.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">
</head>
<body>
<h1>提交成功</h1>
</body>
</html>