Spring missing the extension file

2019-07-31 08:10发布

问题:

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 ??

回答1:

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.



回答2:

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

@RequestMapping (value = "/delete/{file:.+}", method = RequestMethod.DELETE)


标签: spring-mvc