使用静态HTML页面玩Spring Boot + MVC,同时注意到这件事:
首先,我有:
指数控制器:
@Controller
public class IndexController {
@RequestMapping("/")
public String index() {
return "index.html";
}
@RequestMapping("/{path:[^\\.]+}/**")
public String forward() {
return "forward:/";
}
}
Run Code Online (Sandbox Code Playgroud)
Html文件是:...\src\main\resources\static\index.html
所以当我的主应用程序类是:
@SpringBootApplication
public class MyApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
一切运作良好,默认路径:localhost:8080\我得到index.html页面内容
但是,如果我使用注释Application类 @EnableWebMvc
@SpringBootApplication
@EnableWebMvc
public class MyApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到例外:javax.servlet.ServletException: Could not resolve view with …