Flexibility in applications is always a great thing, to adapt the application to the business needs without recoding things. And if adding this flexibility can be done without lots of code, you will probably use it.
This is one way of creating a keyword data bean for you XPages.
Start by creating a form named Keyword, in this form add the fields Name and Data, add the multivalue checkbox in the datafield
Save the form
Now you need to create the lookup view, create a view called byKeyword
Add 2 columns name and data. The name column should display the name of the Namefield and it must be sorted and the second view should show the data field, Save the new view.
Let move over to the interesting code, create a new java class named FormHelper and place it in a package, I call mine com.xpagedeveloper.
I create two functions, the first one will return return a vector with the data from the keyword.
And my Second function returns string from the array separated with a string of your choice
After I add the bean init setup in the faces-context.xml in the WebContent\WEB-INF folder from the package explorer.
Now it’s very simple to get keyword values to you fields and functions inside your XPages.
Create a Keyword using the Keyword form, in this case I’ve created a Keyword named Category and added some values to it.
‘
And in my XPage I have added an Combobox
And in the values tab click on the “Add Formula Item” button
And write the following code
If you want the full code for the bean, it can be found here as an xSnippet
Now your XPage application is more flexible without lots of code.
Fredrik,
Nicely done and thanks for sharing this! You now have me thinking that this could be used in a central database for keywords that are not application specific (and are used in many applications). For example: lists of states or countries. Any thoughts / recommendations?
Don
Absolutly, that’s a great way to do it. There is a followup article coming showing how to add caching to the bean.
And perhaps if the keyword starts with Global_ it is featched from the common database and in other cases locally 🙂
The possibilities are endless.
Hey Fredrik. This is very well explained and so easy. What I like is the practical aspect of what you showed. We all need a keywords database. I was getting tired of the @DBColumns and @DBlookups. I’m sure that this way is even quicker on the server. Thanks
Amazing way of referencing keywords, which we use in all applications. Very helpful. Thanks for sharing
Hi Fredrik,
I am just starting to learn Java in XPages, (no idea why, yet!) so some of the above Java is beyond me at this stage, but this is so professionally produced, – unlike many well intended articles, – you can follow every step of the process regardless of whether you are an expert or a novice like me.
Thank you.
Great that you like my article. Feel free to ask if something is hard to understand.
/Fredrik