Call another form from the command button

2020-02-26 11:13发布

问题:

When I submit a form it should call another form so that I can render it. I use primefaces and I want it to work something like below. But it is throwing an error.

xhtml

<h:form id="from1">
<h:inputText=......
<h:outputText=....
<p:commandButton id="submit" actionlistener="#{bean.method}" value="submit" process="form2"/>

<h:form id="form2">
<h:outoutText value="it is working" render="#{bean.boolean}" />
</h:form>

error

[Faces Servlet]] (http--127.0.0.1-8080-2) Servlet.service() for servlet Faces Servlet threw exception: javax.faces.FacesException: Cannot find component with identifier "form2" referenced from "form1_submit".

Updated

<h:form id="from1">
    <h:inputText=......
    <h:outputText=....
    <p:commandButton id="submit" actionlistener="#{bean.method}" value="submit" update=":form1"/>
</h:form>

回答1:

There are two mistakes:

  • Relative component client IDs are releative to the closest parent UINamingContainer component (which is in your case thus the <h:form id="form1">). The client ID "form2" does not exist anywhere within the context of "form1". You need to use an absolute client ID instead, prefixed with the naming container separator character which defaults to :. This will search for components from the view root on.

    process=":form2"
    
  • You cannot process another from by a submit of one form. All of the fields which you need to be processed are not been submitted at all (note that this is not a JSF limitation, but a HTML limitation). Just enclose the fields which you need to process in the very same form as you need to submit.



回答2:

There is another solution with p:remoteCommand and JS with onclick described HERE.