2

In XPages you can access the value(s) of an input control (e.g. xp:inputText) on the server side in three (see answer of Sven Hasselbach) different ways:

  1. Use javax.faces.component.UIComponent to get the base object for a UI component in combination with an appropriate getter (e.g. getComponent("txtRootFolder") .getValueAsString())

enter image description here


  1. Use a Scope Variable (e.g. requestScope.rootfolder)

enter image description here


I know the usage/purpose of scope variables, that's not the questions here.

I want to know what are the main differences (advantages/ disadvantages, best practices, etc.) between this two possible solutions?

Community
  • 1
  • 1
Georg Kastenhofer
  • 1,387
  • 12
  • 32

2 Answers2

4

There is a third way which I prefer: Add a binding to your component. Then you can access the component and it's value directly.

Here is simple example:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">

    <xp:inputText id="inputText1" binding="#{myValue}" />
    <xp:label value="#{myValue.value}" id="label1" />

    <xp:button value="Label" id="button1">
        <xp:eventHandler event="onclick" submit="true" refreshMode="partial" />
    </xp:button>

</xp:view>
Sven Hasselbach
  • 10,455
  • 1
  • 18
  • 26
2

The general idea: your components represent a view on your data, while your variables represent the model. When you use value= you decouple the view from the model. Your variable doesn't need to know WHAT component did update it. So when you change the UI or decide that the variable will get updated from code only, nothing in your (controller) code needs to change. When you use binding (which has very valid use cases) you are dealing with a specific type of component, not only the value anymore. This is good when you need to manipulate the component (eg dynamic dropdown values), but overkill when you only need the value.

So IMHO use bound values any time unless you have a good justification to do otherwise.

Bonus tip: instead of scattering values around in scopes use a managed bean to keep them. This will make controller logic easier and consistent.

stwissel
  • 20,110
  • 6
  • 54
  • 101
  • Thanks for your detailed answer. In most cases, I already work with managed beans and value binding, but in some special cases (e.g. dialog boxes, etc.) I access the UI component values directly. – Georg Kastenhofer Jul 29 '15 at 07:36
  • @stwissel: can you give a clear example of this? (I mean managed bean) – Malin Apr 19 '17 at 10:57
  • Sure, here you go: https://wissel.net/blog/2011/01/binding-controls-to-managed-beans.html – stwissel Apr 19 '17 at 11:15