嗨,请帮助我使用 Spring RequestMapping。我有这样的页面:
<form action="/add_photo" enctype="multipart/form-data" method="POST">
Photo: <input type="file" name="photo">
<input type="submit" />
</form>
和这样的控制器:
@Controller
@RequestMapping("/")
public class MyController {
private Map<Long, byte[]> photos = new HashMap<>();
@RequestMapping("/")
public String onIndex() {
return "index";
}
@RequestMapping(value = "/add_photo", method = RequestMethod.POST)
public ModelAndView onAddPhoto(@RequestParam MultipartFile photo) {
if (photo.isEmpty()) {
throw new PhotoErrorException();
}
try {
long id = System.currentTimeMillis();
photos.put(id, photo.getBytes());
ModelAndView model = new ModelAndView();
model.addObject("photo_id", id);
model.setViewName("result");
return model;
} catch (IOException e) {
throw new PhotoErrorException();
}
}
}
方法“onIndex”正在工作,但 onAddPhoto 似乎没有,当我单击带有 URL“/add_photo”的按钮时,它给了我 404 而不是页面“结果”