addProduct.jsp页面代码如下
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>添加商品</h1>
<form action="addProductAction.do" method="post" enctype="multipart/form-data">
<p>商品名: <input type="text" name="prodname"/></p>
<p>描述: <input type="text" name="describe"/></p>
<p>单价: <input type="text" name="price"/></p>
<p>图片: <input type="file" name="file"/></p>
<p><input type="submit" value="提交" /></p>
</form>
</body>
</html>
ProductController控制器页面代码如下package cn.bdqn.controller;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import cn.bdqn.pojo.Product;
import cn.bdqn.service.ProductService;
@Controller
public class ProductController {
@Autowired
private ProductService productService;
@RequestMapping("/addProduct")
public String addProduct() {
return "addProduct";
}
@RequestMapping("/show")
public String show() {
return "show";
}
@RequestMapping("/addProductAction")
public String addProductAction(MultipartFile file, Product product,
HttpServletRequest request) {
try {
// 原始的文件名
String originalFilename = file.getOriginalFilename();
String perfix = originalFilename.substring(0,
originalFilename.lastIndexOf("."));
String suffix = originalFilename.substring(originalFilename
.lastIndexOf(".") + 1);
String[] types = {"jpg","png","gif"};
List<String> typeList = Arrays.asList(types);
if(!typeList.contains(suffix)){
throw new Exception("上传的文件类型不匹配");
}
String filename = perfix + "-" + System.currentTimeMillis() + "."
+ suffix;
product.setImgpath(filename);
// String upload = request.getServletContext().getRealPath("upload")
// + File.separator;
File newFile = new File("D:\\development\\fileserver\\" + filename);
// 将文件写到本地
file.transferTo(newFile);
int addProduct = productService.addProduct(product);
if (addProduct != 1) {
throw new Exception("数据添加失败");
}
return "redirect:show.do";
} catch (Exception e) {
e.printStackTrace();
return "redirect:addProduct.do";
}
}
@RequestMapping("/getAllProduct.do")
@ResponseBody
public List<Product> getAllProduct() {
return productService.getAllProduct();
}
}
成功后的显示页面show.jsp<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="static/js/jquery.js"></script>
<script>
$(function(){
$.post("getAllProduct.do",function(result){
$.each(result,function(){
var $li = $('<li>'+
'<img src="http://localhost:8080/fileserver/'+this.imgpath+'"/>'+
'<p>'+this.prodname+'</p>'+
'<p>'+this.describe+'</p>'+
'<p>'+this.price+'</p>'+
'</li>');
$(".product").append($li);
});
});
});
</script>
<title>Insert title here</title>
</head>
<body>
<ul class="product">
</ul>
</div>
</body>
</html>