在使用 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
所以当我的主要应用程序类是:
@SpringBootApplication
public class MyApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
一切正常并且在默认路径中:localhost:8080\
I get index.html
页面内容
但是如果我用注释 Application 类@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'
但根据今年春天的医生 http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/config/annotation/EnableWebMvc.html这是一个有效的配置。
也许有人可以解释我为什么?我理解有问题吗?