EL--ExpressinLanguage表达式中函数的用法

EL--ExpressinLanguage表达式中函数的用法,今天碰到一段代码:

laneAnalysis.jsp

<div class="block_title">
			<h3>${app:i18n('rut.analysis.laneanalysisview.expfilename')}</h3>
		</div>

知道这应该是EL表达式,但是不太理解:的意思,后来上网查询了下,这是app类中的函数i18n,括号内是其参数;

相关代码如下:

laneAnalysis.jsp的引用:

<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>

taglibs.jsp中的内容:

<%@ taglib uri="/struts-tags" prefix="s" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="/app-tags" prefix="app" %>
<%@ taglib uri="/sso-tags" prefix="sso" %>

app-tags.tld中的内容:

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">

	<description><![CDATA[Application TagLib]]></description>
	<display-name>Application Tags</display-name>
	<tlib-version>2.0</tlib-version>
	<short-name>app</short-name>
	<uri>/app-tags</uri>


..........

          <function>
		<description><![CDATA[get global resource message]]></description>
		<name>i18n</name>
		<function-class>com.ibm.gbs.ai.portal.framework.browser.tags.I18nEl</function-class>
		<function-signature>java.lang.String i18n(java.lang.String)</function-signature>
	</function>


I18nEl.java中的代码;

package com.ibm.gbs.ai.portal.framework.browser.tags;

import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ibm.gbs.ai.portal.framework.server.context.UserContext;
import com.ibm.gbs.ai.portal.framework.server.i18n.IMessageSource;
import com.ibm.gbs.ai.portal.framework.server.i18n.MessageSourceContainer;

/**
 * 国际化EL表达式
 * 使用如:
 * <%@ taglib prefix="app" uri="/app-tags"%>
 * ${app:i18n(‘key’)}
 * ${app:i18n_arg1(‘key’, arg1)}
 * ${app:i18n_arg2(‘key’ , arg1, arg2)}
 * ${app:i18n_arg3(‘key’ , arg1, arg2, arg3)}
 * ${app:i18n_arg4(‘key’ , arg1, arg2, arg3, arg4)}
 * ${app:i18n_args(‘key’ , new Object[]{arg1, arg2 })}
 * ${app:i18n_def(‘key’,’defaultValue’)}
 * ${app:i18n_def_arg1 (‘key’,’defaultValue’,arg1)}
 * ${app:i18n_def_arg2(‘key’ ,’defaultValue’, arg1, arg2)}
 * ${app:i18n_def_arg3(‘key’ ,’defaultValue’, arg1, arg2, arg3)}
 * ${app:i18n_def_arg4(‘key’ ,’defaultValue’ , arg1, arg2, arg3, arg4)}
 * ${app:i18n_def_args(‘key’ ,’defaultValue’ , new Object[]{arg1, arg2})}
 *
 * @author 
 *
 */

/**
 * modify memo:
 * added i18n_func(String key, String value,String defaultValue)
 */
public class I18nEl {

	protected static Log logger = LogFactory.getLog(I18nEl.class);
	
	static MessageSourceContainer messageSourceContainer = MessageSourceContainer.getInstance ();

	public static final String concat(String str1, String str2) {
		return str1 + str2;
	}

	public static final String i18n(String key) {

		Locale locale = UserContext.getUserContext().getLocale();
		
		IMessageSource messageSource = messageSourceContainer.getMessageSource(locale);
		
		String retVal = null;
		if(messageSource!=null){
			retVal = messageSource.getMessage ( key );
		}

		if(null==retVal ) {
			logger.info("找不到" + key);
			logger.info(locale);
			//logger.debug("找不到" + key);
			return "";
		}

		return retVal;
	}

	public static final String i18n_arg1(String key, Object arg0) {
		return i18n_args(key, new Object[]{arg0});
	}

	public static final String i18n_arg2(String key, Object arg0, Object arg1) {
		return i18n_args(key, new Object[]{arg0, arg1});
	}

	public static final String i18n_arg3(String key, Object arg0, Object arg1, Object arg2) {
		return i18n_args(key, new Object[]{arg0, arg1, arg2});
	}

	public static final String i18n_arg4(String key, Object arg0, Object arg1, Object arg2, Object arg3) {
		return i18n_args(key, new Object[]{arg0, arg1, arg2, arg3});
	}

	public static final String i18n_func_def(String key, String propertyValue, String defaultValue) {
		Locale locale = UserContext.getUserContext().getLocale();

		IMessageSource msgRes = messageSourceContainer.getMessageSource(locale);
		String retVal = null;
		if(msgRes!=null){
			retVal = msgRes.getMessage(key, new String[]{propertyValue}, defaultValue);
		}

		if(null==retVal ) {
			if(logger.isDebugEnabled()){
				logger.debug("找不到" + key);
			}
			//logger.debug("找不到" + key);
			return "";
		}

		return retVal;
	}

	public static final String i18n_func(String key, String propertyValue) {
		Locale locale = UserContext.getUserContext().getLocale();
		IMessageSource msgRes = messageSourceContainer.getMessageSource(locale);
		String retVal = null;
		if(msgRes!=null){
			retVal = msgRes.getMessage(key,propertyValue);
		}

		if(null==retVal ) {
			if(logger.isDebugEnabled()){
				logger.debug("找不到" + key);
			}
			//logger.debug("找不到" + key);
			return "";
		}

		return retVal;
	}

	public static final String i18n_func_bln(String key, Boolean b) {
		Locale locale = UserContext.getUserContext().getLocale();

		IMessageSource msgRes = messageSourceContainer.getMessageSource(locale);
		String retVal = null;
		if(msgRes!=null){
			retVal = msgRes.getMessage(key, b);
		}

		if(null==retVal ) {
			if(logger.isDebugEnabled()){
				logger.debug("找不到" + key);
			}
			//logger.debug("找不到" + key);
			return "";
		}

		return retVal;
	}

	public static final String i18n_args(String key, Object[] args) {
		Locale locale = UserContext.getUserContext().getLocale();

		IMessageSource msgRes = messageSourceContainer.getMessageSource(locale);
		String retVal = null;
		if(msgRes!=null){
			retVal = msgRes.getMessage(key, args);
		}

		if(null==retVal ) {
			if(logger.isDebugEnabled()){
				logger.debug("找不到" + key);
			}
		//	logger.debug("找不到" + key);
			return "";
		}

		return retVal;
	}

	public static final String i18n_def(String key, String defaultValue) {
		Locale locale = UserContext.getUserContext().getLocale();

		IMessageSource msgRes = messageSourceContainer.getMessageSource(locale);
		String retVal = null;
		if(msgRes!=null){
			retVal = msgRes.getMessage(key);
		}

		if(null==retVal ) {
			if(logger.isDebugEnabled()){
				logger.debug("找不到" + key);
			}
			//logger.debug("找不到" + key);
			return "";
		}

		return retVal;
		//return ApplicationContext.getContext().getMessageSource().getMessage(key, defaultValue);
	}

	public static final String i18n_def_arg1(String key, String defaultValue, Object arg0) {
		return i18n_def_args(key, defaultValue, new Object[]{arg0});
	}

	public static final String i18n_def_arg2(String key, String defaultValue, Object arg0, Object arg1) {
		return i18n_def_args(key, defaultValue, new Object[]{arg0, arg1});
	}

	public static final String i18n_def_arg3(String key, String defaultValue, Object arg0, Object arg1, Object arg2) {
		return i18n_def_args(key, defaultValue, new Object[]{arg0, arg1, arg2});
	}

	public static final String i18n_def_arg4(String key, String defaultValue, Object arg0, Object arg1, Object arg2, Object arg3) {
		return i18n_def_args(key, defaultValue, new Object[]{arg0, arg1, arg2, arg3});
	}

