在 Java 应用中集成

本文讲解如何在 Java 服务中集成前端资源,我们以 Spring Boot 为例。

resources

新建 /velocity/layout/index.vm

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta http-equiv="x-ua-compatible" content="ie=edge,chrome=1" />
  6. <meta name="viewport" content="width=device-width" />
  7. <title>ICE Design Lite</title>
  8. <link rel="shortcut icon" href="$!publicPath/favicon.png" />
  9. <link href="$!publicPath/css/index.css" rel="stylesheet" />
  10. </head>
  11. <body>
  12. <div id="ice-container"></div>
  13. <script type="text/javascript" src="$!publicPath/js/index.js"></script>
  14. </body>
  15. </html>

Controller

  1. @GetMapping("/")
  2. @VelocityLayout("/velocity/layout/index.vm")
  3. public String index(Model model) {
  4. model.addAttribute(publicPath, jsConfig.get(publicPath));
  5. return "index";
  6. }

示例展示的是 HashRouter 路由,如果为 BrowserRouter,建议改为 @GetMapping(value = { "/**" }) 实现前端 fallback