校园商铺-6店铺编辑列表和列表功能-3店铺信息编辑之Controller层的实现
1.店铺编辑范围
除了店铺名称、店铺分类不可更改,其他信息(区域、地址、电话、缩略图、店铺简介)都可以更改。
2.编写获取店铺信息的controller
@RequestMapping(value="/getshopbyid",method=RequestMethod.GET)
@ResponseBody
private Map<String,Object> getShopById(HttpServletRequest request){
Map<String,Object> modelMap = new HashMap<String,Object>();
Long shopId = HttpServletRequestUtil.getLong(request, "shopid");
if(shopId > -1) {
try {
Shop shop = shopService.getByShopId(shopId);
List<Area> areaList = areaService.getAreaList();
modelMap.put("shop", shop);
modelMap.put("areaList", areaList);
modelMap.put("success", true);
}catch (Exception e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.toString());
}
}else {
modelMap.put("success", false);
modelMap.put("errMsg", "empty shopId");
}
return modelMap;
}
3.验证controller
访问地址http://127.0.0.1:18080/o2o/shopadmin/getshopbyid?shopid=1
4.更改店铺信息
店铺注册流程:
- 1.验证码校验
- 2.获取请求头的店铺信息 接收从前台传递的shopStr对象,将这个对象转换成Shop实体类
- 3.获取图片 将请求中的文件流从CommonsMultipartResolver解析为CommonsMultipartFile
- 4.注册店铺
更改店铺同注册流程:
- 1.验证码校验
- 2.获取请求头的店铺信息 接收从前台传递的shopStr对象,将这个对象转换成Shop实体类
- 3.获取图片 将请求中的文件流从CommonsMultipartResolver解析为CommonsMultipartFile
- 4.修改店铺
4.Session
在web开发中,服务器可以为每个用户浏览器创建一个会话对象,就是Session对象。一个浏览器独占一个Session对象。因此在保存用户数据时,服务器程序可以把用户信息写到用户浏览器独占的Session中,当用户使用浏览器访问服务器程序时,服务器程序可以从用户的Session中,取出该用户的数据为用户服务。服务器创建Session出来后,会把Session的ID号以cookie的形式回写给客户机,这样只要客户端的浏览器不关,再去访问服务器的时候,都会带着Session的ID号去。
服务器发现客户端的浏览器带Session的ID过来,就会使用内容中与之对应的Session为之服务。Session是有过期时间的,tomcat默认Session超时时间为30分钟。如果30分钟内没有在平台上做任何操作,Session就过期了。这种情况下,想通过Session去取相关的用户信息,就获取不到了。
Session主要用来保存店铺、用户信息。在我们去存储数据的时候,可以从里面取出想要的信息,同时还能方便我们做权限的验证。
在修改店铺信息的时候,是不需要修改用户的信息的,对它权限的验证,可以放在拦截器中去实现。
需要加入Session的地方是在注册中,获取用户的信息。
package com.csj2018.o2o.web.shopadmin;
import java.io.File;
import java.io.FileOutputStream;
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 java.io.InputStream;
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.csj2018.o2o.dto.ShopExecution;
import com.csj2018.o2o.entity.Area;
import com.csj2018.o2o.entity.PersonInfo;
import com.csj2018.o2o.entity.Shop;
import com.csj2018.o2o.entity.ShopCategory;
import com.csj2018.o2o.enums.ShopStateEnum;
import com.csj2018.o2o.exceptions.ShopOperationException;
import com.csj2018.o2o.service.AreaService;
import com.csj2018.o2o.service.ShopCategoryService;
import com.csj2018.o2o.service.ShopService;
import com.csj2018.o2o.util.CodeUtil;
import com.csj2018.o2o.util.HttpServletRequestUtil;
import com.csj2018.o2o.util.ImageUtil;
import com.csj2018.o2o.util.PathUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
// 首先在Controller里面定义了SpringMVC相关的标签,这个标签包含了Controller的访问路径以及registerregisterShop方法的访问路径,
@Controller
@RequestMapping("/shopadmin")
public class ShopManagerController {
// 同时给它在执行的时候通过Spring容器注入之前实现好的ShopService实现类,用来提供addShop的服务。
@Autowired
private ShopService shopService;
@Autowired
private ShopCategoryService shopCategoryService;
@Autowired
private AreaService areaService;
@RequestMapping(value="/getshopbyid",method=RequestMethod.GET)
@ResponseBody
private Map<String,Object> getShopById(HttpServletRequest request){
Map<String,Object> modelMap = new HashMap<String,Object>();
Long shopId = HttpServletRequestUtil.getLong(request, "shopid");
if(shopId > -1) {
try {
Shop shop = shopService.getByShopId(shopId);
List<Area> areaList = areaService.getAreaList();
modelMap.put("shop", shop);
modelMap.put("areaList", areaList);
modelMap.put("success", true);
}catch (Exception e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.toString());
}
}else {
modelMap.put("success", false);
modelMap.put("errMsg", "empty shopId");
}
return modelMap;
}
@RequestMapping(value = "getshopinitinfo",method = RequestMethod.GET)
@ResponseBody
private Map<String,Object> getShopInitInfo(HttpServletRequest request){
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>();
//1.验证码校验
if(!CodeUtil.checkVerifyCode(request)) {
modelMap.put("success", "false");
modelMap.put("message", "输入了错误的验证码");
return modelMap;
}
//2.获取请求头的店铺信息 接收从前台传递的shopStr对象,将这个对象转换成Shop实体类
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("errMeg", e.getMessage());
return modelMap;
}
// 3.获取图片 将请求中的文件流从CommonsMultipartResolver解析为CommonsMultipartFile
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;
}
// 4.注册店铺
if (shop != null && shopImg != null) {
/*
* 添加Session
* 注册店铺或对店铺做操作,是需要登陆的
*/
PersonInfo owner = (PersonInfo) request.getSession().getAttribute("user");
shop.setOwner(owner);
/*
* 由于addShop的第二个参数是File类型的,而传入的ShopImg是CommonsMultipartFile这样的一个类型,因此需要将CommonsMultipartFile转换成File类型
File shopImgFile = new File(PathUtil.getImgBasePath() + ImageUtil.getRandomFileName());
try {
shopImgFile.createNewFile();
} catch (IOException e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
try {
inputStreamToFile(shopImg.getInputStream(), shopImgFile);
} catch (Exception e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
*/
ShopExecution se;
try {
se = shopService.addShop(shop, shopImg.getInputStream(), shopImg.getOriginalFilename());
if (se.getState() == ShopStateEnum.CHECK.getState()) {
/*
* 在店铺添加完成后,还需要做Session的操作。用户和店铺的关系是一对多的,即一个owner能够创建多个店铺。
* 因此需要在Session里面保存一个店铺列表来显示用户可以操作的店铺。
*/
@SuppressWarnings("unchecked")
List<Shop> shopList = (List<Shop>) request.getSession().getAttribute("shopList");
if(shopList == null || shopList.size() == 0) {
shopList = new ArrayList<Shop>();
}
shopList.add(se.getShop());
request.getSession().setAttribute("shopList", shopList);
modelMap.put("success", true);
} else {
modelMap.put("success", false);
modelMap.put("errMsg", se.getStateInfo());
}
}catch (ShopOperationException 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;
}
}
@RequestMapping(value = "/modifyshop", method = RequestMethod.POST)
@ResponseBody
private Map<String, Object> modifyShop(HttpServletRequest request) {
Map<String, Object> modelMap = new HashMap<String, Object>();
//1.验证码校验
if(!CodeUtil.checkVerifyCode(request)) {
modelMap.put("success", "false");
modelMap.put("message", "输入了错误的验证码");
return modelMap;
}
//2.获取请求头的店铺信息 接收从前台传递的shopStr对象,将这个对象转换成Shop实体类
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("errMeg", e.getMessage());
return modelMap;
}
// 3.获取图片 将请求中的文件流从CommonsMultipartResolver解析为CommonsMultipartFile
CommonsMultipartFile shopImg = null;
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
if (commonsMultipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
shopImg = (CommonsMultipartFile) multipartHttpServletRequest.getFile("shopImg");
}
// 2.修改店铺
if (shop != null && shop.getShopId() != null) {
//由于图片是可上传、可不上传的,因此图片非空判断去除;取而代之确保shopId不为空
//修改店铺信息不用从session获取用户信息
PersonInfo owner = new PersonInfo();
// 预期从Session获取,目前自定义,以后完善
owner.setUserId(1L);
shop.setOwner(owner);
// 由于addShop的第二个参数是File类型的,而传入的ShopImg是CommonsMultipartFile这样的一个类型,因此需要将CommonsMultipartFile转换成File类型
/*
* 这块代码,教程没有
File shopImgFile = new File(PathUtil.getImgBasePath() + ImageUtil.getRandomFileName());
try {
shopImgFile.createNewFile();
} catch (IOException e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
try {
inputStreamToFile(shopImg.getInputStream(), shopImgFile);
} catch (Exception e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
*/
ShopExecution se;
try {
if(shopImg == null) {
se = shopService.modifyShop(shop, null, null);
}else {
se = shopService.modifyShop(shop, shopImg.getInputStream(), shopImg.getOriginalFilename());
}
if (se.getState() == ShopStateEnum.SUCCESS.getState()) {
modelMap.put("success", true);
} else {
modelMap.put("success", false);
modelMap.put("errMsg", se.getStateInfo());
}
}catch(ShopOperationException 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", "请输入店铺Id");
return modelMap;
}
}
private static void inputStreamToFile(InputStream inputStream, File file) {
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = inputStream.read(buffer)) > 0) {
os.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
throw new RuntimeException("调用inputStreamToFile产生异常:" + e.getMessage());
} finally {
try {
if (os != null) {
os.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
throw new RuntimeException("调用inputStreamToFile产生异常:" + e.getMessage());
}
}
}
}