By BestPractices


2012-07-06 15:22:12 8 Comments

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 comments

@panayot_kulchev_bg 2019-11-19 15:00:26

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 :)

@Kukeltje 2019-11-19 15:44:26

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 minimal reproducible example flavour) it is hard to tell.

@BalusC 2012-07-06 17:51:42

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

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

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

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

From PrimeFaces 6.2 RequestContext#update() has been deprecated, use PrimeFaces::Ajax#update instead.

PrimeFaces.current().ajax().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.

@BestPractices 2012-07-06 18:28:19

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?

@BalusC 2012-07-06 18:30:44

No, it's too late. Move the job to invoke action phase or before render response phase. Invoke the method by <f:ajax listener> or <f:event type="preRenderView" listener>

@RinaldoPJr 2012-12-06 20:16:27

Any idea on how to do that with JSF 1.2? I can ask a new question if the answer is too long/complex.

@BalusC 2012-12-07 00:01:48

@RinaldoPJr: JSF 1.2 has no concept of <f:ajax>. This is introduced since JSF 2.0. You'd need to resort to JSF 1.2 component library specific facilities.

@BalusC 2015-06-17 10:08:08

@DanielK: No idea. I don't do Trinidad. Just use standard JSF API approach if you still can't figure out the Trinidad way.

@amphibient 2015-07-10 23:00:43

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: stackoverflow.com/questions/31350850/…

@BalusC 2015-07-11 00:38:33

@amphibient: update the dialog's form instead of the dialog.

@amphibient 2015-07-13 17:22:02

how do i do just the form ?

@ℛɑƒæĿ 2018-06-07 19:28:36

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:

@Frithjof Schaefer 2015-07-02 10:48:39

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!!

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] Update PrimeFaces dataTable from backing bean

  • 2013-10-03 12:45:25
  • Guwanch
  • 35778 View
  • 6 Score
  • 3 Answer
  • Tags:   jsf primefaces

4 Answered Questions

[SOLVED] How to provide a file download from a JSF backing bean?

2 Answered Questions

[SOLVED] Primefaces right click - backing bean method call

3 Answered Questions

[SOLVED] Calling bean methods with arguments from JSF pages

  • 2011-12-25 19:03:29
  • Rajat Gupta
  • 62555 View
  • 13 Score
  • 3 Answer
  • Tags:   jsf primefaces

2 Answered Questions

[SOLVED] Calling two Bean methods in JSF

  • 2013-02-14 12:42:09
  • sciFi
  • 545 View
  • 0 Score
  • 2 Answer
  • Tags:   jsf-2 primefaces

Sponsored Content