1、创建ShopManagementController
2、编写HttpServletRequestUtil类解析HttpServletRequest的参数
3、引入json解析的jar包依赖
4、接收并转化响应的参数,包括店铺信息及图片信息
5、注册店铺
先完成controller 相关的springMVC标签的定义
注意:
记得写@Controller
第三个红框method是POST,用于提交前台的表单数据
ResponseBody 是将Map转为JSON
HttpServletRequest用于存储前台提交的数据
import javax.servlet.http.HttpServletRequest;
/**
* 解析HttpServletRequest的参数
* @author shawn
*
*/
public class HttpServletRequestUtil {
public static int getInt(HttpServletRequest request,String key) {
try {
//从request中对应键位为key的值,转成整型
return Integer.decode(request.getParameter(key));
} catch(Exception e) {
return -1;
}
}
public static long getLong(HttpServletRequest request,String key) {
try {
//从request中提取key,转成长整型
return Long.valueOf(request.getParameter(key));
} catch(Exception e) {
return -1;
}
}
public static Double getDouble(HttpServletRequest request,String key) {
try {
//从request中提取key,转成double型
return Double.valueOf(request.getParameter(key));
} catch(Exception e) {
return -1d;
}
}
public static boolean getBoolean(HttpServletRequest request,String key) {
try {
//从request中提取key,转成boolean型
return Boolean.valueOf(request.getParameter(key));
} catch(Exception e) {
return false;
}
}
public static String getString(HttpServletRequest request,String key) {
try {
String result = request.getParameter(key);
if(result != null) {
result.trim();
}
if("".equals(result)) {
result = null;
}
return result;
} catch (Exception e) {
return null;
}
}
}
https://github.com/FasterXML/jackson-databind 查看API
将实体类转为json,或将json转为实体类
1 接受前台传来的店铺相关的字符串信息
2 转换成shop实体类,shop = mapper.readValue(shopStr, Shop.class); 这句话是按照API来写的。
3 处理图片逻辑
这里addShop()报错,因为里面参数类型需要Shop和File类型(File类型是为了便于shopService做单元测试),而上面第二个参数传入的是CommonsMultipartFile类型,在这里不能转换为File。
解决方法:
由于shopImg是CommonsMultipartFile类型,查看其源码有转换为InputStream的方法
因此可以再将 InputStream转为File
添加方法
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());
}
}
}
最终代码
@Controller
@RequestMapping("/shopadmin")
public class ShopManagementController {
@Autowired
private ShopService shopService;
@RequestMapping(value="/registershop",method=RequestMethod.POST)
@ResponseBody
private Map registerShop(HttpServletRequest request){
Map modelMap = new HashMap();
//1、接收并转化响应的参数,包括店铺信息及图片信息
String shopStr = HttpServletRequestUtil.getString(request, "shopStr");
ObjectMapper mapper = new ObjectMapper();
Shop shop = null;
try {
//转换成shop实体类
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());//从request会话中的上下文获取相关文件上传的内容
//判断是否有上传的文件流
if(commonsMultipartResolver.isMultipart(request)) {
//将request转换成MultipartHttpServletRequest对象
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
shopImg = (CommonsMultipartFile)multipartHttpServletRequest.getFile("shopImg");//shopImg是前端约定好的变量中传来的
}else {
modelMap.put("success", false);
modelMap.put("errMsg", "上传图片不能为空");
return modelMap;
}
//2、注册店铺
if(shop!=null&&shopImg!=null) {
PersonInfo owner = new PersonInfo();
owner.setUserId(1L);
shop.setOwner(owner);
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 (IOException e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
ShopExecution se = shopService.addShop(shop,shopImgFile);
if(se.getState() == ShopStateEnum.CHECK.getState()) {
modelMap.put("success", true);
} else {
modelMap.put("success", false);
modelMap.put("errMsg", se.getStateInfo());
}
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());
}
}
}
}