Accessing attributes passed to extended PrimeFaces component

筅森魡賤 提交于 2019-12-05 22:00:04
String fieldClass = attrs.containsKey("fieldClass") ? (String) attrs.get("fieldClass").toString() : "NONE";

Your mistake is that you're using containsKey() to check if the property has been specified.

Here's an extract from UIComponent#getAttributes() javadoc:

getAttributes

public abstract java.util.Map<java.lang.String,java.lang.Object> getAttributes()

Return a mutable Map representing the attributes (and properties, see below) associated wth this UIComponent, keyed by attribute name (which must be a String). The returned implementation must support all of the standard and optional Map methods, plus support the following additional requirements:

  • The Map implementation must implement the java.io.Serializable interface.
  • Any attempt to add a null key or value must throw a NullPointerException.
  • Any attempt to add a key that is not a String must throw a ClassCastException.
  • If the attribute name specified as a key matches a property of this UIComponent's implementation class, the following methods will have special behavior:
    • containsKey() - Return false.
    • get() - If the property is readable, call the getter method and return the returned value (wrapping primitive values in their corresponding wrapper classes); otherwise throw IllegalArgumentException.
    • put() - If the property is writeable, call the setter method to set the corresponding value (unwrapping primitive values in their corresponding wrapper classes). If the property is not writeable, or an attempt is made to set a property of primitive type to null, throw IllegalArgumentException.
    • remove() - Throw IllegalArgumentException.

Note that it thus always returns false for containsKey for component's properties. That's because dynamic properties are not stored in the attribute map, but instead in the component instance itself. They're only resolved when calling get().

You need to change the wrong line as follows:

String fieldClass = (String) attrs.get("fieldClass");
if (fieldClass == null) fieldClass = "NONE";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!