开发者

Struts multiple file upload with Dyna Action Forms (Struts 1)

I need to upload multiple files on a single page. With DynaAction forms you must specify the "name" of each one. I need this to be dynamic. I believe that I can use an array/list to get a bunch of files, but I can't match the files to anything specific.

A map would be perfect, but I am afraid I cannot figure out the "key" part of the map开发者_如何学Python.

Any ideas?


So I researched and spent 3 hours trying different stuff. There is a dearth of info on the web concerning this. Lots of unanswered questions. Now that Struts 1 is sunset, there will probably be no more info, so I thought I would add a nail to the coffin...

I discovered that I was making this a bigger deal than I needed to. With the help of these two pages:

Building a controller

Index Properties

I figured out that what you really want to do is use a Map to store the FormFile Objects. Most of the documentation I found was relying on Model Objects but I was using DynaForms. So the Struts-config.xml looks like this:

<form-property name="theFiles" type="java.util.HashMap"/>

The JSP looks like this:

 <c:forEach items="${userForm.map.roleChanges}" var="changeMap" varStatus="status"> 
...
    <html:file property="theFile(${changeMap.key.roleName})"  styleId="theFile" />
...     
</c:forEach>

The secret sauce is the property: theFiles(${changeMap.key.roleName}). Since theFiles is a Map, Struts needs a key, and you use the syntax above to get it in the map. The Java to get the map is as follows:

Map fileMap =  (Map)userForm.get("theFile");

This is a Map of FormFile objects. Sweet as can be.

I got hung up because I never used the ArrayList/Map stuff before. It is quite simple (as it should be. Also, there is lots of talk about the "index" parameter on some Struts tags. I fiddled around with it for a while, but could not get it to work. I think it is because of DynaForm. Not sure...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