动态增加文件上传,表单中的数据如何处理(hibernate ,struts)
问题的提出:
主要是在附件上传中动态增加附件数目上传后,对于 struts 中存放数据的actionForm不知道如何设置以及原理不太清楚。
下面是具体的问题描述。
-------------------------------
jsp页面部分代码:
<input type="button" name="Button" value=" 添加 " onclick="addFile()">//可以动态添加多个附件上传
<FORM METHOD="POST" ACTION="<c:url value='SystemBigSlave.xsg?command=save'/>"
ENCTYPE="multipart/form-data" name="systemBigSlave" target="_self">
<table width="75%" border="0" align="center" id="upfile">
<tr>
<td><input type="file" name="theFile[0]" size="30"></td>
</tr>
</table>
</form>
<script language="javascript">
var row=1;
function addFile(){//每点击一下添加按钮就生成一个上传条
row++;
var obj = document.getElementById("upfile");
var r = obj.insertRow().insertCell();
var temp="";
temp="<input type=file name=theFile["+row+"] size=30>";//这里命名的都是theFile开头
r.innerHTML+=temp;
}
</script>
那相对应的Form应该怎么写来对应上传上来的信息呢。而hibernate 中的设置文件呢?
以下是公司给出的解决办法大家帮我理解一下,让后说说你们各自有什么好的办法。
--------------------------------------------
form :
private Map fileMap = new HashMap();//声明一个集合来放多个file
public FormFile getTheFile(int index) {//取单个文件
return (FormFile) fileMap.get(new Integer(index));
}
//多附件同名上传
public void setTheFile(int index, FormFile file) {//set 下标是index的文件
fileMap.put(new Integer(index), file); //把文件放在map中
}
public FormFile[] getFiles() {//取出文件的集合
return (FormFile[]) fileMap.values().toArray(new FormFile[fileMap.size()]);
}
问题一: setTheFile()中,struts怎么来运行自己,让jsp中的="<input type=file name=theFile["+row+"] size=30>" 中的 theFile[i]字段与setTheFile(int index, FormFile file)对应起来呢?页面中字段theFile[0],theFile[2],那对应的struts 中的Form应该要有 setTheFile[0],setTheFile[1]与之对应才对。对于struts的机制还不太了解请大家指点。
------------------------------------------------
相对应的hibernate.xml文件只有一个对应
<property
name="FileName"
type="java.lang.String"
column="FileName"
>