在使用Struts的系统中使用 org.apache.struts.upload.FormFile 可以很方便的实现文件的上传,无论是单个或者多个文件。不过又应该如何来实现不限个数的文件上传呢?
首先,在Jsp页面中,需要有像163那样动态添加附件的功能,这个可以使用JS来实现。代码如下:
[align=top" />fileTD";
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] var fileId = "uploadFile(" +fileIndex+ ")";
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] addInputFile(spanId, fileId);
[img]http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" align="top[/img] }
[img]http://www.blogjava.net/Images/OutliningIndicators/None.gif" alt="" align="top[/img] function addInputFile(spanId, fileId)
{
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] var span = document.getElementById(spanId);
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] if (span != null)
{
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] var divObj = document.createElement("div");
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] var fileObj, delObj;
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] divObj.id = fileId;
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] fileObj = document.createElement("");
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] delObj = document.createElement("");
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] fileObj.name = fileId;
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] fileObj.size = "40";
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] fileObj.className = "input";
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] delObj.value ='删除';
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] divObj.appendChild(fileObj);
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] divObj.appendChild(document.createTextNode(" "));
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] divObj.appendChild(delObj);
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] span.appendChild(divObj);
[img]http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" align="top[/img] }
[img]http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" align="top[/img] }
[img]http://www.blogjava.net/Images/OutliningIndicators/None.gif" alt="" align="top[/img] function delInputFile(spanId, fileId)
{
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] var span = document.getElementById(spanId);
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] var divObj = document.getElementById(fileId);
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] if (span != null && divObj != null)
{
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] span.removeChild(divObj);
[img]http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" align="top[/img] }
[img]http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" align="top[/img] }[/align]
上面代码中的Html和JS代码就可以实现动态添加行的效果。(上述JS代码参考了一位网友的实现,这里对他表示感谢。)
Html代码中的 容易让人迷惑,我也是第一次写 name="uploadFile(0)"这样的Html代码。
这里的uploadFile(0)表示数组中index=0的元素,那么在Struts的FormBean中又应该如何让BeanUtils在做反射的时候可以正确的赋值呢?
来看一下FormBean中的代码:
[align=top" />[/align]
这里提供的setUploadFIle(String index, FormFile formFile)其实就是给BeanUtils一个正确的借口,让它可以对uploadFile(0)这样的元素进行正确的反射。当然,这里不用考虑index,直接将formFile添加进list中。
因此,在Struts中不限个数上传文件的实现中,主要是页面中动态添加上传文件行和在FormBean中正确赋值,Action中要做的相信就很简单了,只要把FormBean中的List取出来逐个进行操作。
首先,在Jsp页面中,需要有像163那样动态添加附件的功能,这个可以使用JS来实现。代码如下:
[align=top" />fileTD";
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] var fileId = "uploadFile(" +fileIndex+ ")";
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] addInputFile(spanId, fileId);
[img]http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" align="top[/img] }
[img]http://www.blogjava.net/Images/OutliningIndicators/None.gif" alt="" align="top[/img] function addInputFile(spanId, fileId)
{
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] var span = document.getElementById(spanId);
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] if (span != null)
{
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] var divObj = document.createElement("div");
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] var fileObj, delObj;
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] divObj.id = fileId;
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] fileObj = document.createElement("");
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] delObj = document.createElement("");
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] fileObj.name = fileId;
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] fileObj.size = "40";
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] fileObj.className = "input";
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] delObj.value ='删除';
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] divObj.appendChild(fileObj);
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] divObj.appendChild(document.createTextNode(" "));
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] divObj.appendChild(delObj);
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] span.appendChild(divObj);
[img]http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" align="top[/img] }
[img]http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" align="top[/img] }
[img]http://www.blogjava.net/Images/OutliningIndicators/None.gif" alt="" align="top[/img] function delInputFile(spanId, fileId)
{
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] var span = document.getElementById(spanId);
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] var divObj = document.getElementById(fileId);
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] if (span != null && divObj != null)
{
[img]http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif" alt="" align="top[/img] span.removeChild(divObj);
[img]http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" align="top[/img] }
[img]http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" align="top[/img] }[/align]
上面代码中的Html和JS代码就可以实现动态添加行的效果。(上述JS代码参考了一位网友的实现,这里对他表示感谢。)
Html代码中的 容易让人迷惑,我也是第一次写 name="uploadFile(0)"这样的Html代码。
这里的uploadFile(0)表示数组中index=0的元素,那么在Struts的FormBean中又应该如何让BeanUtils在做反射的时候可以正确的赋值呢?
来看一下FormBean中的代码:
[align=top" />[/align]
这里提供的setUploadFIle(String index, FormFile formFile)其实就是给BeanUtils一个正确的借口,让它可以对uploadFile(0)这样的元素进行正确的反射。当然,这里不用考虑index,直接将formFile添加进list中。
因此,在Struts中不限个数上传文件的实现中,主要是页面中动态添加上传文件行和在FormBean中正确赋值,Action中要做的相信就很简单了,只要把FormBean中的List取出来逐个进行操作。