简体   繁体   English

Spring Boot viewResolver 不起作用 - 未映射到给定位置

[英]Spring Boot viewResolver not working - not mapping to given location

在此处输入图片说明

I am developing spring boot application for learning.我正在开发用于学习的 Spring Boot 应用程序。

@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下面是页面的 API 端点

@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.但是当我点击这个 api 时,我无法得到回应。 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我得到 404 并且我在控制台上得到的错误是No mapping found for HTTP request with URI [/WEB-INF/views/index.jsp] in DispatcherServlet with name 'dispatcherServle

How can i fix this ?我怎样才能解决这个问题 ?

for people struggling with issues like this - the Spring Boot documentation states here that JSPs are not supported when running in embeddable servlet container - ie when using JAR packaging.对于在此类问题上苦苦挣扎的人 - Spring Boot 文档在此处声明,在可嵌入的 servlet 容器中运行时不支持 JSP - 即使用 JAR 打包时。

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使用 WAR 打包时,此处的答案中给出的建议应该可行 - 但请检查在最终 WAR 中是否在根目录下有一个目录:WEB-INF/views,以及您的 JSP

the view path is wrong /WEB-INF/view/ instead /view/视图路径错误/WEB-INF/view/而不是/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或者您可以使用位于/resources/的 application.properties

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重要: jsp 视图应该在/resources/目录中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM