Search code examples
javajspresolver

How to resolve the name properly by BeanELResolver


we have to work with a very specific class in Java that has the following property:

protected SPSExchangedDocumentType spsExchangedDocument;

public SPSExchangedDocumentType getSPSExchangedDocument() {
  return spsExchangedDocument;
}

I use the instance of that class inside a jsp page:

<c:out value = "${certificate.spsExchangedDocument.id.value}"/>

It throws me the following exception:

javax.el.PropertyNotFoundException: The class 'un.unece.uncefact.data.standard.spscertificate._5.SPSCertificateType' does not have the property 'spsExchangedDocument'. at javax.el.BeanELResolver.getBeanProperty(BeanELResolver.java:579) at javax.el.BeanELResolver.getValue(BeanELResolver.java:281) at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175) at com.sun.el.parser.AstValue.getValue(AstValue.java:138) at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:206) at org.apache.jasper.runtime.PageContextImpl.evaluateExpression(PageContextImpl.java:1001)

I think the problem is directly with the fact, that the property is called spsExchangedDocument, but the method is called getSPSExchangedDocument. I cannot change the code of the class, because it's generated automatically from XSD, that we cannot alter, so I should change the page. But what name should I use in order for EL to resolve it properly:

  • spsExchangedDocument.
  • SPSExchangedDocument.
  • sPSExchangedDocument.

What is the rule for such naming conventions in java and EL anyway?


Solution

  • Assuming the BeanELResolver implementation conforms to the JavaBeans spec:

    8.8 Capitalization of inferred names.

    Thus when we extract a property or event name from the middle of an existing Java name, we normally convert the first character to lower case. However to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone. So for example,

    • “FooBah” becomes “fooBah”
    • “Z” becomes “z”
    • “URL” becomes “URL”

    The java.beans API resolves the property name as SPSExchangedDocument:

    import java.beans.*;
    
    public class Bean {
      public Object getSPSExchangedDocument() {
        return null;
      }
    
      public static void main(String[] args) throws IntrospectionException {
        BeanInfo info = Introspector.getBeanInfo(Bean.class);
        for (PropertyDescriptor prop : info.getPropertyDescriptors()) {
          System.out.println(prop.getName());
        }
      }
    }
    

    That said, there have been bugs in this area before.