Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为静态页面提供服务_Java_Spring_Spring Mvc - Fatal编程技术网

Java 如何为静态页面提供服务

Java 如何为静态页面提供服务,java,spring,spring-mvc,Java,Spring,Spring Mvc,我需要从SpringWeb中的“/”GET请求提供静态index.html页面 我的模块包含在更大的模块中,打包为WAR并部署到tomcat 我试过了 @Configuration @EnableWebMvc public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) {

我需要从SpringWeb中的“/”GET请求提供静态index.html页面

我的模块包含在更大的模块中,打包为WAR并部署到tomcat

我试过了

@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        System.out.println("WebMvcConfigurer - addResourceHandlers() function get loaded...");
        registry.addResourceHandler("/WEB-INF/classes/index.html")
                .addResourceLocations("/WEB-INF/classes/index.html");
    }
}

并将index.html放在resources文件夹中。仍然是404。有人能帮我理解我做错了什么吗?

ResourceHandler的模式应该反映查询路径,在您的例子中是
/index.html

jar/war
/WEB-INF/classes/
中的资源可以通过
classpath:
访问,在您的例子中是
classpath:/index.html

所以你的配置应该是这样的

@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/index.html")
                .addResourceLocations("classpath:/index.html");
    }
}
如果你有更多的资源,你可以简化

registry.addResourceHandler("/*.html")
        .addResourceLocations("classpath:/");

为我工作:
registry.addResourceHandler(“/static/index.html”).addResourceLocations(“classpath:/”)