【问题标题】:Angular 4 with spring boot routing URL with out using hashcode带有 Spring Boot 路由 URL 的 Angular 4,不使用哈希码
【发布时间】:2017-08-04 07:25:56
【问题描述】:

以下是我的场景:

  1. 我正在使用带有 Angular 4 的 Spring Boot
  2. 我正在使用 angular-cli 生成构建文件并将其放置在资源 -- 静态文件夹下。
  3. 在运行我的 pom.xml 时,所有静态文件夹都在复制目标 --> 项目文件夹 --> WEB-INF --> 类 --> 静态文件夹(这是预期的)

塞纳里奥:

我的网址是

https://localhost:9443/consumer https://localhost:9443/consumer/preapproval https://localhost:9443/consumer/preapproval/申请人 https://localhost:9443/consumer/preapproval/客户

每次点击整个/整个 URL 都不会改变。如果我使用以下配置:

`@配置 公共类 WebConfig 扩展 WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
}

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("forward:/index.html");
}

@Bean
public InternalResourceViewResolver defaultViewResolver() {
    return new InternalResourceViewResolver();
}

}`

在 Spring Boot 中,当我刷新/输入除 (https://localhost:9443/consumer) 以外的任何 URL 时,它会引发 404 错误。任何人都可以建议我最好的解决方案。

注意:在我的业务中,我们不应该在 Angular 4 中使用哈希码。

提前致谢

【问题讨论】:

  • we shouldn't use hashcode in Angular 4. 是什么问题,能解释清楚吗
  • 使用localhost:9443/consumer/#——我们可以使用它,但出于某种原因管理层建议我们不要使用'#'。
  • 这就是我尝试从服务器端(如 Spring boot)或 Angular 4 中的任何其他解决方案而不使用“#”来寻找其他解决方案的原因。希望你可能清楚
  • 这必须由服务器处理,事实上,由于浏览器中的实际地址正在更新(并且没有# / hashbang方法),因此出现404错误是正常的。默认情况下,HTML5 历史记录用于在 Angular2 中重用。如果您不想出现 404 错误,则需要更新您的服务器,以便为您定义的每个路由路径提供 index.html 文件。
  • 要解决此问题,您可以查看link 将文件部署到 gh-pages 时会发生同样的情况,并且有解决方法

标签: spring angular spring-boot


【解决方案1】:

我在我的项目中设置了如下所示的 getMapping。

@GetMapping("/{path:[^\\.]*}")
String redirect() {
    return "forward:/";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 2018-09-14
    • 2022-08-20
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 2016-06-24
    相关资源
    最近更新 更多