日期:2014-05-16  浏览次数:20452 次

收藏 JSP自定义标签EL表达式取值问题

源:http://bbs.csdn.net/topics/210040287

评:

给你个标签的例子自己看看吧!
标签:<framework:VoucherCodeToName?code=""?/>

tld.xml


<tag>
??????? <name>VoucherCodeToName</name>
??????? <tagclass>com.itown.crm.fee.vouchermanager.util.VoucherCodeToNameTag</tagclass>
??????? <bodycontent>JSP</bodycontent>
??????? <attribute>
??????????? <name>code</name>
??????????? <required>true</required>
??????????? <rtexprvalue>true</rtexprvalue>
??????? </attribute>
</tag>
VoucherCodeToNameTag.java

package?com.itown.crm.fee.vouchermanager.util;

import?java.util.ArrayList;

import?javax.servlet.jsp.JspException;
import?javax.servlet.jsp.tagext.TagSupport;

import?org.apache.commons.lang.StringUtils;
import?org.apache.taglibs.standard.tag.el.core.ExpressionUtil;

import?com.itown.crm.fee.vouchermanager.po.FeeVoucherInfo;
import?com.itown.crm.util.DaoUtil;
import?com.itown.framework.persistence.CachedRowSet;
import?com.itown.util.ormapping.BeanHelper;

public?class?VoucherCodeToNameTag?extends?TagSupport?{
	
	protected?String?code?=?"";

	public?String?getCode()?{
		return?code;
	}

	public?void?setCode(String?code)?{
		this.code?=?code;
	}
	
	public?int?doStartTag()?throws?JspException?{
		//解析表达式
		evaluateExpressions();
		//如果属性值为NUll或者为"",则不进行转换
		if(StringUtils.isBlank(code))?{
			return?super.doStartTag();
		}
		try?{
			String?name?=?null;
			StringBuffer?sql?=?new?StringBuffer();
			sql.append("select?*?from?FEE_VOUCHER_INFO?where?VOUCHER_ID='");
			sql.append(code);
			sql.append("'");

			CachedRowSet?rs?=?DaoUtil.executeQuery(sql.toString());
			
			ArrayList?list?=?BeanHelper.mapBeansByRS(rs,?FeeVoucherInfo.class);
			if(list.size()?>?0){
				name?=?((FeeVoucherInfo)list.get(0)).getVoucherName();
			}
			this.pageContext.getOut().print(StringUtils.trimToEmpty(name));
		}?catch?(Exception?e)?{
			e.printStackTrace();
		}
		return?SKIP_BODY;
	}
	private?void?evaluateExpressions()?{
		try?{
			code?=?(String)?ExpressionUtil.evalNotNull("VoucherCodeToName",?"code",?code,?java.lang.Object.class,?this,?pageContext);?//第一个参数为这个标签的名称
????????}
????????catch(Exception?ex)?{
????????	code?=?null;
????????}
	}

}

?