0

I need to redirect page into external site with POST parameters, but I cannot use vanilla HTML <form action="url"> like it is explained here:

JSF commandButton - passing POST params to an external site

because then the form would be within a jsf form - and it doesn't work.

Is it possible to use:

FacesContext.getCurrentInstance().getExternalContext().redirect("http://example.com");

with POST parameters without additional vanilla form somehow? Or maybe there is other way to acheive this without form?

Community
  • 1
  • 1
robson
  • 1,623
  • 8
  • 28
  • 43
  • I understand that you have a FORM in your main HTML and inside you want to do the redirect. You can create another FORM outside and summon it with javascript. – maqjav Jun 21 '13 at 09:52
  • That's quite good idea, but the problem is that I need several buttons created dynamically for items in each row in a table. – robson Jun 21 '13 at 09:59
  • Each one is sending different parameters?, maybe you can implement your JS function to receive those parameters and modify dinamically the form, or if the difference is just the URL, then passing the URL. – maqjav Jun 21 '13 at 10:00
  • have you seen this http://stackoverflow.com/questions/11718542/jsf-managed-bean-redirect-with-parameters-without-viewing-url – Freak Jun 21 '13 at 10:15
  • @freak - I'd like to redirect to external site - so I cannot use neither cookies nor session. – robson Jun 21 '13 at 10:30

1 Answers1

1

Try something like this:

JAVASCRIPT:

function redirect() {
    document.getElementById("mySubmitButton").submit();
}

XHTML:

<h:form>
     <span onclick="javascript:redirect()" class="linkClass">REDIRECT</span>
</h:form>

<div style="display:none:"> <!-- If you want it hidden -->
    <form action="http://external/myplace.html" method="post"> 
        <input type="hidden" value="value1"></input>
        <input type="submit" id="mySubmitButton"</input>
    </form>
</div>

EDIT: Added another test.

PASSING DYNAMIC PARAMETER:

In this example we assume that we are always going to send a value.

JAVASCRIPT:

function redirect(dynamicValue) {
    document.getElementById("dynamicField").value = dynamicValue;
    document.getElementById("mySubmitButton").submit();
}

XHTML:

<h:form>
     <span onclick="javascript:redirect('myValue')" class="linkClass">REDIRECT</span>
</h:form>

<div style="display:none:"> <!-- If you want it hidden -->
    <form action="http://external/myplace.html" method="post"> 
        <input id="dynamicField" type="hidden" value=""></input>
        <input type="hidden" value="value1"></input>
        <input type="submit" id="mySubmitButton"</input>
    </form>
</div>
maqjav
  • 2,310
  • 3
  • 23
  • 35
  • I like this solution, but how pass different values depending on button I press? I need to pass one value from the table - #{v.value}. – robson Jun 21 '13 at 10:12
  • @robson I added another version passing a parameter, I hope this is what you are looking for :) – maqjav Jun 21 '13 at 10:43
  • @BalusC - do you have better one? – robson Jun 21 '13 at 11:15
  • @BalusC - I don't need JSF - you're right, but I need to get rid of form - because I'm already in one, so - I think that maqjav's solution is useful in my situation. And that's why I wanted to try with FacesContext redirect - to try redirect to another page within my first form, but what I need is to pass POST params. – robson Jun 21 '13 at 11:22