This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring portlet MVC forms, multipart data and servlets?

 
Jimi Svedenholm
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've gotten into a situation with the CMS we use in a project, where we need to build a form for a portlet, and that form should have some file fields. I planed to use Spring portlet MVC and their form solution, so that I get my bean/command object populated from the form inputs, and also be able to validate the form with ease.

Now, the problem is that the CMS has some servlet filter that reads any multipart/form-data information from the request, making it unavailable for me in the portlet.

When I asked the CMS support about this they said that the filter is an internal-only feature, and even though it is possible to get access to the data (ie the files) that the filter has "absorbed", it is not recommended nor supported, since they might change this implementation at any time. The only recommendation they had for me was to post the form to a servlet in the "portlet webapp", since that would not be subjected to the filter.

I tried their solution, and sure thing, I can now get access to the file data using CommonsMultipartResolver. But I still need to somehow show the form page again after the files has been handled. So now I'm faced with two options, that both have serious disadvantages:

1. I do a forward* to the actionURL of the portlet

2. I do a redirect

The problem with the forward is that I noticed that the onSubmitAction method is never triggered, instead the showForm method is triggered. For some reason my actionURL is handled as a RenderRequest instead of an ActionRequest when I do the forward from the servlet. The result is that my command object is not populated with the form values, and no validation is done. So all that happens is that a new empty form is displayed.

Also, a forward causes the URL in the browser to point to the servlet that the POST was issued for, and that is quite ugly. And this causes the relative urls on the page to stop working (even if that can be fixed by using only absolute urls everywhere).

The problem with the redirect is that I have to store everything in the session for retrieval in the portlet (using the APPLICATION scope). To append all the POST parameters as request parameters on the actionURL is not an option, since the form contains quite a few different input fields, and combined together the URL could be quite long.

What I really would want is a way to make the Spring Portlet MVC form population and validation work without having to do a bunch of manual spring-form stuff. But is that possible given the existing conditions?

If not, is there a good way to use Spring Portlet MVC form classes as regular tools somehow?

Or maybe my best choice is to just bite the bullet and do all the bean field population and validation manually?

Regards
/Jimi


* using getServletContext().getContext("/").getRequestDispatcher(actionURL).forward(multipartRequest, response);
 
Ashish Sarin
author
Ranch Hand
Posts: 432
5
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jimi,

I think switching between servlet and portlet can be tricky, but this is where you can only choose to go the servlet way. I would suggest that you use portlet to render form and use AJAX for uploading files , which'll save the effort to switch between portlets and servlets. You may some details in this Liferay Wiki article (which also contains the source code):http://www.liferay.com/community/wiki/-/wiki/Main/Spring-Hibernate-DWR

regards
ashish
 
Jimi Svedenholm
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Sarin wrote:Hi Jimi,

I think switching between servlet and portlet can be tricky, but this is where you can only choose to go the servlet way. I would suggest that you use portlet to render form and use AJAX for uploading files , which'll save the effort to switch between portlets and servlets. You may some details in this Liferay Wiki article (which also contains the source code):http://www.liferay.com/community/wiki/-/wiki/Main/Spring-Hibernate-DWR

regards
ashish


Hi Ashish,

Thanks for the tip! I don't understand how I have missed DWR. Some simple tests proved that this should work just fine.

/Jimi
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic