1. 程式人生 > >spring boot + html 頁面

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<Book> readingList = bookMapper
.findByReader(reader); if(readingList != null){ modelAndView.addObject("books", readingList); } return modelAndView; }
}
}

專案路徑:


啟動專案: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");

根本原因是什麼呢?