EnableWebMvc 抛出 ServletException:无法解析具有名称的视图
EnableWebMvc throws ServletException: Could not resolve view with name
玩 Spring Boot + MVC 静态 HTML 页面,同时注意到这个东西:
首先,我有:
索引控制器:
@Controller
public class IndexController {
@RequestMapping("/")
public String index() {
return "index.html";
}
@RequestMapping("/{path:[^\.]+}/**")
public String forward() {
return "forward:/";
}
}
Html 文件是:...\src\main\resources\static\index.html
所以当我的主要应用程序 class 是:
@SpringBootApplication
public class MyApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
一切正常,在默认路径中:localhost:8080\
我得到 index.html
页面内容
但是如果我用 @EnableWebMvc
注释应用程序 class
@SpringBootApplication
@EnableWebMvc
public class MyApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
我得到异常:javax.servlet.ServletException: Could not resolve view with name 'index.html' in servlet with name 'dispatcherServlet'
但根据 this spring doc 这是一个有效的配置。
也许有人能解释一下为什么?我是不是理解错了?
The auto-configuration adds the following features on top of Spring’s defaults:
- Static
index.html
support.
...
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 @Configuration
class of type
WebMvcConfigurerAdapter
, but without @EnableWebMvc
. If you wish to
provide custom instances of RequestMappingHandlerMapping
,
RequestMappingHandlerAdapter
or ExceptionHandlerExceptionResolver
you
can declare a WebMvcRegistrationsAdapter
instance providing such
components.
因此,通过添加 @EnableWebMvc
,您只需禁用 spring-boot 自动配置即可。即静态index.html
支持。
实际上我认为当您选择使用 spring 引导时,您应该使用 spring 引导的默认配置。这意味着您只需编辑文件 application.properties。现在如果你使用 spring mvc,你必须提供你自己的 servlet。所以我认为混淆 to 不是一个好主意。要么使用 spring Boot with no much config to do 要么使用 spring mvc 并进行所有必要的配置。
根据 Spring Boot MVC 结构,您应该在 templates
文件夹中找到 html 文件。然后 Spring Boot
可见
src\main\resources\templates\index.html
玩 Spring Boot + MVC 静态 HTML 页面,同时注意到这个东西:
首先,我有:
索引控制器:
@Controller
public class IndexController {
@RequestMapping("/")
public String index() {
return "index.html";
}
@RequestMapping("/{path:[^\.]+}/**")
public String forward() {
return "forward:/";
}
}
Html 文件是:...\src\main\resources\static\index.html
所以当我的主要应用程序 class 是:
@SpringBootApplication
public class MyApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
一切正常,在默认路径中:localhost:8080\
我得到 index.html
页面内容
但是如果我用 @EnableWebMvc
@SpringBootApplication
@EnableWebMvc
public class MyApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
我得到异常:javax.servlet.ServletException: Could not resolve view with name 'index.html' in servlet with name 'dispatcherServlet'
但根据 this spring doc 这是一个有效的配置。
也许有人能解释一下为什么?我是不是理解错了?
The auto-configuration adds the following features on top of Spring’s defaults:
- Static
index.html
support....
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
@Configuration
class of typeWebMvcConfigurerAdapter
, but without@EnableWebMvc
. If you wish to provide custom instances ofRequestMappingHandlerMapping
,RequestMappingHandlerAdapter
orExceptionHandlerExceptionResolver
you can declare aWebMvcRegistrationsAdapter
instance providing such components.
因此,通过添加 @EnableWebMvc
,您只需禁用 spring-boot 自动配置即可。即静态index.html
支持。
实际上我认为当您选择使用 spring 引导时,您应该使用 spring 引导的默认配置。这意味着您只需编辑文件 application.properties。现在如果你使用 spring mvc,你必须提供你自己的 servlet。所以我认为混淆 to 不是一个好主意。要么使用 spring Boot with no much config to do 要么使用 spring mvc 并进行所有必要的配置。
根据 Spring Boot MVC 结构,您应该在 templates
文件夹中找到 html 文件。然后 Spring Boot
src\main\resources\templates\index.html