@requestmapping 和 ModelAndView

@requestmapping and ModelAndView

嗨,请帮我 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"

在您的 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