/**********************************************Author:Java619*Time:2007-02-15**********************************************/
本文利用自定义标签实现用于产生select列表的选项,并可以指定要选中的项(无重复项),实例中用到了Jakarta commons-beanutil.jar包
1.自定义标签 OptionsTag.java
/** */ /** * OptionsTag.java * Created on Dec 23, 2006 11:11:12 AM * Author:ceun */ package com.ceun.tag; /** */ /** * ceun标签库 * */ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Collection; import java.util.Iterator; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; import org.apache.commons.beanutils.PropertyUtils; import com.ceun.util.RequestUtils; /** */ /** * <p>主要用于产生select列表的选项,并可以指定要选中的项</p> * * 主要用于产生select列表的选项,并可以指定要选中的项 * <br>属性说明: <br>collection: 包含目标元素的集合的名称(必须是实现Collection接口的集合类,如Vector)(必须) <br>label: 将目标元素的label值指定的属性作为显示值(必须) <br>value: 将目标元素的value值指定的属性作为选项值(必须) <br>select: 要指定元素的value值或是包含要选中值的bean的名称(可选) <br>property: bean的属性(可选) <br>scope 指定collection及bean的查找范围,其值是形如request:page,(可选) <br> 以":"分隔前者指明collection范围,后者指明bean范围 <br> 可以范围有page,request,session,application <br>例: <br>若在某个范围(如request)内存在名为subjects集合对象以及一个名为stu且 <br>具有id属性的JavaBean,我们假设用subjects集合中元素的name属性作为选项显示值 <br>id属性作为option值(value),并设置列表选中id与stu.id相同的选项 <br><ceun:options collection="subjects" label="name" <br>value="id" select="stu" property="id" /> <br>或 <br><ceun:options collection="terns" label="tern" <br>value="tern" select="stu" property="tern" scope="request:page"/> <br>说明:scope="request:page"表明terns集合对象存在于request范围 <br>而stu JavaBean存在于page范围 * */ public final class OptionsTag extends TagSupport ... { private static final long serialVersionUID = 1L; private String collection = null;// 包含目标元素的集合 private String label = null;// 将目标元素的label值指定的属性作为显示值 private String value = null;// 将目标元素的value值指定的属性作为选项值 private String select = null;// 要指定元素的value值或是包含要选中值的bean的名称 private String property = null;// bean的属性 /** *//** * 指定collection及bean的查找范围,其值是形如request:page, * 以":"分隔前者指明collection范围,后者指明bean范围 */ private String scope = null; /** *//** * @exception IllegalAccessException * @exception InvocationTargetException * @exception NoSuchMethodException * @exception IOException */ public int doEndTag() throws JspException ...{ String[] scopes = new String[2]; if (scope != null) scopes = scope.split(":"); Collection elements = (Collection) RequestUtils.lookup(pageContext, collection, scopes[0]); if (elements == null) return (EVAL_PAGE); Object selectValue=null; if(select!=null) selectValue = RequestUtils.lookup(pageContext, select, property, scopes[1]); Iterator iterator = elements.iterator(); JspWriter out = pageContext.getOut(); while (iterator.hasNext()) ...{ Object element = iterator.next(); Object lab;// 显示值 Object v;// 选项值 try ...{ lab = PropertyUtils.getSimpleProperty(element, label); v = PropertyUtils.getSimpleProperty(element, value); out.print("<option value="" + v + "" "); //若selectValue为空则无选项被选中 if (selectValue!=null&&v.toString().equals(selectValue.toString())) out.print("selected"); out.println(">" + lab + "</option>"); } catch (IllegalAccessException e) ...{ e.printStackTrace(); } catch (InvocationTargetException e) ...{ e.printStackTrace(); } catch (NoSuchMethodException e) ...{ e.printStackTrace(); } catch (IOException e) ...{ e.printStackTrace(); } } return (EVAL_PAGE); } public int doStartTag() throws JspException ...{ return SKIP_BODY; } public void release() ...{ super.release(); this.collection = null; this.label = null; this.value = null; this.select = null; this.scope = null; this.property = null; } public void setCollection(String collection) ...{ this.collection = collection; } public String getCollection() ...{ return this.collection; } /** *//** * @return label */ public String getLabel() ...{ return label; } /** *//** * @param label * 要设置的 label */ public void setLabel(String label) ...{ this.label = label; } /** *//** * @return value */ public String getValue() ...{ return value; } /** *//** * @param value * 要设置的 value */ public void setValue(String value) ...{ this.value = value; } /** *//** * @return select */ public String getSelect() ...{ return select; } /** *//** * @param select * 要设置的 select */ public void setSelect(String select) ...{ this.select = select; } /** *//** * @return property */ public String getProperty() ...{ return property; } /** *//** * @param property * 要设置的 property */ public void setProperty(String property) ...{ this.property = property; } /** *//** * @return scope */ public String getScope() ...{ return scope; } /** *//** * @param scope * 要设置的 scope */ public void setScope(String scope) ...{ this.scope = scope; }}2.辅助类 RequestUtils.java
/** */ /** * RequestUtils.java * Created on 2006-12-23 上午09:52:50 */ package com.ceun.util; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import org.apache.commons.beanutils.PropertyUtils; /** */ /** * @author ceun * */ public class RequestUtils ... { private static Map scopes = new HashMap(); static ...{ scopes.put("page", new Integer(PageContext.PAGE_SCOPE)); scopes.put("request", new Integer(PageContext.REQUEST_SCOPE)); scopes.put("session", new Integer(PageContext.SESSION_SCOPE)); scopes.put("application", new Integer(PageContext.APPLICATION_SCOPE)); } /** *//** * Locate and return the specified bean, from an optionally specified * scope, in the specified page context. If no such bean is found, * return <code>null</code> instead. If an exception is thrown, it will * have already been saved via a call to <code>saveException()</code>. * * @param pageContext Page context to be searched * @param name Name of the bean to be retrieved * @param scopeName Scope to be searched (page, request, session, application) * or <code>null</code> to use <code>findAttribute()</code> instead * @return JavaBean in the specified page context * @exception JspException if an invalid scope name * is requested * @since ceunUtil 1.2.1 */ public static Object lookup(PageContext pageContext, String name, String scopeName) throws JspException ...{ if (scopeName == null) ...{ return pageContext.findAttribute(name); } try ...{ return pageContext.getAttribute(name, getScope(scopeName)); } catch (JspException e) ...{ saveException(pageContext, e); throw e; } } /** *//** * Converts the scope name into its corresponding PageContext constant value. * @param scopeName Can be "page", "request", "session", or "application" in any * case. * @return The constant representing the scope (ie. PageContext.REQUEST_SCOPE). * @throws JspException if the scopeName is not a valid name. * @since ceunUtil 1.2.1 */ public static int getScope(String scopeName) throws JspException ...{ Integer scope = (Integer) scopes.get(scopeName.toLowerCase()); if (scope == null) ...{ throw new JspException("未指定范围"); } return scope.intValue(); } /** *//** * Locate and return the specified property of the specified bean, from * an optionally specified scope, in the specified page context. If an * exception is thrown, it will have already been saved via a call to * <code>saveException()</code>. * * @param pageContext Page context to be searched * @param name Name of the bean to be retrieved * @param property Name of the property to be retrieved, or * <code>null</code> to retrieve the bean itself * @param scope Scope to be searched (page, request, session, application) * or <code>null</code> to use <code>findAttribute()</code> instead * @return property of specified JavaBean * * @exception JspException if an invalid scope name * is requested * @exception JspException if the specified bean is not found * @exception JspException if accessing this property causes an * IllegalAccessException, IllegalArgumentException, * InvocationTargetException, or NoSuchMethodException */ public static Object lookup( PageContext pageContext, String name, String property, String scope) throws JspException ...{ // Look up the requested bean, and return if requested Object bean = lookup(pageContext, name, scope); if (bean == null) ...{ JspException e = null; if (scope == null) ...{ e = new JspException("在指定范围内找不到对象"); } else ...{ e = new JspException("找不到对象"); } saveException(pageContext, e); throw e; } if (property == null) ...{ return bean; } // Locate and return the specified property try ...{ return PropertyUtils.getProperty(bean, property); } catch (IllegalAccessException e) ...{ saveException(pageContext, e); throw new JspException("不能访问指定对象的属性"); } catch (InvocationTargetException e) ...{ Throwable t = e.getTargetException(); if (t == null) ...{ t = e; } saveException(pageContext, t); throw new JspException("找不到对象"); } catch (NoSuchMethodException e) ...{ saveException(pageContext, e); throw new JspException("找不到此方法"); } } public static void saveException(PageContext pageContext, Throwable exception) ...{ pageContext.setAttribute("ceun.tag.error", exception, PageContext.REQUEST_SCOPE); }}
3.标签描述文件
<? xml version="1.0" encoding="ISO-8859-1" ?> <! DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" > < taglib > <!-- ========== Tag Library Description Elements ========================= --> < tlib-version > 1.0 </ tlib-version > < jsp-version > 1.2 </ jsp-version > < short-name > ceun </ short-name > < uri > http://www.java619.com/util </ uri > < description > 2006-12-22 </ description > < tag > < name > options </ name > < tag-class > com.ceun.tag.OptionsTag </ tag-class > < body-content > empty </ body-content > < description > iterate the collection ,and render some options for the select list </ description > < attribute > < name > collection </ name > < required > true </ required > < rtexprvalue > true </ rtexprvalue > < description > the name of the collection object </ description > </ attribute > < attribute > < name > label </ name > < required > true </ required > < rtexprvalue > true </ rtexprvalue > < description > set the name of label </ description > </ attribute > < attribute > < name > value </ name > < required > true </ required > < rtexprvalue > true </ rtexprvalue > < description > set the name of value </ description > </ attribute > < attribute > < name > scope </ name > < required > false </ required > < rtexprvalue > true </ rtexprvalue > < description > specify the lookup scope of the collection and the bean, it's value look like "request:page",use ":" to split the former specify the lookup scope of the collection, and the latter specify the lookup scope of the bean, available scopes : page,request,session,application </ description > </ attribute > < attribute > < name > select </ name > < required > false </ required > < rtexprvalue > true </ rtexprvalue > < description > set the selected option or the selected bean,must be the same property as the value property </ description > </ attribute > < attribute > < name > property </ name > < required > false </ required > < rtexprvalue > true </ rtexprvalue > < description > the property of select bean </ description > </ attribute > </ tag > </ taglib >4.使用方法
ceun标签库使用方法(2006-12-23) 1.复制ceun.tld到应用WEB-INF目录下 复制依赖包commons-beanutil.jar 到应用WEB-INF/lib目录下 2.在JSP页面顶部加上 <%@ taglib uri="http://www.java619.com/util" prefix="ceun" %> 3.在JSP页面使用 ceun:options标签使用方法(2006-12-23) 用途: 主要用于产生select列表的选项,并指定要选中的项 属性说明: collection: 包含目标元素的集合的名称(必须是实现Collection接口的集合类,如Vector) label: 将目标元素的label值指定的属性作为显示值 value: 将目标元素的value值指定的属性作为选项值 select: 要指定元素的value值或是包含要选中值的bean的名称 property: bean的属性(可选,当select指定的是一Bean时,此时要设置该值) scope (可选) 指定collection及bean的查找范围,其值是形如request:page, 以":"分隔前者指明collection范围,后者指明bean范围 可以范围有page,request,session,application 例: 若在某个范围(如request)内存在名为subjects集合对象以及一个名为stu且 具有id属性的JavaBean,我们假设用subjects集合中元素的name属性作为选项显示值 id属性作为option值(value),并设置列表选中id与stu.id相同的选项 <ceun:options collection="subjects" label="name" value="id" select="stu" property="id" /> 或 <ceun:options collection="terns" label="tern" value="tern" select="stu" property="tern" scope="request:page"/> 说明:scope="request:page"表明terns集合对象存在于request范围 而stu JavaBean存在于page范围