问题描述
我得到的Spring MVC 3,工作正常经典的多部分形式:我可以上传文件,带2个参数(名称和说明)控制器。我的控制得到MultipartFile文件和参数,并把数据库与DAO类。
我的目标:只需添加到我的形式的进度栏上传文件!
谁能帮助我,告诉我不同的步骤做。我preFER做它DWR如果可能的话,露出ProgressListener方法。(实施ProgressListener为MultipartResolver,JavaScript来添加到我的表格)
任何帮助,将鸭preciate!
下面是我的形式:(ajoutDocumentRapport.jsp)
<形式:形式方法=邮报行动=save.html的CommandName =documentFormBeanENCTYPE =的multipart / form-data的>
<输入类型=隐藏名称=油库值=$ {depot.id}/>
<表>
&其中; TR>
< TD><形式:标签路径=名与GT;文件&LT的名称; /形式:标签>< / TD>
< TD><形式:输入路径=名/>< / TD>
< TD><形式:错误路径=名的CssClass =错误/>< / TD>
< / TR>
&其中; TR>
< TD><形式:标签路径=描述>文件&LT的说明; /形式:标签>< / TD>
< TD><形式:文本路径=说明/>< / TD>
< TD><形式:错误路径=说明的CssClass =错误/>< / TD>
< / TR>
&其中; TR>
< TD><形式:标签路径=内容>文件< /形式:标签>< / TD>
< TD><输入类型=文件名称=文件ID =文件>< /输入>< / TD>
< / TR>
&其中; TR>
&所述; TD列跨度=2>
<输入类型=提交值=添加文件。/>
< / TD>
< / TR>
< /表>
< /形式:形式GT;
这里是我的控制器:
@RequestMapping(值=/保存,方法= RequestMethod.POST)
公共ModelAndView的保存(HttpServletRequest的要求,
@Valid DocumentFormBean documentFormBean,BindingResult结果,
@RequestParam(文件)MultipartFile文件){
地图<字符串,对象>图=新的HashMap<字符串,对象>();
map.put(documentFormBean,新DocumentFormBean());
map.put(documentList,documentDao.findAllForaDepot(的Long.parseLong((字符串)的request.getParameter(库))));
map.put(油库,depotDao.find(的Long.parseLong((字符串)的request.getParameter(库))));
如果(result.hasErrors()){
map.put(错误,结果);
返回新的ModelAndView(ajoutDocumentsRapport,映射);
} 其他{
尝试 {
一滴一滴= Hibernate.createBlob(file.getInputStream());
文档新建文档=新的文件();
newDocument.setTitle(documentFormBean.getName());
newDocument.setDescription(documentFormBean.getDescription());
newDocument.setFilename(documentFormBean.getName());
newDocument.setContentType(file.getContentType());
newDocument.setFilename(file.getOriginalFilename());
newDocument.setContent(BLOB);
newDocument.setDepot(depotDao.find(的Long.parseLong((字符串)的request.getParameter(库))));
documentDao.save(新建文档);
map.put(documentList,documentDao.findAllForaDepot(的Long.parseLong((字符串)的request.getParameter(库))));
}赶上(IOException异常E){
e.printStackTrace();
}
返回新的ModelAndView(ajoutDocumentsRapport,映射);
}
}
确认这个解决方案,我用:使用Spring MVC 3.1和jQuery,使其工作
的
I got a classic multipart form with spring mvc 3 which works fine : I can upload a file to a controller with 2 parameters ( name and description).My controller get the MultipartFile file and parameters, and put in database with DAO classes.
My Goal : just add to my form a progress bar while uploading the file !
Can anyone help me by telling me different steps to do.I prefer do it with DWR if possible to expose the ProgressListener method.(implement a ProgressListener for for MultipartResolver ,javascript to add to my form)
Any help Would Be Appreciate!
Here is my form : ( ajoutDocumentRapport.jsp)
<form:form method="post" action="save.html" commandName="documentFormBean" enctype="multipart/form-data">
<input type="hidden" name="depot" value="${depot.id}"/>
<table>
<tr>
<td><form:label path="name">Name of file</form:label></td>
<td><form:input path="name" /></td>
<td><form:errors path="name" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="description">Description of file</form:label></td>
<td><form:textarea path="description" /></td>
<td><form:errors path="description" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="content">Document</form:label></td>
<td><input type="file" name="file" id="file"></input></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="add Document."/>
</td>
</tr>
</table>
</form:form>
And here is my controller :
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView save(HttpServletRequest request,
@Valid DocumentFormBean documentFormBean,BindingResult result,
@RequestParam("file") MultipartFile file) {
Map<String,Object> map = new HashMap<String, Object>();
map.put("documentFormBean", new DocumentFormBean());
map.put("documentList",documentDao.findAllForaDepot(Long.parseLong((String) request.getParameter("depot"))));
map.put("depot", depotDao.find(Long.parseLong((String) request.getParameter("depot"))));
if (result.hasErrors()) {
map.put("errors", result);
return new ModelAndView("ajoutDocumentsRapport", map);
} else{
try {
Blob blob = Hibernate.createBlob(file.getInputStream());
Document newDocument = new Document();
newDocument.setTitle(documentFormBean.getName());
newDocument.setDescription(documentFormBean.getDescription());
newDocument.setFilename(documentFormBean.getName());
newDocument.setContentType(file.getContentType());
newDocument.setFilename(file.getOriginalFilename());
newDocument.setContent(blob);
newDocument.setDepot(depotDao.find(Long.parseLong((String) request.getParameter("depot"))));
documentDao.save(newDocument);
map.put("documentList", documentDao.findAllForaDepot(Long.parseLong((String) request.getParameter("depot"))));
} catch (IOException e) {
e.printStackTrace();
}
return new ModelAndView("ajoutDocumentsRapport",map);
}
}
Check this solution that I use:Use Spring MVC 3.1 and JQuery to make it work
http://abambenjamin.blogspot.mx/2012/04/ajax-jquery-html5-progressbar-spring.html
这篇关于Spring MVC的&放大器; DWR - 需要帮助实施进度栏上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!