struts2配置和引用资源文件

在struts.xml里面加入:
<constant name="struts.custom.i18n.resources" value="globalMessages" />
如下:
<?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="globalMessages" /> 多个配置文件以逗号分割,资源文件不在根目录时,要添加相对路径,value中的值不能带.properties。
<package name="test" namespace="/test" extends="struts-default">
<!-- 国际化支持 -->
<action name="i18n" class="com.test.web.HelloAction">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>

myeclipse在src下新建

globalMessages_zh_CN.properties
hello=\u4F60\u597D\u4E16\u754C

globalMessages_en_US.properties

hello=Hello World\!

hello.jsp中可以输出
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>test</title>
</head>

<body>
${param.username}
${param.votedate}
<br/>
<s:text name="hello"/>
<br/>
通过request获取:${requestScope.hello}
</body>
</html>

HelloAction.java里面execute方法可以将hello获取到,并加入大request对象中
public String execute() throws Exception {
this.message="成功登陆";v
ActionContext.getContext().put("hello", this.getText("hello");
System.out.println(this.getText("hello"));
return "success";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值