4

I have a CommandButton (PrimeFaces 3.0.1), that is not in a Form-Tag. Is there a way to reference a form via it's Id or something, so that the Button can post a Form even when it's not a child-element of that Form? I did'nt find any apropriate in the specs...

The Button currently looks like this:

<p:commandButton value="#{messages['000.label.ok']}"
         oncomplete="w00001.hide()" type="submit" 
                 action="cancel"
         ajax="false" immediate="true" />

After pressing the Button JSF forwards to another page, that is mapped by the key "cancel".

Thanks!!!

treeno
  • 2,510
  • 1
  • 20
  • 36
  • Possible duplicate of [Call another form from the command button](http://stackoverflow.com/questions/11766255/call-another-form-from-the-command-button) – Kukeltje Oct 08 '15 at 12:24

2 Answers2

4

edit1 So what you want to do is submit a form, then redirect. As said before, commandButton has to be inside a form if you want to use it for submit, but it doesn't has to be the same form. You can wrap a second form around commandButton, and still use it to submit the values of your first form.

        <h:form id="form_test">
            <!-- values of this form will be submitted -->
        </h:form>
        <h:form>
            <p:commandButton value="cancel" action="#{yourBean.cancelFlow}"
                process="form_test @this" ajax="false" />
        </h:form>

the Method in YourBean.java could look like this (assuming your cancelpage.xhtml is in the package your.package)

public String cancelFlow() {
    // do something with form data

    return "/your/package/cancelpage?faces-redirect=true";
}

You could also redirect programmatically, using FacesContext.getCurrentInstance().getExternalContext().redirect

Mario B
  • 2,102
  • 2
  • 29
  • 41
  • Unfortunatly, I think, this is not why I need... I need a submit – treeno Jan 23 '12 at 14:59
  • This indeed submits the first form (input type of commandButton defaults to "submit"). Maybe i misunderstood what you want, can you post your source-code? – Mario B Jan 23 '12 at 15:21
  • I've edited the problem-description. I need a way to send this "cancel"-token to the Server so that JSF can redirect me to another page – treeno Jan 23 '12 at 15:46
  • Thank you for your help! In the end I was able to set the button in the form, so my problem is solved now. – treeno Jan 24 '12 at 10:44
0

According to https://stackoverflow.com/a/11766570/3568831 You cannot process another from by a submit of one form you should use <p:remoteCommand /> as explained here and also with this link to see an example

Community
  • 1
  • 1
Nwawel A Iroume
  • 1,249
  • 3
  • 21
  • 42