struts2输出带有占位符的国际化信息

来源:互联网 发布:微软人工智能小冰 编辑:程序博客网 时间:2024/05/22 13:27

第一步:

在给国际化资源文件配置属性的时候,我们有可能用到占位符{1}、{2}、{3}等等;

例如,我们现在给iName_zh_CN.properties配置一个属性welcome={0}、{1}_欢迎来到中国

在struts.xml文件中配置国际化信息文件:

<constant name="struts.custom.i18n.resources" value="itcast"/>

第二步:

那么如何给这个占位符赋值呢?很简单!

在jsp文件中:
我们先要引入struts标签,

<%@ taglib uri="/struts-tags" prefix="s"%>
给通配符赋值,并输出:

<s:text name="welcome">         <s:param>坤铭</s:param>          <s:param>思想领袖</s:param>     </s:text> 


第三步:

如何在action中访问,并给赋值呢?很简单!

让该Action先继承ActionSupport,调用getText方法

public class HelloWorldAction extends ActionSupport{public String execute(){ActionContext.getContext().put("message", this.getText("welcome", new String[]{"坤铭","思想领袖"}));this.getText(TextName, args)return "success";}}

搞定!

------------------------------------------------------------------------------------------------苦难不是博得同情的资本,唯有不断奋斗才能改变命运!




0 0
原创粉丝点击