SSM处理表单提交包含上传文件的整个表单的办法
别再找了,各种说法都有,但是我翻看了很多都不全,我就把我写的还算全套拿出来。。。。。
我的前台代码是:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/5/4
Time: 21:56
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改商品信息</title>
</head>
<body>
<!-- enctype="multipart/form-data" -->
<form id="itemForm" action="${pageContext.request.contextPath }/items/updateItems.do" enctype="multipart/form-data" method="post">
<input type="hidden" name="id" value="${items.id }" /> 修改商品信息:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td><input type="text" name="name" value="${items.name }" /></td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" name="price"
value="${items.price }" /></td>
</tr>
<tr>
<td>商品生产日期</td>
<td><input type="text" name="createtime"
value="<fmt:formatDate value="${items.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>" /></td>
</tr>
<tr>
<td>商品图片</td>
<td><c:if test="${items.pic !=null}">
<img src="/pic/${items.pic}" width=100 height=100 />
<br />
</c:if> <input type="file" name="upload" /></td>
</tr>
<tr>
<td>商品简介</td>
<td><textarea rows="3" cols="30" name="detail">${items.detail }</textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>
我的Controller代码是:
@RequestMapping("/updateItems.do")
public String updateItems(@Param("id") Integer id, @Param("name") String name, @Param("price") Float price, @Param("createtime") String createtime,
@Param("detail") String detail, @Param("upload") MultipartFile upload, HttpServletRequest request) throws Exception {
String realPath = request.getSession().getServletContext().getRealPath("/pic/");
File file = new File(realPath);
if(!file.exists()) {
file.mkdirs();
}
Items items = new Items();
if(upload!=null) {
String filename = upload.getOriginalFilename();
upload.transferTo(new File(realPath,filename));
items.setPic(filename);
}else {
items.setPic(null);
}
items.setDetail(detail);
items.setId(id);
items.setPrice(price);
items.setName(name);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
items.setCreatetime(sdf.parse(createtime));
productService.update(items);
return "redirect:findAll.do";
}
我的dao层代码是:
@Update("update items set name = #{name},price=#{price},pic=#{pic},createtime=#{createtime},detail=#{detail} where id=#{id}")
void update(@Param("id") Integer id, @Param("createtime") Date createtime, @Param("detail") String detail, @Param("name") String name, @Param("pic") String pic,@Param("price") Float price) throws Exception;
**还有一定要导入pom依赖:
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4`**
springmvc.xml:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760" />
</bean>
其他就没什么了,