0

I have a JSF 2.0/primefaces web app on Tomcat 7.0.25. The application has a simple file upload form, as in the primefaces showcase.

<h:form enctype="multipart/form-data">
    <p:messages showDetail="true" />
    <p:fileUpload value="#{fileUploadController.file}" mode="simple" />
    <p:commandButton value="Submit" ajax="false" actionListener="#{fileUploadController.upload}" />
</h:form>
public void upload() throws IOException {
    FacesMessage msg = new FacesMessage("Succesful", this.file.getFileName() + " is uploaded.");
    FacesContext.getCurrentInstance().addMessage(null, msg);
    // do stuff with xml
}

I'm trying to make a multipart/form-data POST request to te upload form using a stndalone java client. Sinc now I've tried this and this but without success.

In both cases I receive the HTTP/1.1 200 OK response and I get the form page, as if the POST was not sent.

Any ideas?

Community
  • 1
  • 1
loscuropresagio
  • 1,832
  • 15
  • 26

1 Answers1

0

When programmatically submitting a form to a JSF web application, you need to make sure that you take the following 3 things in account:

  • Maintain the HTTP session (certainly if the JSF webapp has server side state saving turned on).
  • Send the name-value pair of the javax.faces.ViewState hidden input field.
  • Send the name-value pair of the submit button which is virtually to be pressed.

As to maintaining the HTTP session, when using URLConnection, add the following line before the first URLConnection creation ever.

CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

Or when using HttpClient, use the following:

HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, new BasicCookieStore());
// ...
HttpResponse getResponse = httpClient.execute(someHttpMethod, httpContext);

Sending the name=value pair of the javax.faces.ViewState hidden input field is mandatory in order to keep the JSF view state alive. Sending the name=value pair of the submit button is mandatory in order to let JSF invoke the action. This is the easiest if it does not have an autogenerated client ID, so give it and its parent form a fixed ID.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452