spring boot + html 页面

springboot + html 项目报错:

javax.servlet.ServletException: Circular view path [readingList]: would dispatch back to the current handler URL [/book/readingList] again. Check your ViewResolver setup!


controller 代码:

@RestController
@RequestMapping("/book")
public class ReadingListController {
    @Resource
    private BookMapper bookMapper;
    @RequestMapping(value="/{reader}",method = RequestMethod.GET)
    public ModelAndView readersBooks(@PathVariable("reader") String reader){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/book/readingList.html");
        List readingList = bookMapper.findByReader(reader);
        if(readingList != null){
            modelAndView.addObject("books", readingList);
        }
        return modelAndView;
    }
}
}


项目路径:

spring boot + html 页面_第1张图片


启动项目:gradle bootRun


访问:http://localhost:8080/book/pang

现象:一直error,javax.servlet.ServletException: Circular view path [readingList]: would dispatch back to the current handler URL [/book/readingList] again. Check your ViewResolver setup!


这个error是在InternalResourceView.prepareForRendering 方法中抛出的


debug 发现这个请求会走两次servlet,也就是会调用两次InternalResourceView.prepareForRendering 方法,第一次调用success,第二次调用时抛出异常


原因是:controller 返回的路径 /book/readingList.html 因为和mapping 的路径 @RequestMapping("/book") 重合,第一次返回response 后会再次匹配mapping,导致error。

解决方法:modelAndView.setViewName("/book/readingList.html"); 改成 modelAndView.setViewName("/page/readingList.html");

根本原因是什么呢?

你可能感兴趣的:(spring,spring,boot,gradle)