Activate a commandButton action using a JavaScript-passed variable in JSF

I want to perform an Ajax call from my .xhtml page to my ManagedBean function. Here is the code snippet of what I am trying to achieve:

.xhtml form code

            <h:form id = "jsfform">
                <h:commandButton id="button" action="#{cellBean.children()}">
                    <f:ajax render=":results" />
                </h:commandButton> </h:form>
            <h:panelGroup id="results">
                <th>Child cells</th>
                <td>
                <ui:repeat value="#{cellBean.childCells}" var="cell">
                        <tr>#{cell.cellName}</tr>
                </ui:repeat>
                </td>
            </h:panelGroup>

managed bean's function code

 public List<Cell> children(){
    this.childCells.add(new Cell("cell1"));
    return this.childCells;
}

The above implementation works well, where a new "Cell" object is added to the list and rendered asynchronously whenever the commandButton is triggered.

In my next step, I aim to modify the cellBean.children function to take parameters (for example, a list of Strings), pass it to the backing bean function, and execute operations. However, I cannot trigger the commandButton in the same way as before due to the inability to pass parameters like that.

For example:

        <h:form id = "jsfform">
            <h:commandButton id="button" action="#{cellBean.children(a_list)}">
                <f:ajax render=":results" />
            </h:commandButton> </h:form>
        <h:panelGroup id="results">
            <th>Child cells</th>
            <td>
            <ui:repeat value="#{cellBean.childCells}" var="cell">
                    <tr>#{cell.cellName}</tr>
            </ui:repeat>
            </td>
        </h:panelGroup>


public List<Cell> children(List<String> alist){
    this.childCells.add(new Cell("cell1"));
    return this.childCells;
}

Thank you for any insight or recommendations on how to accomplish this task effectively.

=============== UPDATE ==================

Based on this example, I have attempted the following approach:

.xhtml form

 <h:form id = "jsfform">
                <h:inputHidden id="childCells" value="#{cellBean.childCellsStr}" />
                <h:inputHidden id="parentCells" value="#{cellBean.parentCellsStr}" />
                <h:commandButton id="button"  style="display: none" action="#{cellBean.children()}">
                    <f:actionListener binding="#{cellBean.checkBtnDisable()}" /> 
                    <f:ajax render=":ajaxform"/>
                </h:commandButton>

Javascript code

  // json1 && json2 are two json strings
document.getElementById('jsfform:childCells').value = json1;
document.getElementById('jsfform:parentCells').value = json2;
//trigger the button of the form with javascript
document.getElementById('jsfform:button').onclick();

Managed Bean

private String childCellsStr;
private String parentCellsStr;
//getters && setters

Despite setting values for the inputHidden fields, unfortunately, the backing bean setters do not get triggered, resulting in null values.

Answer №1

After following this tutorial, I simply added the execute parameter in the tag, and now my setters are being called properly with the values I desire.

.xhtml code

<h:form id = "jsfform">
                <h:inputHidden id="childCells" value="#{cellBean.childCellsStr}" />
                <h:inputHidden id="parentCells" value="#{cellBean.parentCellsStr}" />
                <h:commandButton id="button"  style="display: none" action="#{cellBean.children()}">
                    <f:actionListener binding="#{cellBean.checkBtnDisable()}" /> <!--first call the children function, then call the checkBtnDisable -->
                    <f:ajax execute="childCells parentCells" render=":ajaxform"/>
                </h:commandButton>
            </h:form>

Javascript code

    document.getElementById('jsfform:childCells').value = json1;
document.getElementById('jsfform:parentCells').value = json2;
//trigger the button of the form with javascript
document.getElementById('jsfform:button').onclick();

Java Bean

private String childCellsStr;
private String parentCellsStr;
//getters && setters

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps can be taken to resolve the "npm ERR! code ELIFECYCLE" error in a React application?

After attempting to start my React app with npm start, an error occurred : $ npm start > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f3b3d2a212b3c0f7f617e617f">[email protected]</a> start C:\Users&bso ...

What is the best way to programmatically close a bootstrap modal?

Currently, I am performing update operations through a modal. When the modal pops up, it loads specific row data that I intend to update. Here is the code for my modal: <form id="form1" runat="server"> <asp:ScriptManager ID="sm1" runat="serve ...

Using Java in C code

I am currently exploring JNI and Android NDK, and I am trying to invoke a Java API from one of my C language threads. However, I keep encountering an error during the Java method call. Can anyone provide guidance on the best approach to invoke a Java meth ...

