相关疑难解决方法(0)

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

使用静态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 …

java spring-mvc static-html spring-boot

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×1

spring-boot ×1

spring-mvc ×1

static-html ×1