Reputation: 12631
I have a JSF-page in which the user can submit some data to an external link. However, before doing so I need to call a local action from a backing bean. In what way can I achieve this?
This is an example of my form (using vanilla HTML due to the external link action):
<form method="POST" action="https://EXTERNAL-LINK.com/">
<input type="hidden" ... />
<button>Submit</button>
</form>
So in short this is my case:
Any ideas on how to achieve this?
Upvotes: 1
Views: 5012
Reputation: 11742
I would submit a JSF form via AJAX behind the scenes first and when the response arrives I would submit your plain HTML form to an external URL.
Example:
<script type="text/javascript">
function jsf(data) {
if(data.status == 'success') {
document.getElementById('plain-submit').click();
}
}
function normal() {
document.getElementById('jsf-submit').click();
}
</script>
...
<h:form prependId="false">
...
<h:commandButton id="jsf-submit" action="#{bean.action} value="Submit" style="display:none">
<f:ajax execute="@form" ... onevent="jsf"/>
</h:commandButton>
</h:form>
<form method="POST" action="https://EXTERNAL-LINK.com/">
...
<input id="button-submit" type="button" onclick="normal()" ...>Submit</input>
<input id="plain-submit" type="submit" style="display:none">Submit</input>
</form>
Or it would be even easier to reverse the situation by making plain HTML form invisible, thus decreasing the number of JavaScript operations: you submit the (visible) JSF form via AJAX and then submit (invisible) plain HTML form when response successfully arrives back.
Upvotes: 2
Reputation: 85779
Change the <form>
to <h:form>
and other HTML components to JSF components, bind their values with your managed bean and let the user submit the data. Then, in your action
method you evaluate the data and then use a library like Apache HttpClient to send a POST request to the URL you want/need.
This could be a raw example of this (based on your example).
JSF code
<h:form >
<h:inputHidden value="#{bean.aField}" />
<h:commandButton value="Submit" action="#{bean.anAction}" />
</h:form>
Managed bean code
@ManagedBean
@RequestScoped
public class Bean {
private String aField;
//constructor, getter and setter...
public void anAction() {
//do your form processing...
HttpRequestHandler httpRequestHandler = new HttpRequestHandler();
httpRequestHandler.handlePost(...); //send the arguments here
}
}
public class HttpRequestHandler {
public void handlePost(String ... parameters) {
//you do the Apache HttpClient POST handling here
//always create a class between your application and your third party libraries
//code adapted from HttpClient examples: http://hc.apache.org/httpcomponents-client-ga/examples.html
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httpPost = new HttpPost(...);// your URL goes here
//do as you please with the HttpPost request
} finally {
httpclient.getConnectionManager().shutdown();
}
}
}
If you don't want to add the Apache HttpClient library for this job, then you could use native Java classes like URLConnection
as shown here: Using java.net.URLConnection to fire and handle HTTP requests
Upvotes: 2