html - Spring Boot 与 AngularJS html5Mode

标签 html angularjs angular spring spring-boot

我使用 Spring Boot 启动我的 Web 应用程序。它使用一个简单的主类来启动一个嵌入式 tomcat 服务器:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

我想以他可以处理将被激活的angularjs html5mode的方式配置服务器

$locationProvider.html5Mode(true);

其他用户的相关帖子显示需要重定向到root。 html5 模式从 url 中删除 hashbag。如果您刷新页面,服务器不会找到该页面,因为他不处理哈希。见:AngularJS - Why when changing url address $routeProvider doesn't seem to work and I get a 404 error

最佳答案

使用此 Controller 将 URI 转发到 index.html 以保留 AngularJS 路由。来源 https://spring.io/blog/2015/05/13/modularizing-the-client-angular-js-and-spring-security-part-vii

@Controller
public class ForwardController {

    @RequestMapping(value = "/**/{[path:[^\\.]*}")
    public String redirect() {
        // Forward to home page so that route is preserved.
        return "forward:/";
    }
} 

在此解决方案中,ForwardController 仅转发路径,这些路径未在任何其他 ControllerRestController 中定义。这意味着如果您已经拥有:

@RestController
public class OffersController {

    @RequestMapping(value = "api/offers")
    public Page<OfferDTO> getOffers(@RequestParam("page") int page) {
        return offerService.findPaginated(page, 10);
    }
} 

两个 Controller 都将正常工作 - @RequestMapping(value = "api/offers")@RequestMapping(value = "/**/{[path:[ ^\\.]*}")

关于html - Spring Boot 与 AngularJS html5Mode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24837715/

相关文章:

html - 并排对齐两个div?

html - 导航栏不起作用

html - CSS:在 img 和列表上垂直对齐

javascript - AngularJS - 来自 ng-repeater 和函数的链接数据

angular - Rxjs 可观察 : Get HTTP data only for the first subscriber and cached data for the rest

angular - MatDialogRef .afterClosed Observable

html - 编辑 : How do you make a container fill the width of bootstrap col class?

javascript - 使用 Web API Get() 正确格式化 JSON

javascript - 自定义指令链接中的 element.replaceWith 仅在第一次调用时有效

java - 将字符串转换为 Angular 中的 byte[] 并将其另存为 pdf