嗨,请帮我用一下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而不是页面"result“。
发布于 2018-01-29 01:40:49
在jsp页面中使用pageContext:
<form action="${pageContext.request.contextPath}/add_photo" enctype="multipart/form-data" method="POST">
Photo: <input type="file" name="photo">
<input type="submit" />
</form>有关更多细节,请访问:Check this
https://stackoverflow.com/questions/48489323
复制相似问题