6

静的 HTML ページで Spring Boot + MVC をいじっていると、次のことに気付きました。

まず、私が持っているもの:

インデックス コントローラ:

@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\を取得します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' しかし、この春のドキュメントによると、それは有効な構成です。

多分誰かが私に理由を説明できますか?私は何か間違っていることを理解していますか?

4

3 に答える 3

0

実際、Spring Boot を使用する場合は、Spring Boot のデフォルト設定を使用する必要があると思います。これは、ファイルapplication.propertiesを編集するだけでよいことを意味します。spring mvc を使用する場合は、独自のサーブレットを提供する必要があります。ですから、to を混同するのは得策ではないと思います。あまり設定を行わずにSpring Bootを使用するか、Spring MVCを使用して必要なすべての設定を行います。

于 2017-01-09T08:47:30.057 に答える
0

Spring Boot MVC 構造に従って、html ファイルをtemplatesフォルダーに配置する必要があります。その後、Spring Boot で表示されます

src\main\resources\templates\index.html
于 2017-01-09T09:58:53.913 に答える