Georg Kastenhofer
Georg Kastenhofer

Reputation: 1417

Using Java Controller Classes and Custom Control Properties

In the future I want to use almost only JAVA instead of SSJS. Therefore I am using for each XPage a corresponding Java class as an "XPage Controller" similar to Jesse Gallagher Scaffolding project. Another example for such an approach would be ASP.NET they are using code behind files.

Now I have some architectural questions concerning custom control properties.

Take a look at the following code snippets:

Szenario 1: "Use custom control properties"

XPage:

<xp:view xmlns:xp="http://www.ibm.com/xsp/core">    
    ...
    <xc:actionButtons showSaveButton="true"></xc:actionButtons>
    ...
</xp:view>

Custom Control:

<xp:button id="btnSave" value="Save" themeId="Button.Command.FormButton" 
    rendered="#{compositeData.showSaveButton}" />

Szenario 2: "Don't use custom control properties use only the controller class"

This means the controller class (in view scope) could handle all properties

XPage:

<xp:view xmlns:xp="http://www.ibm.com/xsp/core">    
    ...
    <xc:actionButtons></xc:actionButtons>
    ...
</xp:view>

Custom Control:

<xp:button id="btnSave" value="Save" themeId="Button.Command.FormButton" 
    rendered="#{documentController.showSaveButton}" 
    actionListener="#{documentController.actionSavePerformed}"/>

Szenario 3: "Use the custom control properties and the controller class together"

XPage:

<xp:view xmlns:xp="http://www.ibm.com/xsp/core">    
    ...
    <xc:actionButtons showSaveButton="#{documentController.showSaveButton}"></xc:actionButtons>
    ...
</xp:view>

Custom Control:

<xp:button id="btnSave" value="Save" themeId="Button.Command.FormButton" 
    rendered="#{compositeData.showSaveButton}" 
    actionListener="#{documentController.actionSavePerformed}"/>

UPDATE

Above with the three scenarios, I have tried to describe my point of view in a general way, how you can work with custom controls in combination with controller classes.

Now my question is, how I should deal with custom control properties, when I try to work with controller classes:

In my opinion all relevant code (e.g. handling the visibility of buttons, etc.) should be placed in the controller classes. So, it is not necessary, to have a custom control property (e.g. Scenario 1: compositeData.showSaveButton vs Scenario 2: documentController.showSaveButton), but on the other hand using custom controls properties makes the control more independent. In Scenario 3 I have tried to give a short example, combining custom control properties with the advantages of controller classes (e.g. showSaveButton="#{documentController.showSaveButton}"), but this approach seems to be over-engineered.

I am not sure which is the best technical approach?

Ideas for improvements (pros and cons) or better architectural approaches are welcome!

Upvotes: 1

Views: 234

Answers (1)

David Leedy
David Leedy

Reputation: 3593

There's been many shows on NotesIn9 about using controller classes. There might be some examples there of interest. But to answer your question, it's really up to you. The custom control can reach into the controller easily enough but then it's not as "independent" as you say, however, since you're using controllers that's less of an issue I think because you can make the assumption that the controller will have the code needed to support the custom control. And depending on the custom control your controller could extend additional controllers that have the custom control support built in. Then it's a given that it'll work. But if you want to pass values in via the compositeData you can. Just create a custom property and set the type to "java.lang.Object". Then you can pass in anything you want really. Depending on the control I'd personally lean towards just having it depend on the controller.

Upvotes: 1

Related Questions