0
点赞
收藏
分享

微信扫一扫

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

林肯公园_97cc 2022-07-27 阅读 53


国际化-输出带点位符的国际化信息:

资源文件中的内容如下:
welcome={0},欢迎来到北大青鸟{1}

在jsp页面中输出带点位符的国际信息
<s:text name="welcome">
<s:param>小明</s:param>
<s:param>学习</s:param>
</s:text>

在Action类中获取带点位符的国际化信息,可以getText(String kye,String[] args)
或getText(String key,List<?> args)方法。


两个.properties文件



welcome={0},welcom to bdqn{1}

welcome={0},欢迎来到北大青鸟{1}

两个struts.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<!-- 引用外部struts.xml文件 -->
<include file="struts_native.xml" />
</struts>


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<!-- 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法和freemarker、velocity的输入 -->
<constant name="struts.custom.i18n.resources" value="itcast" />

<package name="besa" namespace="/demo" extends="struts-default">
<action name="manage" class="cn.itcast.action.PersonManageAction" method="execute">
<result name="message">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>


package cn.itcast.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PersonManageAction extends ActionSupport {

@Override
public String execute() throws Exception {
// List<String> s = new ArrayList<String>();
// s.add("小明");
// s.add("你好");
// ActionContext.getContext().put("message", this.getText("welcome", s));
ActionContext.getContext().put("message", this.getText("welcome", new String[] { "小明", "学习" }));
return "message";
}
}


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>国际化</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>

<body>
<s:text name="welcome">
<s:param>小明</s:param>
<s:param>学习</s:param>
</s:text>
</body>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<title>结果</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">


</head>

<body>
${message }

<h1>中国万岁</h1>
</body>
</html>


举报

相关推荐

0 条评论