112

Is there a way to have a JSF Backing bean cause an update of a component on the page? I am not looking to use an ajax component with update attribute to update a component on the page. I need to trigger an update from within a JSF backing bean method. Note the update on the page can happen after this method completes or prior to its completion. I am using PrimeFaces, if there is a solution that can be had from using PrimeFaces.

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
BestPractices
  • 12,738
  • 29
  • 96
  • 140

5 Answers5

211

Using standard JSF API, add the client ID to PartialViewContext#getRenderIds().

FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add("foo:bar");

Using PrimeFaces specific API, use PrimeFaces.Ajax#update().

PrimeFaces.current().ajax().update("foo:bar");

Or if you're not on PrimeFaces 6.2+ yet, use RequestContext#update().

RequestContext.getCurrentInstance().update("foo:bar");

If you happen to use JSF utility library OmniFaces, use Ajax#update().

Ajax.update("foo:bar");

Regardless of the way, note that those client IDs should represent absolute client IDs which are not prefixed with the NamingContainer separator character like as you would do from the view side on.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 6
    It appears as though I cannot get this to work during the Render Response phase (which, unfortunately is where I am forced to place this code). If I place in Invoke Application or earlier phases, things work fine. Any workarounds for being in the Render Response phase? – BestPractices Jul 06 '12 at 18:28
  • 8
    No, it's too late. Move the job to invoke action phase or before render response phase. Invoke the method by `` or `` – BalusC Jul 06 '12 at 18:30
  • Any idea on how to do that with JSF 1.2? I can ask a new question if the answer is too long/complex. – RinaldoDev Dec 06 '12 at 20:16
  • @RinaldoPJr: JSF 1.2 has no concept of ``. This is introduced since JSF 2.0. You'd need to resort to JSF 1.2 component library specific facilities. – BalusC Dec 07 '12 at 00:01
  • @DanielK: No idea. I don't do Trinidad. Just use standard JSF API approach if you still can't figure out the Trinidad way. – BalusC Jun 17 '15 at 10:08
  • when i did this, i lost the form submit action invocation in the dialog, which works if i don't do context update. please see here: http://stackoverflow.com/questions/31350850/pdialog-form-submission-loses-action-method-after-requestcontext-update – amphibient Jul 10 '15 at 23:00
  • @amphibient: update the dialog's form instead of the dialog. – BalusC Jul 11 '15 at 00:38
  • how do i do just the form ? – amphibient Jul 13 '15 at 17:22
10

I also tried to update a component from a jsf backing bean/class

You need to do the following after manipulating the UI component:

FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add(componentToBeRerendered.getClientId())

It is important to use the clientId instead of the (server-side) componentId!!

Frithjof Schaefer
  • 1,135
  • 11
  • 22
8

The RequestContext is deprecated from Primefaces 6.2. From this version use the following:

if (componentID != null && PrimeFaces.current().isAjaxRequest()) {
    PrimeFaces.current().ajax().update(componentID);
}

And to execute javascript from the backbean use this way:

PrimeFaces.current().executeScript(jsCommand);

Reference:

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
2

Everything is possible only if there is enough time to research :)

What I got to do is like having people that I iterate into a ui:repeat and display names and other fields in inputs. But one of fields was singleSelect - A and depending on it value update another input - B. even ui:repeat do not have id I put and it appeared in the DOM tree

<ui:repeat id="peopleRepeat"
value="#{myBean.people}"
var="person" varStatus="status">

Than the ids in the html were something like:

myForm:peopleRepeat:0:personType
myForm:peopleRepeat:1:personType

Than in the view I got one method like:

<p:ajax event="change"
listener="#{myBean.onPersonTypeChange(person, status.index)}"/>

And its implementation was in the bean like:

String componentId = "myForm:peopleRepeat" + idx + "personType";
PrimeFaces.current().ajax().update(componentId);

So this way I updated the element from the bean with no issues. PF version 6.2

Good luck and happy coding :)

  • If you have the ajax change event on the `personType`, then passing both the person and index on to the ajax listener is superfluous. You can get both the id and the value from the event. But your solution might be a needed one for your specific usecase, but since there is not full example (in [mcve] flavour) it is hard to tell. – Kukeltje Nov 19 '19 at 15:44
-2

In order to updte the component from backing bean, we can achieve as below RequestContext.getCurrentInstance().update('updatePanelGroup');

<h:panelGroup id="updatePanelGroup">
    .....
    ....
</h:panelGroup>

Updating the component differs with respect to prima face version.

Betlista
  • 10,327
  • 13
  • 69
  • 110
Arun
  • 11
  • 2