Struts2_08_Struts2国际化实例

本文介绍如何使用Struts2框架实现网站的国际化功能,包括创建properties文件、编写Action类及JSP页面,以及在struts.xml中配置相关参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 类

什么都没有,直接返回success

package 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、运行结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值