java EnableWebMvc 抛出 ServletException:无法解析具有名称的视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41543549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:57:46  来源:igfitidea点击:

EnableWebMvc throws ServletException: Could not resolve view with name

javaspring-mvcspring-bootstatic-html

提问by Bublik

Playing around with Spring Boot + MVC with static HTML pages, while noticed this thing:

用静态 HTML 页面玩 Spring Boot + MVC,同时注意到这个事情:

Firstly, what I have:

首先,我有什么:

Index controller:

索引控制器:

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index() {
        return "index.html";
    }

    @RequestMapping("/{path:[^\.]+}/**")
    public String forward() {
        return "forward:/";
    }
}

The Html file is:...\src\main\resources\static\index.html

Html 文件是:...\src\main\resources\static\index.html

So when my main application class is:

所以当我的主要应用程序类是:

@SpringBootApplication
public class MyApplication extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

Everything works well and in default path: localhost:8080\I get index.htmlpage content

一切正常并且在默认路径下:localhost:8080\我得到index.html页面内容

But if I annotate Application class with @EnableWebMvc

但是如果我用 @EnableWebMvc

@SpringBootApplication
@EnableWebMvc
public class MyApplication extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

I get exception: javax.servlet.ServletException: Could not resolve view with name 'index.html' in servlet with name 'dispatcherServlet'But according to this spring docit is a valid configuration.

我得到了例外:javax.servlet.ServletException: Could not resolve view with name 'index.html' in servlet with name 'dispatcherServlet'但是根据这个 spring 文档,它是一个有效的配置。

Maybe someone can explain me why? Do I understand something wrong?

也许有人可以解释我为什么?我理解错了吗?

回答by Ruslan Stelmachenko

According to spring-boot's docs

根据spring-boot 的文档

The auto-configuration adds the following features on top of Spring's defaults:

  • Static index.htmlsupport.

...

If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Configurationclass of type WebMvcConfigurerAdapter, but without@EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapteror ExceptionHandlerExceptionResolveryou can declare a WebMvcRegistrationsAdapterinstance providing such components.

自动配置在 Spring 的默认值之上添加了以下功能:

  • 静态index.html支持。

...

如果您想保留 Spring Boot MVC 功能,并且只想添加额外的 MVC 配置(拦截器、格式化程序、视图控制器等),您可以添加自己@Configuration的 type 类 WebMvcConfigurerAdapter,但不添加@EnableWebMvc. 如果您希望提供 的自定义实例RequestMappingHandlerMappingRequestMappingHandlerAdapter或者ExceptionHandlerExceptionResolver您可以声明一个WebMvcRegistrationsAdapter提供此类组件的实例。

So by adding @EnableWebMvcyou just disable what spring-boot autoconfiguring for you. Namely static index.htmlsupport.

因此,通过添加@EnableWebMvc您只需禁用 spring-boot 自动配置。即静态index.html支撑。

回答by vvs

Actually I think when you choose to use spring boot you should use the default config of spring Boot. It means you just have to edit the file application.properties. Now if you use spring mvc, you have to provide your own servlet. So I think mixing up the to is not a good idea. Either you use spring Boot wiht no much config to do or you use spring mvc and you make all the necessary config.

其实我觉得当你选择使用 spring boot 时,你应该使用 spring Boot 的默认配置。这意味着您只需要编辑文件application.properties。现在,如果您使用 spring mvc,则必须提供自己的 servlet。所以我认为混淆 to 不是一个好主意。要么使用不需要太多配置的 spring Boot,要么使用 spring mvc 并进行所有必要的配置。

回答by cralfaro

According to Spring Boot MVC structure, you should locate your html file in the templatesfolder. Then will be visible for Spring Boot

根据 Spring Boot MVC 结构,您应该在templates文件夹中找到您的 html 文件。然后对 Spring Boot 可见

src\main\resources\templates\index.html