Reputation: 5
Hi help me please with Spring RequestMapping. I have page like this:
<form action="/add_photo" enctype="multipart/form-data" method="POST">
Photo: <input type="file" name="photo">
<input type="submit" />
</form>
and controller like this:
@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();
}
}
}
method "onIndex" is working but onAddPhoto seems not and when I click button whith URL "/add_photo" it gives me 404 instead page "result"
Upvotes: 0
Views: 1753
Reputation: 1590
Use pageContext in your jsp page as:
<form action="${pageContext.request.contextPath}/add_photo" enctype="multipart/form-data" method="POST">
Photo: <input type="file" name="photo">
<input type="submit" />
</form>
For more details: Check this
Upvotes: 1