Checking attribute exists in JSP

后端 未结 5 1399
别那么骄傲
别那么骄傲 2021-02-01 13:15

I have some classes which extends a superclass, and in the JSP I want to show some attributes of these classes. I only want to make one JSP, but I don\'t know in advance if the

5条回答
  •  粉色の甜心
    2021-02-01 13:39

    You can readily create a custom function to check for the property, as per vivin's blog post.

    In short, if you already have your own taglib its just a matter of creating a static 'hasProperty' method...

    import java.beans.PropertyDescriptor;
    import org.apache.commons.beanutils.PropertyUtils;
    
    ...
    
    public static boolean hasProperty(Object o, String propertyName) {
        if (o == null || propertyName == null) {
            return false;
        }
        try
        {
          return PropertyUtils.getPropertyDescriptor(o, propertyName) != null;
        }
        catch (Exception e)
        {
          return false;
        }
    }
    

    ...and adding five lines to your TLD...

    
        hasProperty
        my.package.MyUtilClass
        boolean hasProperty(java.lang.Object,
            java.lang.String)
        
    
    

    ... and calling it in your JSP

    
      
    
    

提交回复
热议问题