简体   繁体   中英

<f:ajax> executing different forms

So I have 2 forms and a command button with f:ajax attached on it. I want to execute the second form on click on button but it seems that it ignores when I'm passing the form's id on execute attribute. But when I replace it with 'execute=":form1"' it runs correctly(the information from the form is sent to server). Can someone tell me why won't work with the id of second form, or how can I achieve this: with one button to execute any form i want from the page.( as it is now no information is sent to server, only the listener is called).

Code bellow:

<h:form id="form1">
    <h:panelGrid id="inputSection" >
        <h:inputText id="input1" value="#{counterMB.number1}" />
    <h:inputText id="input2" value="#{counterMB.number2}" />
    </h:panelGrid>
    <h:outputLabel id="sumResponse2" value="#{counterMB.sum}" />
</h:form>

<h:form id="form2">
    <h:panelGrid id="inputSection" >
        <h:inputText id="input1" value="#{counterMB.number1}" />
    <h:inputText id="input2" value="#{counterMB.number2}" />
    </h:panelGrid>
    <h:outputLabel id="sumResponse2" value="#{counterMB.sum}" />
</h:form>

<h:commandButton value="Sum numbers">
    <f:ajax event="click" execute=":form2" listener="#{counterMB.sum}" render=":form2 :form1"/>
</h:commandButton>

Update: so to be more explicit: from what I have read I can use ajax to update/refresh etc some parts of a page instead of refreshing the whole page. What i have tried to do is group some components in different forms and execute a form at a time to see how it behaves. It works perfectly if I use the id of first group(form) but doesn't work at all if I use the id of the second form(it calls the action but doesn't submit any data from any form). I don't understand why. (PS for those who claim i lack some knowledge : this is the reason of asking questions isn't it? I'm trying to learn some new stuff)

It seems that you are lacking basic knowledge of how jsf works. A command button must be inside a form. And it's not possible to submit 2 forms with one single button. The attribute execute of f:ajax tells which components to process, for example if you have 2 input texts, you can process only one and ignore the other using this attribute. but it's not possible to do what you are trying to do.

It doesn't really make sense to submit 2 forms and execute a single action method once.. there's no point in having 2 forms. why don't you put everything inside a single form?

In your current solution, you use a h:commandButton outside of any h:form - this is a little bit against HTML and JSF, so don't count on it. What I would suggest is

  • if you use Richfaces , put a a4f:jsFunction in every form and trigger the resulting Javascript from anywhere
  • if you use Primefaces , put a p:remoteCommand in every form and trigger the resulting javascript from anywhere
  • if you neither use any of them, put a <h:commandButton /> in every form, set the style hidden and use javascript to submit the form.

Hope it helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM