目录
1、编写ShopManagementController 的getshopbyid
2、编写ShopManagementController 的modifyshop
3、registershop的session添加
@RequestMapping(value="/getshopbyid",method=RequestMethod.GET)
@ResponseBody
private Map getShopById(HttpServletRequest request){
Map modelMap = new HashMap();
Long shopId = HttpServletRequestUtil.getLong(request, "shopId");//在将前台键位shopId的值传入
if(shopId > -1) {
try {
Shop shop = shopService.getByShopId(shopId);
List areaList = new ArrayList();
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;
}
测试
浏览器输入controller的路由,因为是GET方法,在shopId后传入参数1
http://localhost:8080/o2oDemo/shopadmin/getshopbyid?shopId=1
@RequestMapping(value="modifyshop",method=RequestMethod.POST)
@ResponseBody
private Map modifyShop(HttpServletRequest request){
Map modelMap = new HashMap();
//将验证码工具引入
if(!CodeUtil.checkVerifyCode(request)) {
modelMap.put("success", false);
modelMap.put("errMsg", "输入了错误的验证码");
return modelMap;
}
//1、接收并转化响应的参数,包括店铺信息及图片信息
String shopStr = HttpServletRequestUtil.getString(request, "shopStr");
ObjectMapper mapper = new ObjectMapper();//用于Java对象与JSON的互换
Shop shop = null;
try {
shop = mapper.readValue(shopStr, Shop.class);//转换成shop实体类
} catch(Exception e){
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
/**
* 以下处理图片相关逻辑
*/
CommonsMultipartFile shopImg = null;
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());//从request会话中的上下文获取相关文件上传的内容
//判断是否有上传的文件流
if(commonsMultipartResolver.isMultipart(request)) {
//将request转换成MultipartHttpServletRequest对象
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
shopImg = (CommonsMultipartFile)multipartHttpServletRequest.getFile("shopImg");//shopImg是前端约定好的变量中传来的
}
//2、修改店铺信息
if(shop!=null&&shop.getShopId()!=null) {
ShopExecution se;
try {
if(shopImg == null) {
//图片为空,传入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("error", e.getMessage());
} catch (IOException e) {
modelMap.put("success", false);
modelMap.put("error", e.getMessage());
}
return modelMap;
}else {
modelMap.put("success", false);
modelMap.put("errMsg", "请输入店铺Id");
return modelMap;
}
}
session的作用:
服务器可以为浏览器创建一个会话对象,在需要保存用户数据时,服务器可以把用户信息写到用户独占的session中,当用户使用浏览区访问服务器时,服务器可以从用户session中取出用户数据。服务器创建session后,会把session的id以cookie的形式回写给客户机,因此只要客户端浏览器不关闭,再去访问服务器时都会带着session的id,服务器发现该id后,会使用与之对应的内容进行服务。session有过期时间,Tomcat默认是30min,即30min不操作就会过期
获取session
request.getSession().getAttribute();