Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring boot单页应用程序-将每个请求转发到index.html_Spring_Spring Mvc_Spring Boot - Fatal编程技术网

Spring boot单页应用程序-将每个请求转发到index.html

Spring boot单页应用程序-将每个请求转发到index.html,spring,spring-mvc,spring-boot,Spring,Spring Mvc,Spring Boot,我有一个Spring Boot(v1.3.6)单页应用程序(angular2),我想将所有请求转发到index.html 对的请求正在工作(200,我得到index.html),但没有(404) 赛跑运动员 @SpringBootApplication @ComponentScan({"packagea.packageb"}) @EnableAutoConfiguration public class Runner { public static void main(String[]

我有一个Spring Boot(v1.3.6)单页应用程序(angular2),我想将所有请求转发到
index.html

对的请求正在工作(200,我得到index.html),但没有(404)

赛跑运动员

@SpringBootApplication
@ComponentScan({"packagea.packageb"})
@EnableAutoConfiguration
public class Runner {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext run = SpringApplication.run(Runner.class, args);
    }
}
WebAppConfig.class

@Configuration
@EnableScheduling
@EnableAsync
public class WebAppConfig extends WebMvcConfigurationSupport {

    private static final int CACHE_PERIOD_ONE_YEAR = 31536000;

    private static final int CACHE_PERIOD_NO_CACHE = 0;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.setOrder(-1);
        registry.addResourceHandler("/styles.css").addResourceLocations("/styles.css").setCachePeriod(CACHE_PERIOD_ONE_YEAR);
        registry.addResourceHandler("/app/third-party/**").addResourceLocations("/node_modules/").setCachePeriod(CACHE_PERIOD_ONE_YEAR);
        registry.addResourceHandler("/app/**").addResourceLocations("/app/").setCachePeriod(CACHE_PERIOD_NO_CACHE);
        registry.addResourceHandler("/systemjs.config.js").addResourceLocations("/systemjs.config.js").setCachePeriod(CACHE_PERIOD_NO_CACHE);
        registry.addResourceHandler("/**").addResourceLocations("/index.html").setCachePeriod(CACHE_PERIOD_NO_CACHE);
    }

}

style.css
/app/third-party/xyz/xyz.js
,。。正在工作(200,我得到了正确的文件)。只有
/**
index.html
不起作用。

如果不查看日志,我不完全确定为什么它没有正确映射,但是如果您想将URL映射到视图(html),那么您最好使用spring提供的
视图控制器
机制。e、 g

(摘自上面链接的spring文档-这是您应该如何将url映射到视图,而不是重新定位静态资源的映射。)

我不确定资源映射是否有任何类型的后缀过滤-例如,我不知道spring如何决定将请求映射到
ResourceHttpRequestHandler
-您是否尝试过(只是确认或拒绝)amps之类的东西是否映射到任何东西


也可能由于Spring Boot的默认主页行为,您上面定义的html映射被忽略,index.html正在工作:

您还可以添加一个转发控制器,如:

@Controller
public class ForwardingController {
    @RequestMapping("/{path:[^\\.]+}/**")
    public String forward() {
        return "forward:/";
    }
}

第一部分
{path:[^\\.]+}
匹配除
以外的一个或多个字符。这将确保对
文件.ext的请求不会被此RequestMapping处理。如果您还需要支持转发的子路径,请将
/**
放在
{…}

之外,我也遇到了同样的问题,下面的方法也适用于我。我的html文件位于src/main/resources/static/app中

关键是删除@EnableWebMvc并将“classpath:/static/app/”添加到addResourceLocations!希望这有帮助

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
        "classpath:/META-INF/resources/", "classpath:/resources/",
        "classpath:/static/","classpath:/static/app/", "classpath:/public/" };

@Bean
public WebMvcConfigurer webMvcConfigurer() {
    return new WebMvcConfigurerAdapter() {

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!registry.hasMappingForPattern("/webjars/**")) {
                registry.addResourceHandler("/webjars/**").addResourceLocations(
                        "classpath:/META-INF/resources/webjars/");
            }
            if (!registry.hasMappingForPattern("/**")) {
                registry.addResourceHandler("/**").addResourceLocations(
                        CLASSPATH_RESOURCE_LOCATIONS);
            }
        }


        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            // forward requests to /admin and /user to their index.html
            registry.addViewController("/portal").setViewName(
                    "forward:/app/index.html");
        }
    };
}

这个对我不起作用:

return "forward:/";
感谢您,我找到了一个非常有效的解决方案:

@RequestMapping(value = "/{[path:[^\\.]*}")
public void redirect(HttpServletResponse response) throws IOException {
    response.sendRedirect("/");
}

可能index.html位于受保护的文件夹或其他路径中?@duardito the
index.html
styles.css
位于同一文件夹(
webapp
)中。您是否有与“/home”匹配的端点?@duardito No我没有与“/code>/home”匹配的端点?因此,如果没有与“/home”匹配的资源,我认为这个404错误是正常的,因为这个页面不存在,并且后端中没有端点与“/home”匹配,所以每次使用“/home”向后端发出请求时,都会出现404错误,因为没有匹配的端点。有关{varName:regex}的解释,请参阅表单允许更复杂的基于正则表达式的匹配。由于我将请求转发到“/ui/index.html”,我必须将路径映射调整为“/**/{path:[^\\.]+}”,以避免由于匹配在最后一个组件中具有句点字符的路径而导致堆栈溢出异常。
“/{path:[^\\.]+}/**”
无法处理子目录中的静态文件<代码>“/**/{path:[^\\.]+}”
是有史以来最好的答案!您需要为“forward:/”设置Thymeleaf模板才能工作。
@RequestMapping(value = "/{[path:[^\\.]*}")
public void redirect(HttpServletResponse response) throws IOException {
    response.sendRedirect("/");
}