shop--6.店铺注册--店铺注册之Controller层(逻辑)

Posted windbag7

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shop--6.店铺注册--店铺注册之Controller层(逻辑)相关的知识,希望对你有一定的参考价值。

1.接收并转化相应的参数,包括店铺信息和图片信息

2.注册店铺

3.返回结果

    @Controller
@RequestMapping("/shopadmin")
public class ShopManagementController{
@Autowired
private ShopService shopService;
//
注册店铺 @RequestMapping(value="/registershop", method= RequestMethod.POST ) @ResponseBody public Map<String, Object> registerShop(HttpServletRequest request){ Map<String, Object> modelMap = new HashMap<String,Object>(); //1.接收并转化相应的参数,包括店铺信息和图片信息 String shopStr = HttpServletRequestUtil.getString( request, "shopStr" ); ObjectMapper mapper = new ObjectMapper(); // create once, reuse Shop shop = null; try { shop = mapper.readValue(shopStr, Shop.class); } catch (IOException 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 user = (PersonInfo) request.getSession().getAttribute("user"); shop.setOwner( user ); ShopExecution shopExecution = shopService.addShop(shop, shopImg); if(shopExecution.getState() == ShopStateEnum.CHECK.getState()){ modelMap.put( "success", true ); }else{ modelMap.put( "success", false ); modelMap.put( "errMsg", shopExecution.getStateInfo()); } }else{ modelMap.put("success", false); modelMap.put("errMsg", "请输入店铺信息"); } //3.返回结果 return modelMap; }
}

解析request里面的一些参数

HttpServletRequestUtil

 1 public class HttpServletRequestUtil {
 2     public static int getInt(HttpServletRequest request,String key) {
 3         try {
 4             return Integer.decode(request.getParameter(key));
 5         }catch (Exception e) {
 6             //如果转化失败
 7             return -1;
 8         }
 9     }
10     public static long getLong(HttpServletRequest request,String key) {
11         try {
12             return Long.valueOf(request.getParameter(key));
13         }catch (Exception e) {
14             //如果转化失败
15             return -1;
16         }
17     }
18     public static Double getDouble(HttpServletRequest request,String key) {
19         try {
20             return Double.valueOf(request.getParameter(key));
21         }catch (Exception e) {
22             //如果转化失败
23             return -1d;
24         }
25     }
26     public static boolean getBoolean(HttpServletRequest request,String key) {
27         try {
28             return Boolean.valueOf(request.getParameter(key));
29         }catch (Exception e) {
30             //如果转化失败
31             return false;
32         }
33     }
34     
35     public static String getString(HttpServletRequest request,String key) {
36         try {
37             String result = request.getParameter(key);
38             if(result!=null) {
39                 result = result.trim();
40             }
41             if("".equals(result)) {
42                 result = null;
43             }
44             return result;
45         }catch (Exception e) {
46             return null;
47         }
48     }
49 }

 

 

改造后的Controller

技术分享图片
 1 @RequestMapping(value = "/registershop", method = RequestMethod.POST)
 2     @ResponseBody
 3     private Map<String, Object> registerShop(HttpServletRequest request) {
 4         Map<String, Object> modelMap = new HashMap<String, Object>();
 5         if (!CodeUtil.checkVerifyCode(request)) {
 6             modelMap.put("success", false);
 7             modelMap.put("error", "输入了错误的验证码");
 8             return modelMap;
 9         }
10         // 1.接受并转化相应的参数,包括店铺信息以及图片信息
11         String shopStr = HttpServletRequestUtil.getString(request, "shopStr");
12         ObjectMapper mapper = new ObjectMapper();
13         Shop shop = null;
14         try {
15             shop = mapper.readValue(shopStr, Shop.class);
16         } catch (Exception e) {
17             modelMap.put("success", false);
18             modelMap.put("errMsg", e.getMessage());
19             return modelMap;
20         }
21         CommonsMultipartFile shopImg = null;
22         CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
23                 request.getSession().getServletContext());
24         // 如果文件上传有值的话
25         if (commonsMultipartResolver.isMultipart(request)) {
26             MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
27             shopImg = (CommonsMultipartFile) multipartHttpServletRequest.getFile("shopImg");
28         } else {
29             modelMap.put("success", false);
30             modelMap.put("errMsg", "上传图片不能为空");
31             return modelMap;
32         }
33         // 2.注册店铺
34         if (shop != null && shopImg != null) {
35 
36             PersonInfo owner = (PersonInfo) request.getSession().getAttribute("user");
37 
38             shop.setOwner(owner);
39             ShopExecution se;
40             try {
41                 se = shopService.addShop(shop, shopImg.getInputStream(), shopImg.getOriginalFilename());
42                 if (se.getState() == ShopStateEnum.CHECK.getState()) {
43                     modelMap.put("success", true);
44                     // 需要在session中保存一个店铺列表 就是用户可操作的店铺
45                     @SuppressWarnings("unchecked")
46                     List<Shop> shopList = (List<Shop>) request.getSession().getAttribute("shopList");
47                     if (shopList == null || shopList.size() == 0) {
48                         shopList = new ArrayList<Shop>();
49 
50                     }
51                     shopList.add(se.getShop());
52                     request.getSession().setAttribute("shopList", shopList);
53 
54                 } else {
55                     modelMap.put("errMsg", se.getStateInfo());
56                 }
57             } catch (ShopOperationException e) {
58                 modelMap.put("success", false);
59                 modelMap.put("errMsg", e.getMessage());
60             } catch (IOException e) {
61                 modelMap.put("success", false);
62                 modelMap.put("errMsg", e.getMessage());
63             }
64 
65             return modelMap;
66         } else {
67             modelMap.put("success", false);
68             modelMap.put("errMsg", "请输入店铺信息");
69             return modelMap;
70         }
71         // 3.返回结果
72     }
View Code

 










以上是关于shop--6.店铺注册--店铺注册之Controller层(逻辑)的主要内容,如果未能解决你的问题,请参考以下文章

shop--6.店铺注册--js实现

shop--6.店铺注册

shop--6.店铺注册--使用kaptcha实现验证码

校园商铺-4店铺注册功能模块-6店铺注册之Controller层的改造

校园商铺-4店铺注册功能模块-1Dao层之新增店铺

校园商铺-4店铺注册功能模块-11店铺类别区域信息的获取