Search code examples
javascriptjsfservletsplupload

Integration beetween a compositeComponent's attribute and a Servlet in JSF


I'm trying to integrate Plupload with JSF in the most independent way possible, for that I created a composite component.

To use Plupload in my JSF page i just call:

<comp:plupload ... value=#{MyBean.files} />

Where MyBean.files is a list.

When I add files to Plupload component and click the button "start upload" I want it to upload everything to a temporary folder and fill the object specified in "value" of my composite component with these files properties (path, for instance).

To upload the files i'm using a servlet, it has nothing to do with JSF, it works fine for the 1st part: it uploads everything to a temporary folder. My problem is the second part, I did a lot of research, but I can't find a way to communicate the attribute "value" in my JSF composite component to my servlet.

Plupload uses javascript to configure everything, the request will be sent to the URL specified in the attribute "url" in the following code:

<composite:interface>
  ...
  <composite:attribute name="value" required="true" />
</composite:interface>
<composite:implementation>
  ...
  <script type="text/javascript">
    // Convert divs to queue widgets when the DOM is ready
    $(function() {
      $("#uploader").pluploadQueue({
        // General settings
        runtimes : '#{cc.attrs.runtimePreferences}',
        url : '/plupload',
        max_file_size : '#{cc.attrs.maxFileSize}',
        ...
      });
    });
  </script>

  <div id="uploader">
    <p><h:outputText value="Your browser does not support this." /></p>
  </div>
</composite:implementation>

I specified "/plupload" as url, that's my servlet's url (in web.xml).

I can think about two possible solutions:

  1. Continue using this servlet, which is completely independent from FacesServlet, and find a way to pass the attribute "value" in my composite component as a request attribute to my servlet. But, how can I do this?

  2. Stop using a new servlet, I should't do this since I'm using JSF, everything should be processed by FacesServlet. Instead of using a new servlet, i could create a ManagedBean. Inside this managedBean I could create a method and recover the HttpRequest and HttpResponse. It's easier to comunicate my compositeComponent with the method who handles the upload if it's a managedBean. Problem: How can i reference a managedBean's method through an URL? I still have to fill the attribute "url" in the javascript code. Is there anything like "/faces/MyBean?action='myMethod()'?

Thank you in advance for any answer ;)


Solution

  • Send it as request pathinfo.

    url: '/plupload/' + encodeURIComponent('#{cc.attrs.value}'), // You might want to escape JS special characters like singlequotes, newlines, etc as well, depending on what the value can contain.
    

    If the servlet is mapped on /plupload/* then you can obtain it as follows:

    String value = request.getPathInfo().substring(1);