同学好,感觉还是前端或者后端写错了,信息不足不好定位,直接复制粘贴我的替换你的修改下看看?
shopoperation.js
/**
*
*/
$(function(){
var initUrl = '/bsp/shopadmin/getshopinitinfo';
var registerShopUrl = '/bsp/shopadmin/registershop';
alert(initUrl);
getShopInitInfo();
function getShopInitInfo(){
$.getJSON(initUrl,function(data){
if(data.success){
var tempHtml = '';
var tempAreaHtml = '';
data.shopCategoryList.map(function(item,index){
tempHtml += '<option data-id="' + item.shopCategoryId + '">'
+ item.shopCategoryName+'</option>';
});
data.areaList.map(function(item,index){
tempAreaHtml += '<option data-id="' + item.areaId + '">'
+item.areaName +'</option>';
});
$('#shop-category').html(tempHtml);
$('#area').html(tempAreaHtml);
}
});
$('#submit').click(function(){
var shop = {};
shop.shopName = $('#shop-name').val();
shop.shopAddr = $('#shop-addr').val();
shop.phone = $('#shop-phone').val();
shop.shopDesc = $('#shop-desc').val();
shop.shopCategory = {
shopCategoryId : $('#shop-category').find('option').not(function(){
return !this.selected;
}).data('id')
};
shop.area = {
areaId : $('#area').find('option').not(function(){
return !this.selected;
}).data('id')
};
var shopImg = $('#shop-img')[0].files[0];
var formData = new FormData();
formData.append('shopImg',shopImg);
formData.append('shopStr',JSON.stringify(shop));
var verifyCodeActual = $('#j_captcha').val();
if (!verifyCodeActual) {
$.toast('请输入验证码!');
return;
}
formData.append('verifyCodeActual',verifyCodeActual);
$.ajax({
url : registerShopUrl,
type : 'POST',
data : formData,
contentType : false,
processData : false,
cache : false,
success : function(data){
if(data.success){
$.toast('提交成功!');
}else{
$.toast('提交失败!' + data.errMsg);
}
$('#captcha_img').click();
}
});
});
}
})ShopManagementController.java
package com.sdyu.bsp.web.shopadmin;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sdyu.bsp.dto.ShopExecution;
import com.sdyu.bsp.entity.Area;
import com.sdyu.bsp.entity.PersonInfo;
import com.sdyu.bsp.entity.Shop;
import com.sdyu.bsp.entity.ShopCategory;
import com.sdyu.bsp.enums.ShopStateEnum;
import com.sdyu.bsp.exceptions.ShopOpreationException;
import com.sdyu.bsp.service.AreaService;
import com.sdyu.bsp.service.ShopCategoryService;
import com.sdyu.bsp.service.ShopService;
import com.sdyu.bsp.util.CodeUtil;
import com.sdyu.bsp.util.HttpServletRequestUtil;
@Controller
@RequestMapping("/shopadmin")
public class ShopManagementController {
@Autowired
private ShopService shopService;
@Autowired
private ShopCategoryService shopCategoryService;
@Autowired
private AreaService areaService;
@RequestMapping(value = "/getshopinitinfo", method = RequestMethod.GET)
@ResponseBody
private Map<String, Object> getShopInitInfo() {
Map<String, Object> modelMap = new HashMap<String, Object>();
List<ShopCategory> shopCategoryList = new ArrayList<ShopCategory>();
List<Area> areaList = new ArrayList<Area>();
try {
shopCategoryList = shopCategoryService.getShopCategoryList(new ShopCategory());
areaList = areaService.getAreaList();
modelMap.put("shopCategoryList", shopCategoryList);
modelMap.put("areaList", areaList);
modelMap.put("success", true);
} catch (Exception e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
}
return modelMap;
}
@RequestMapping(value = "/registershop", method = RequestMethod.POST)
@ResponseBody
private Map<String, Object> registerShop(HttpServletRequest request) {
Map<String, Object> modelMap = new HashMap<String, Object>();
if (!CodeUtil.checkVerifyCode(request)) {
modelMap.put("success", false);
modelMap.put("errMsg", "输入了错误的验证码");
return modelMap;
}
// 1.接收并转化相应的参数,包括店铺信息以及图片信息
String shopStr = HttpServletRequestUtil.getString(request, "shopStr");
ObjectMapper mapper = new ObjectMapper();
Shop shop = null;
try {
shop = mapper.readValue(shopStr, Shop.class);
} catch (Exception e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
CommonsMultipartFile shopImg = null;
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
if (commonsMultipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
shopImg = (CommonsMultipartFile) multipartHttpServletRequest.getFile("shopImg");
} else {
modelMap.put("success", false);
modelMap.put("errMsg", "上传图片不能为空");
return modelMap;
}
// 2.注册店铺
if (shop != null && shopImg != null) {
PersonInfo owner = new PersonInfo();
// Session TODO
owner.setUserId(1L);
shop.setOwner(owner);
ShopExecution se;
try {
se = shopService.addShop(shop, shopImg.getInputStream(), shopImg.getOriginalFilename());
if (se.getState() == ShopStateEnum.CHECK.getState()) {
modelMap.put("success", true);
} else {
modelMap.put("success", false);
modelMap.put("errMsg", se.getStateInfo());
}
} catch (ShopOpreationException e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
} catch (IOException e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
}
return modelMap;
} else {
modelMap.put("success", false);
modelMap.put("errMsg", "请输入店铺信息");
return modelMap;
}
}
// private static void inputStreamToFile(InputStream ins,File file) {
// FileOutputStream os=null;
// try {
// os=new FileOutputStream(file);
// int bytesRead=0;
// byte[] buffer=new byte[1024];
// while ((bytesRead=ins.read(buffer))!=-1) {
// os.write(buffer,0,bytesRead);
// }
// } catch (Exception e) {
// throw new RuntimeException("调用inputStreamToFile产生异常:"+e.getMessage());
// }finally {
// try {
// if(os!=null) {
// os.close();
// }
// if(ins!=null) {
// ins.close();
// }
// } catch (IOException e) {
// throw new RuntimeException("inputStreamToFile关闭IO产生异常:"+e.getMessage());
// }
// }
// }
}