How to invoke a sever side method with parameter from GUI

Hello,

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.

How can I do that?

Thank you for any hints!

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.

  1. [url]- The Java EE 6 Tutorial
  2. [url]https://javaee.github.io/uel-ri/[/url]

For example, you could set the “Action” property of the command control to something like this:

#{TestDefaultviewView.doSomething(1)}

Hello Eric,

thank you for the quick response! The obvious thing I didn’t see :slight_smile:

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.

Thanks!

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,

  1. Create a read/write ‘toggleItem’ String field in your page bean.
  2. Bind the ‘Toggle Tabs’ value to the expression that resolves to the field from #1. For example, something like: #{TestDefaultviewView.toggleItem}
  3. 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.

1 Like

Cool, thanks! I’ll try this out.

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.

Thank you again!

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
	}

Eric, hello again,

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…

I solved this by attaching a “Raise On Change” script to the toggleTab and firing a refresh of the area there.

Yes, that would be a server-side listener so it would only get triggered when there is a round-trip to the server that updates the server-side model.

The “Raise On Change” control is indeed a good way to trigger a partial refresh.

Thank you for the confirmation that I did it an appropriate way!