Spring missing the extension file

2019-07-31 07:44发布

I am developing an application using Spring MVC, and Extjs and I have a problem, I need to pass a file path to my controller, which going to delete the image with the path. However in my view the path is correct, but when the request arrive in controller the path is without extension.

View : notepad-icon.png

Controller: notepad-icon

@RequestMapping (value = "/delete/{file}", method = RequestMethod.DELETE)
public ModelAndView delete(@PathVariable String file){
    ModelAndView view = new ModelAndView(VIEW);
    service.delete(file);
    view.addObject("success", Boolean.TRUE);
    return view;
}

Can anyone provide me a insight, please ??

标签: spring-mvc
2条回答
不美不萌又怎样
2楼-- · 2019-07-31 08:08

If it is a simple extension can I recommend doing this:

@RequestMapping (value = "/delete/{file}.{ext}", method = RequestMethod.DELETE)
public ModelAndView delete(@PathVariable("file") String file, @PathVariable("ext") String ext){
    ModelAndView view = new ModelAndView(VIEW);
    service.delete(file + "." + ext);
    view.addObject("success", Boolean.TRUE);
    return view;
}

Another way to do this will be a little roundabout - this will be by setting the useSuffixPatternMatch flag in RequestMappingHandlerMapping to false, which should give you the entire filename, however setting the flag is a little difficult.

查看更多
叼着烟拽天下
3楼-- · 2019-07-31 08:16

Just add {file:.+} to the RequestMapping.

@RequestMapping (value = "/delete/{file:.+}", method = RequestMethod.DELETE)
查看更多
登录 后发表回答