问题
I have an action bean method:
public void submitForm() {
// here bean save some information about input data in local DB
saveEnteredData();
// I should redirect to external system page with post params.
}
So i should save data and make a redirection into page of external system with some params by POST
I hope that you'll help me.
回答1:
Send a HTTP 307 redirect. Assuming that you're still on JSF 1.x:
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
response.setStatus(307);
response.setHeader("Location", "http://other.com");
facesContext.responseComplete();
This will however issue a (non-JSF-related) security confirmation warning at the client side with the question whether the site may resend the entered data to the other site.
If you dislike this warning, then there's no other option than to play for proxy yourself.
URLConnection connection = new URL("http://other.com").openConnection();
connection.setDoOutput(true); // POST
// Copy headers if necessary.
InputStream input1 = request.getInputStream();
OutputStream output1 = connection.getOutputStream();
// Copy request body from input1 to output1.
InputStream input2 = connection.getInputStream();
OutputStream output2 = response.getOutputStream();
// Copy response body from input2 to output2.
This will however not change the URL and the client thinks he's still on your site.
来源:https://stackoverflow.com/questions/4913289/jsf-i-want-to-send-post-request-inside-action-bean