I have a portlet view with some GUI controls. The view bean has a public method with an int parameter: doSomething(int param).
I’d like to trigger this method when certain GUI controls are clicked. I.e. when control1 is clicked, I’d like to have doSomething(1) executed, when control2 is clicked, I’d like to have doSomething(2) executed etc.
If the parameter values are supplied by you and not the end user, then the simplest way is to utilize the "Parameterized Method Calls’ support that is built into the standard expression language.
thank you for the quick response! The obvious thing I didn’t see
Is this also possible with controls of type “Option”? I.e. if I have a ToggleTab with three Options, I’d want that on selecting a tab, a server side method is called. The parameter is fixed and would be provided by me.
If you have the ‘Toggle Tabs’ control value bound to some field in your page bean, then your action expression can use the same expression to get the value that was submitted.
For example,
Create a read/write ‘toggleItem’ String field in your page bean.
Bind the ‘Toggle Tabs’ value to the expression that resolves to the field from #1. For example, something like: #{TestDefaultviewView.toggleItem}
Bind the ‘Action’ property of the command control to use the same value expression as a parameter to the action. For example, something like this: #{TestDefaultviewView.doSomething(TestDefaultviewView.toggleItem)}
Since the UpdateModelValues phase of the lifecycle happens before the InvokeApplication phase, you can be assured that the “Toggle Tabs” value has been updated before it does the action that uses the same.
Actually, if the setter for “toggleItem” will be called when a tab is selected and the parameter value makes it possible to find out which tab has just been selected, then I’ll need no other method (like “doSomething”) since I can call it from within the toggleItem-setter.
I do realize that using a command control has the advantage of decoupling of tab selection and calling of doSomething. But OTOH it is a bit more complicated and also requires a command control which is not needed in my scenario. The user interaction is done by selecting the appropriate tabs.
It sounds like the “Custom Value Change Listener” property of the Toggle Tabs control may be a good choice for reacting to tab selection changes.
For example, set the “Custom Value Change Listener” property to something like this: #{TestDefaultviewView.toggleTabs_processValueChange}
And add the listener method to the page bean:
/**
* Value Change Listener for the control with id='toggleTabs'
*/
public void toggleTabs_processValueChange(javax.faces.event.ValueChangeEvent event) {
// TODO: Replace with your code
}
I’ve tried the approach with the listener. It works. Almost. The event is sent not immediately when the user selects other tab but only when the form is refreshed.
How is it possible to fire the event immediately? I’ll try to attach some script to the toggle element…