Dynamically bind an Edit Box in a custom control to a managed bean

这一生的挚爱 提交于 2019-12-03 17:00:15

The trick is to hand over a String as parameter that would be the EL you want to use. Let's say you have a parameter bindTo as String with the value myBean.Color

  <xp:this.value><![CDATA[${javascript:"#{"+compositeData.BindTo+"}"}]]></xp:this.value>

The $ will evaluate first and replace the compositeData with the actual value. The beauty of the approach: works for any EL. Document Bean etc., so your custom control doesn't need to make any assumptions on its container.

So you could call your component with all sorts of bindings:

 <xc:myComponent BindTo="document1.subject"></xc:myComponent>
 <xc:myComponent BindTo="viewScope.someVariable"></xc:myComponent>
 <xc:myComponent BindTo="myBean.Color"></xc:myComponent>

Let us know how that works for you!

Make sure that one of the following is true about the object that you're passing to the Custom Control:

  1. It really is a bean (specifically, follows bean conventions). For example, if you'll be binding to the database property, the object should have (at a minimum) a getDatabase method that returns the current value of the database property; if the property is not read-only, the class should also have a setDatabase method that accepts the new value.
  2. It's a DataObject: your object implements the com.ibm.xsp.model.DataObject interface. The generic getValue and setValue methods this interface requires you to implement will be used to, respectively, retrieve and update values for any property, including database.
  3. It's a Map. Any property name you bind to will be treated as a key for the map.
  4. It's an instance of com.ibm.jscript.types.FBSObject. This is the underlying Java class that all SSJS object literals ({ }) become when the SSJS string is parsed at runtime. Be wary of using this type in managed beans, because these objects are not serializable.

As long as the object you're passing in is one of these four, the EL syntax listed in your question will be valid. Set the property type to be either the actual class name for the object you're passing or a base class it extends (or interface it implements). Or, to ensure it will accept anything, just set the property type to object.

But remember, declaring a class as a managed bean in the faces-config.xml only "manages" a variable name and scope... this declaration doesn't actually make your Java class a bean. Unless it conforms to bean conventions, it's not a bean. If it does conform to bean conventions, it's a bean, whether or not you declare it as a managed bean. This distinction is a source of much confusion in the XPages community, so I just wanted to belabor that again in this context.

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