Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

feilongDisplay loadBundle

feilong edited this page Jun 11, 2017 · 1 revision

将i18n配置文件,转成map,加载到request 作用域, 实现国际化功能,简化开发

自定义标签参数说明:

参数 说明 是否必须required 默认 是否支持动态参数
baseName 配置文件的路径,如果在i18n文件下面有 edu-en.properties那么baseName就是去掉后缀,并且去掉语言的值:i18n/edu true true
locale 国际化语言,支持 java.util.Locale 或 String 类型的实例 false   request.getLocale() true
var 存储在作用于里面的属性名 true true

示例:

1.jsp 顶端 申明引用

	<%@ taglib prefix="feilongDisplay" uri="http://java.feilong.com/tags-display"%>
	
	
	<%@ taglib prefix="feilong" uri="http://java.feilong.com/tags-common"%>
	<%@ taglib prefix="fl" uri="http://java.feilong.com/el-common"%>

2.代码编写

场景描述: 假设有 i18n/education_zh_CN.properties 内容如下:

	edu.option1=初中
	edu.option2=高中
	edu.option3=中专
	edu.option4=大专
	edu.option5=本科
	edu.option6=硕士
	edu.option7=博士
	edu.option8=其他

2.1 在jsp中渲染成 复选框 项,

我们可以使用下面的方式:

	
	<feilongDisplay:loadBundle var="educationMap" baseName="i18n/education" />

	<c:forEach var="educationEntry" items="${educationMap }">
		<feilong:isContains value="${educationEntry.key }" collection="${paramValues['educationCheckbox1'] }">
			<input name="educationCheckbox1" type="checkbox" value="${educationEntry.key }" checked="checked" />
		</feilong:isContains>
		
		<feilong:isNotContains value="${educationEntry.key }" collection="${paramValues['educationCheckbox1'] }">
			<input name="educationCheckbox1" type="checkbox" value="${educationEntry.key }" />
		</feilong:isNotContains>
		
		${educationEntry.value }&nbsp;
	</c:forEach>

此时页面渲染结果为

	<input name="educationCheckbox1" type="checkbox" value="edu.option1" />
		初中&nbsp;
	<input name="educationCheckbox1" type="checkbox" value="edu.option2" />
		高中&nbsp;
	<input name="educationCheckbox1" type="checkbox" value="edu.option3" />
		中专&nbsp;
	<input name="educationCheckbox1" type="checkbox" value="edu.option4" />
		大专&nbsp;
	<input name="educationCheckbox1" type="checkbox" value="edu.option5" />
		本科&nbsp;
	<input name="educationCheckbox1" type="checkbox" value="edu.option6" />
		硕士&nbsp;
	<input name="educationCheckbox1" type="checkbox" value="edu.option7" />
		博士&nbsp;
	<input name="educationCheckbox1" type="checkbox" value="edu.option8" />
		其他&nbsp;

2.2 关于 locale:

如果此时我们有英文语言站点,他的配置文件是 i18n/education_en.properties,内容如下:

	edu.option1=Middle School
	edu.option2=Junior College
	edu.option3=Bachelor
	edu.option4=Others(Master/PHD)

可以传递 locale参数,如下:

	<%
        //此处是演示代码,实际开发过程中,不允许使用  <% %> 里面写jsp scriptlet
        request.setAttribute("enLocale", Locale.ENGLISH);
    %>
    
	<feilongDisplay:loadBundle var="educationMap" baseName="i18n/education" locale="${enLocale}" />

	<c:forEach var="educationEntry" items="${educationMap }">
		<input name="educationCheckbox2" type="checkbox" value="${educationEntry.key }" ${fl:contains(paramValues.educationCheckbox2,educationEntry.key)?'checked="checked"':''} />${educationEntry.value }&nbsp;
	</c:forEach>

返回:

	<input name="educationCheckbox2" type="checkbox" value="edu.option1" />Middle School&nbsp;
	<input name="educationCheckbox2" type="checkbox" value="edu.option2" />Junior College&nbsp;
	<input name="educationCheckbox2" type="checkbox" value="edu.option3" />Bachelor&nbsp;
	<input name="educationCheckbox2" type="checkbox" value="edu.option4" />Others(Master/PHD)&nbsp;