Spring Boot viewResolver not working - not mapping to given location

最后都变了- 提交于 2020-05-28 03:14:20

问题


I am developing spring boot application for learning.

@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/views/");
    resolver.setSuffix(".jsp");
    registry.viewResolver(resolver);
}

I have configured my view resolver as given above. And below is the API endpoint for page

@RequestMapping(value = "/",method = RequestMethod.GET)
public ModelAndView homePage(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("index");
    return modelAndView;
}

But when i hit this api, i am unable to get a response. I get 404 and error i get on console is No mapping found for HTTP request with URI [/WEB-INF/views/index.jsp] in DispatcherServlet with name 'dispatcherServle

How can i fix this ?


回答1:


for people struggling with issues like this - the Spring Boot documentation states here that JSPs are not supported when running in embeddable servlet container - i.e. when using JAR packaging.

when using WAR packaging, the suggestion given in the answers here should work - but check that in the final WAR there is a directory at root: WEB-INF/views, with your JSPs




回答2:


the view path is wrong /WEB-INF/view/ instead /view/

@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    registry.viewResolver(resolver);
}

or you can use the application.properties located in /resources/ instead

and add these properties

spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp

important: the jsp views should be in /resources/ directory



来源:https://stackoverflow.com/questions/47477312/spring-boot-viewresolver-not-working-not-mapping-to-given-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!