How to submit JSF ajax with inputs from multiple forms?

36 Views Asked by At

Due to UI layout issue, I have to separate inputs into multiple form. How to submit JSF ajax with inputs from multiple forms? Based on sample code below, how to get form1 input1 value when click on the Ajax Call button in form2?

Weblogic 12.2 Java 8 Servlet 3.1 JSF 2.2 CDI 1.1 EJB 3.2

JSF Page

<!DOCTYPE HTML>
<html
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:jsf="http://xmlns.jcp.org/jsf"
    xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
    xmlns:fn="http://xmlns.jcp.org/jsp/jstl/functions">
<h:head>
    <meta charset="utf-8" />
    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0" />
    <title>Ajax Test</title>
</h:head>
<h:body>
    <ui:debug hotkey="x" />
    <h:messages id="msg" />
    <fieldset>
        <legend>Form 1</legend>
        <h:form
            id="form1"
            method="get">
            <h:inputText
                id="input1"
                value="#{ajaxTestBean.input1}" />
            <h:commandButton
                value="Standard call"
                action="#{ajaxTestBean.call}" />
        </h:form>
    </fieldset>
    <fieldset>
        <legend>Form 2</legend>
        <h:form
            id="form2"
            method="get">
            <h:inputText
                id="input2"
                value="#{ajaxTestBean.input2}" />
            <h:commandButton value="Ajax Call">
                <f:ajax
                    execute="form1:input1 form2:input2"
                    render="msg form1:input1 form2:input2"
                    listener="#{ajaxTestBean.callAjax}" />
            </h:commandButton>
        </h:form>
    </fieldset>
</h:body>
</html>

Java Class

package dev;

import javax.faces.event.AjaxBehaviorEvent;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@javax.inject.Named("ajaxTestBean")
@javax.faces.view.ViewScoped
public class AjaxTestBean implements java.io.Serializable {
    static final long serialVersionUID = 1L;
    static final Logger logger = LogManager.getLogger();

    String input1;
    String input2;

    public AjaxTestBean() {
    }

    public void call() {
        logger.traceEntry();
        logger.debug("input1: {}", this.input1);
        logger.debug("input2: {}", this.input2);

        logger.traceExit();
    }

    public void callAjax(AjaxBehaviorEvent event) {
        logger.traceEntry();

        logger.debug("input1: {}", this.input1);
        logger.debug("input2: {}", this.input2);

        logger.traceExit();
    }

    public String getInput1() {
        return input1;
    }

    public void setInput1(String input1) {
        this.input1 = input1;
    }

    public String getInput2() {
        return input2;
    }

    public void setInput2(String input2) {
        this.input2 = input2;
    }
}
0

There are 0 best solutions below