Struts2使用native2ascii命令处理国际化资源文件

本文介绍了如何在Eclipse中创建并处理国际化资源文件*.properties,包括设置UTF-8编码,使用native2ascii命令转换非西欧字符,生成*_zh_CN.properties文件,并在Struts2的struts.xml中配置加载这些资源,实现页面的国际化显示。

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

*.properties文件是Java的配置文件,当然也可以来配置国际化资源。


1、在eclipse中创建properties文件
首先,在Window-preferences-General-ContextType中,选择Text-Java Properties File。若File associations中无“*.properties”,则去添加它。并且在Default encoding中改为“UTF-8”

如图所示:

这里写图片描述

然后在/src目录下创建properties文件(new-File),例:c.properties文件
c.properties文件为:

user=JSP消息:用户名
pass=JSP消息:密码
login=JSP消息:登录

示例的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>
<s:form>
<s:textfield name="username" key="user"></s:textfield>
<s:textfield name="password" key="pass"></s:textfield>
<s:submit key="login"></s:submit>
</s:form>
</body>
</html>

注意key属性


2、因为*.properties文件出现了非西欧字符,所以需用使用native2ascii命令去处理这份文件,生成的新文件名为 *_zh_CN.properties文件。
找到*.properties文件目录下,按下“SHIFT”+右键,打开命令窗口,输入:
native2ascii -encoding UTF-8 *.properties *_zh_CN.properties
按下回车,则生成*_zh_CN.properties文件

这里写图片描述

再在Eclipse中 refresh就可出现该文件

这里写图片描述


3、在struts.xml中加入如下语句去加载国际化资源文件
<constant name="struts.custom.i18n.resources" value="c"/>(此例为c)


4、运行jsp文件
如图所示:

这里写图片描述
此时,已经转化为国际化资源信息

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值