0

嗨,请帮助我使用 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 而不是页面“结果”

4

1 回答 1

1

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

有关更多详细信息:检查此

于 2018-01-28T17:40:49.393 回答