How to Update a Jsf Component from a Jsf Backing Bean Method

Can I update a JSF component from a JSF backing bean method?

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.

How to programmatically ajax-update specific component in backing bean

The RequestContext#execute() only executes arbitrary JavaScript code which is been passed-in as argument. It does not ajax-update the client representation of the components.

You need RequestContext#update() instead wherein you just pass the client ID of the to-be-updated component.

context.update("monitorVehicleForm");

This has exactly the same effect as <p:commandXxx ... update="monitorVehicleForm">. This works provided you've a

<h:form id="monitorVehicleForm">

without any NamingContainer parent and thus have a

<form id="monitorVehicleForm" name="monitorVehicleForm" ...> 

in the generated HTML.

See also:

  • How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar"

Update JSF components in sequence from bean

I need to perform an action in the database and depending on the
result I need to close the window and right after update the other two
panelGroups with the updated information

You could do this without calling actions from java code.

Without knowing your exact layout, the task you are asking for would usually look like this:

<p:commandLink 
process="@form"
actionListener="#{bean1.updateDatabase}"
update="j_idt3:panelGroupHolde;j_idt3:center_westBody"
oncomplete="if (args.updateSuccessfull) PF('dlgConfirmApplicationDelete').hide();">

and inside your bean1.updateDatabase method, you specify args.updateSuccessfull depending on the actual result of the query.

Boolean updateSuccessfull = false;
RequestContext.getCurrentInstance().addCallbackParam("updateSuccessfull", updateSuccessfull);

In the example you mentioned the order is not important, as each action will have it's predetermined outcome and is dedicated to a certain phase in the life-cycle (update always comes before oncomplete):

  • If the update-query fails, data does not change, so the update= has no impact and the dialog stays visible due to the CallBack-Param.
  • If the update-query works, data does change, both ui-elements are updated (where order must not matter) and the dialog is hidden due to the CallBack-Param.

If this is not the case, then you have code inside the RenderResponse-Phase, which should rather be executed in the InvokeApplication-Phase (or earlier).

A common misstake causing the later is to call "methods containing heavy code" from the xhtml (while other listings ("second update") depend on that values as well), like

<ui:repeat values="#{bean1.queryDatabaseForXY()}" ...>

Obviously now the SECOND update needs to come after the first update, cause it also requires some data from bean1.queryDatabaseForXY().

Rather than that you should perform data loading and/or calculations in the proper phase and simply refer to a getter returning an already known collection when generating the response:

<ui:repeat values="#{bean1.getUsers()}" ...>

or more precicesly

    <ui:repeat values="#{bean1.users}" ...>

(The actual loading would be either @PostConstruct, an <f:viewAction> or just the already existing list inside a view- or session-scoped bean)

Update PrimeFaces dataTable from backing bean

Assuming you'll be calling a Java method using Listener from Fingerprint device API, and from ManagedBean you can update any Primefaces Component using RequestContext.

RequestContext.getCurrentInstance().update("ID_OF_YOUR_DATATABLE")

how to update dynamic text in PrimeFaces

In the case you are using Primefaces 7.0+ swap the already mentioned

RequestContext.getCurrentInstance().update("userDialog");

for

PrimeFaces.current().ajax().update("userDialog");

If I can suggest you, set some id for <h:form> and use the full client ID after, something like

PrimeFaces.current().ajax().update("userDialog:yourFormId");

BalusC already answer a similar question in this thread Can I update a JSF component from a JSF backing bean method?

update a bean property from the same jsf

If you want to stay on the same page then you don't have to return String, just create void method. If you want to update fields after form submit you can do it in JSF component by ajax with render attribute and specify these fields with ID or by selectors like @form:

<h:commandButton action="#{login.validateUsernamePassword}" value="Login">
<f:ajax execute="formId" render="username otherComponentId"/>
</h:commandButton>

or update from backing bean:

public String validateUsernamePassword() {
...
this.user = "admin";
RequestContext.getCurrentInstance().update("formId:username"); //PrimeFaces solution since you have PrimeFaces tag on your question
...

}

Better option is to use render attribute and probably you would like to also update h:message for any occurred errors (give it ID and update it in render).

  • Difference between returning null and "" from a JSF action
  • Can I update a JSF component from a JSF backing bean method?
  • Render multiple components with f:ajax

How to update p:inputText from backing bean?

Try to remove immediate="true" and also remove <p:ajax>cause it has no effect because you already has update attribute in your <p:commandButton>

 <p:commandButton id="reloadSVGBtn"
styleClass="pbutton"
value="Hidden"
actionListener="#{myform.reloadSVG}"
update=":topologyViewForm:htmlDivs :topologyViewForm:paths">
</p:commandButton>

and for a further details read this.

Update Nested Panel from Backing Bean

Fixed it.

It was a combination of two issues.
First Is it possible to update non-JSF components (plain HTML) with JSF ajax?
I added the jsf:id to the Div

Second from How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar"

It says to look in the HTML output for actual client ID

Once I had the jsf:id and the actual client ID it all worked

JSF update backing bean property before action-method

Just send a request parameter with that value. It's not entirely clear how you're submitting the form, but you could use among others <h:inputHidden> for this.

<h:inputHidden id="hidden" value="#{bean.property}" />

In JS side, just change its value with the fetched value like follows:

document.getElementById("form:hidden").value = fetchedValue;


Related Topics



Leave a reply



Submit