//
you're reading...
Eureka!

Multipart request handling with Struts and Spring

Struts 1.x has inbuilt mechanisms to handle multi part posts (file uploads). By default it uses Apache Commons Fileupload. In the application I was working on we used Struts as the MVC framework, but Spring to do pretty much everything else. Springs, Struts integration module was extensively used, and we were using Spring’s SpringBindingActionForm for almost all action form handling. On the same lines, we wanted to use Spring’s Multipart file support (MultipartFile).

Unfortunately, by the time request would reach Spring’s ServletRequestDataBinder, Commons Fileupload would have already handled the multi part request and wrapped it. And there was no available documentation on the Struts website to disable this feature. A little bit of hacking struts code did reveal a pretty easy solution, just disable multipart handling in the init parameters of the struts servlet in the web.xml of your application:

<!-- Struts Action <span class="hiddenSpellError" pre="Action ">Servlet</span> -->
<servlet>
	<servlet-name>ach-action</servlet-name>
	<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
	<init-param>
		The Struts Config File
		<param-name>config</param-name>
		/WEB-INF/config/struts-config.xml
	</init-param>
	<!-- The following parameter disables multi part handling by struts -->
	<init-param>
		<description>Disable Struts Multipart Handling</description>
		<param-name>multipartClass</param-name>
		<param-value>none</param-value>
	</init-param>
	<load-on-startup>2</load-on-startup>
</servlet>

This has been tested on Struts 1.3.8, but should work on other 1.x versions too.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About Random Musings

Random Musings is a collection of my personal thoughts, opinions, views and anything else that I find interesting. The views posted here are mine and may not necessarily reflect the views of MindTree Limited (my employer).
%d bloggers like this: