예제 #1
0
 // page 202
 @RequestMapping(method = RequestMethod.GET, params = "new")
 public String createSpitterProfile(Model model) {
   Spitter spitter = new Spitter();
   spitter.setFullName("input your name here");
   model.addAttribute(spitter);
   return "spitters/edit";
 }
예제 #2
0
  // page 205
  @RequestMapping(method = RequestMethod.POST)
  public String addSpitterFromForm(
      @Valid Spitter spitter,
      BindingResult bindingResult,
      @RequestParam(value = "image", required = false) // 接收文件上传
          MultipartFile image) {

    if (bindingResult.hasErrors()) {
      return "spitters/edit";
    }
    spitterService.saveSpitter(spitter);

    try {
      if (!image.isEmpty()) {
        validateImage(image);
        saveImage(spitter.getId() + ".jpg", image); // 保存图片文件
      }
    } catch (ImageUploadException e) {
      bindingResult.reject(e.getMessage());
      return "spitters/edit";
    }

    return "redirect:/spitters/" + spitter.getUsername();
  }