/** *//** * 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 * */ publicclass RequestUtils ...{ privatestatic 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 */ publicstatic 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 */ publicstaticint getScope(String scopeName) throws JspException ...{ Integer scope = (Integer) scopes.get(scopeName.toLowerCase()); if (scope ==null) ...{ thrownew 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 */ publicstatic 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); thrownew JspException("不能访问指定对象的属性"); }catch (InvocationTargetException e) ...{ Throwable t = e.getTargetException(); if (t ==null) ...{ t = e; } saveException(pageContext, t); thrownew JspException("找不到对象"); }catch (NoSuchMethodException e) ...{ saveException(pageContext, e); thrownew JspException("找不到此方法"); } } publicstaticvoid 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>