	public static final String i18n_def_args(String key, String defaultValue, Object[] args) {
		Locale locale = UserContext.getUserContext().getLocale();

		IMessageSource msgRes = messageSourceContainer.getMessageSource(locale);
		String retVal = null;
		if(msgRes!=null){
			retVal = msgRes.getMessage(key, args, defaultValue);
		}

		if(null==retVal ) {
			//logger.debug("找不到" + key);
			if(logger.isDebugEnabled()){
				logger.debug("找不到" + key);
			}
			return "";
		}

		return retVal;
	}

	//TODO 菜单的国际化需要补充进来
	//public static final String i18n_menu(String key) {
	//	return ApplicationContext.getContext().getMenuMessageSource().getMessage(key);
	//}

	//public static final String i18n_menu_def(String key, String defaultValue) {
	//	return ApplicationContext.getContext().getMenuMessageSource().getMessage(key, defaultValue);
	//}

}


=======================================================================================================

下面补充上EL的相关知识:

EL——ExpressionLanguage,它是用于替换JSP页面中的脚本表达式。之前也有提到过,在JSP页面中使用脚本表达式进行数据显示是一件痛苦的事。若想使用EL表达式,WEB服务器必须支持Servlet2.4/JSP2.0技术。EL都有哪些功能呢?比如${标识符},EL拿到标识符后会到自己的隐式对象中去查找,如果隐式对象中不存在,则到page,request,session,application中去查找。

举例:${andy},andy不是EL的隐式对象,但它在request域中(request.setAttribute("andy","wcl");),所以会调用pageContext.findAttribute方法进行查找,

${andy}代表的就是request.getAttribute("abcd");

EL的隐式对象:pageContext、pageScope、requestScope、sessionScope、applicationScope,param、paramValues、header、headerValues、cookie、initParam。这些都是Servlet中相关的作用域对象,比如${requestScope.abcd}就是获取request中的abcd属性。既然${abcd}与${requestScope.abcd}有什么区别,使用${abcd}不是更简单吗?咱们来看一下这种情况:request.setAttribute(“abcd”,”hiRequest”); session.setAttribute(“abcd”,”hiSession”);,${abcd},获取的是request中的abcd,如果request中没有abcd它获取的就是session的abcd。这下明白了吧!如果两个域中具有相同的属性,那么些时EL的隐式对象就发挥了作用。EL中的隐式对象完全可以操作相应域的数据


EL也具有基本的运算处理能力:算术运算、逻辑运算和关系运算。在此就不一一列举了!


重点的是:EL中也可以编写自己的函数:

1,编写一个累,定义静态方法:

public class MyEl {
     public static String getMessage(){
        return "HI,我就是EL自定义函数";
     }
}

2,在WEB-INF目录中添加一个*.tld配置文件:

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"

    version="2.0">

<description>A tag library exercising SimpleTag handlers</description>
<tlib-version>1.0</tlib-version>
<short-name>SimpleTagLibrary</short-name
>
<uri>/SimpleTagLibrary</uri>

<function>
    <name>print</name>
    <function-class>com.andy.MyEl</functin-cla
ss>
    <function-signature>java.lang.String getMessage()</function-signature>
</function>


3,在需要调用本函数的页面中添加引用:

<%@tablib uri="/SimpleTagLibrary" prefix="cc"%>

4,调用方法

${cc:print()}


上面就是编写自定义EL函数的过程,Apache组织为EL提供了一些比较常用的函数。比如文件处理函数,以后不需要在Servlet中把处理好的文本传递给页面,可以在页面中直接使用Apache提供的函数来方便快捷的处理文本。

Apache提供的EL常用函数在Standard.jar包中的“org.apache.taglibs.standard.functions.Functions”包中。UIR是http://localhost:8080/jsp/jstl/functions












posted @ 2012-12-07 22:37  龙猫爸爸  阅读(271)  评论(0编辑  收藏  举报