您的位置:首页 > 编程语言 > Java开发

Struts2_08_Struts2国际化实例

2018-03-03 10:59 288 查看

1、建立properties文件

在src目录下建立如下形式的三个文件
   baseName_language_country.properties
   baseName_language.properties

   baseName.properties



globe_en_US.properties



globe_zh_CN.properties,其中\u7528这些是中文的Unicode编码





globe.properties



2、建立LongAction 类

什么都没有,直接返回successpackage com.java1234.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}

3、建立login.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>            
<a href="lang.action?request_locale=zh_CN">中文</a><!--sturts2根据参数显示中文还是英文-->
<a href="lang.action?request_locale=en_US">English</a>
<br/><s:text name="welcomeInfo">
<s:param>Jack</s:param>
</s:text>
<table>
<tr>
<td><s:text name="userName"></s:text></td>
<td>
<input type="text"/>
</td>
</tr>
<tr>
<td><s:text name="password"></s:text></td>
<td>
<input type="text"/>
</td>
</tr>
<tr>
<td>
<input type="button" value="<s:text name='login'></s:text>"/>
</td>
</tr>
</table>
</body>
</html>

4、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="globe"></constant>
<package name="login" extends="struts-default" namespace="/">

<action name="lang" class="action.LangAction">
<result >/login.jsp</result>
</action>
</package>
</struts>

5、运行结果



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Struts