Guide on incorporating jQuery library files into existing application code with the npm command

Recently, I used a node JS yo ko command to create a single-page application using Knockout-JS. Following that, I proceeded to install jquery packages using the command npm install jquery The installation was successful. However, my current goal is to in ...

Utilizing Conditional Styling for an Array of Objects within a Textarea in Angular 6

My array contains objects structured as follows: list =[ { name:"name1", value:true } { name:"name2", value:false } { name:"name3", value:true } { name:"name4", value:false ...

Tips for managing a basic click event within the Backbone.js framework

I am facing a challenge with a basic functionality in Backbone. I am trying to set up the <h1> element on my page so that when a user clicks on it, it smoothly navigates back to the homepage without a page reload. Here is the HTML snippet: <h1& ...

Learn how to utilize JavaScript in VB.NET ASP.NET to successfully close a web form

In the scenario, when the session expires in our system or application, it automatically redirects to the log-in page. If I have the application open in two different browsers and the session times out in one browser window, ideally it should also close t ...

Error encountered while executing node server.js for Azure IoT Hub due to incorrect flags provided in the RegExp constructor

Currently, I am attempting to execute 'node server.js' in order to establish a connection between my Raspberry Pi device and Azure through the Azure IoT Hub. However, upon running the command 'node server.js', an error message is displa ...

When a Typescript object contains a key without a corresponding value, what is its significance?

In my experience, I've come across typescript objects that are defined in the following way: const MyObj = { myVar1, name1: myVar2 } Can someone explain the purpose of this object? I know that there is a key-value ...

The error message "ECONNRESET" occurred while attempting to send a post request using Axios to

Attempting to send a post request to my webserver using axios, I have a client that collects user input to populate an array of strings. This data is then sent via a post request using axios for processing by the server: if (parsedInput > 0 &&am ...

Deletion of component with setTimeout in React Class Component

I have a notification feature that disappears after a certain delay when rendered. The issue arises when attempting to cancel this automatic removal using clearTimeout, as it doesn't seem to work. See below class Notify extends React.Component { ...

The Boolean method in Selenium using Java is consistently yielding false results

public boolean verifyRemoveButtonPresence() { List<WebElement> buttonsOnPage = findAll(BUTTONS); for (WebElement button : buttonsOnPage) { String buttonText = button.getText(); if (buttonText.equals("Remove")) { re ...

Steps to include a jQuery reference in a JavaScript file

I'm looking to create an external JavaScript file with validation functions and incorporate jQuery into it. Can anyone provide guidance on how to accomplish this? I attempted the code below, but unfortunately, it doesn't seem to be functioning c ...

Use xmlhttprequest function during text input instead of traditional form submission

Is it possible to implement a search form xmlhttprequest that dynamically updates results as the user types, rather than requiring them to submit a form? function customersController($scope, $http) { $scope.search = function() { $scope.url = &apos ...

Creating HTML for an email by iterating through a JavaScript object

I need assistance with iterating through a JavaScript object where each key:value pair is displayed on separate lines in the email body using Appscript. I believe it might need to be converted to an HTML string but I'm not entirely sure. The code snip ...

Deliver a universal Angular application using NodeJS and ExpressJS for the server-side functionality

I have set up a Node/Express JS back-end on an AWS instance using a Linux image, along with NGINX as the web server. Currently, my MEAN application is running smoothly as I work on building the Angular application. I copy the dist folder to the Node/Expres ...

What is the best way to retrieve the values from the labels for two separate buttons?

I designed a pair of buttons with a single label below. Both buttons acted as standalone entities. <label for="buttons" class ="val">0</label> <input class="btn btn-primary button1" type="button" ...

Reactjs Promise left hanging in limbo

How can I resolve the pending status of my promise? I have a modal with a form submit in it, where I am trying to retrieve the base64 string of a CSV file. While my code seems to be returning the desired result, it remains stuck in a pending state. c ...

Exploring the process of retrieving JSON from an API using plain JavaScript within the Protractor framework

I'm attempting to retrieve the distance between two locations using the TomTom API. Unfortunately, I am facing an issue with Protractor: When trying to use the *fetch function - I receive an error stating "fetch is not defined" and it advises me to ...

Is there a way to resolve the scrolling problem on Google Maps?

When using the Google Maps V3 API, an issue arises where zooming out on the map using the scrollbar also causes the page to scroll along with it. This can be quite frustrating and so far I have not been able to find a way to disable this scrolling behavi ...