当前位置:   article > 正文

springboot项目集成angular服务的配置运用_springboot显示angular页面

springboot显示angular页面

1.环境安装:maven,jdk1.8,nodejs,npm,angular-cli

2.新建springboot项目,新建angular服务项目

3.springboot项目的pom.xml配置:

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <!-- Frontend插件版本,用于集成angular服务 -->
  4. <plugin.frontend.version>0.0.27</plugin.frontend.version>
  5. <!-- node插件版本,用于集成angular服务 -->
  6. <system.node.version>v8.11.3</system.node.version>
  7. <!-- npm插件版本,用于集成angular服务 -->
  8. <system.npm.version>5.6.0</system.npm.version>
  9. </properties>
  10. <!-- springboot 静态资源访问 -->
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  15. </dependency>
  16. </dependencies>
  17. <build>
  18. <finalName>xxx</finalName>
  19. <sourceDirectory>src/main/java</sourceDirectory>
  20. <resources>
  21. <resource>
  22. <directory>src/main/resources</directory>
  23. </resource>
  24. </resources>
  25. <testResources>
  26. <testResource>
  27. <directory>src/test/resources</directory>
  28. </testResource>
  29. </testResources>
  30. <plugins>
  31. <!-- springboot maven插件 项目以maven方式进行打包jar/war -->
  32. <plugin>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-maven-plugin</artifactId>
  35. <executions>
  36. <execution>
  37. <goals>
  38. <!-- 默认打包参数,在mvn package之后,再次打包可执行的jar/war -->
  39. <goal>repackage</goal>
  40. </goals>
  41. </execution>
  42. </executions>
  43. </plugin>
  44. <!-- 配置Frontend插件,支持前后端结合 -->
  45. <plugin>
  46. <groupId>com.github.eirslett</groupId>
  47. <artifactId>frontend-maven-plugin</artifactId>
  48. <version>${plugin.frontend.version}</version>
  49. <configuration>
  50. <!-- angular 源码根目录 -->
  51. <workingDirectory>src/main/angular</workingDirectory>
  52. <!-- node 国内镜像下载地址 -->
  53. <nodeDownloadRoot>https://npm.taobao.org/mirrors/node/</nodeDownloadRoot>
  54. <!-- node 版本 -->
  55. <nodeVersion>${system.node.version}</nodeVersion>
  56. <!-- npm 版本 -->
  57. <npmVersion>${system.npm.version}</npmVersion>
  58. <!-- 代码安装目录 -->
  59. <installDirectory>target</installDirectory>
  60. </configuration>
  61. <executions>
  62. <!-- 安装 node 和 npm -->
  63. <execution>
  64. <id>install node and npm</id>
  65. <goals>
  66. <goal>install-node-and-npm</goal>
  67. </goals>
  68. <phase>generate-resources</phase>
  69. </execution>
  70. <!-- 安装angular项目依赖 -->
  71. <execution>
  72. <id>npm install</id>
  73. <goals>
  74. <goal>npm</goal>
  75. </goals>
  76. <configuration>
  77. <arguments>install</arguments>
  78. <installDirectory>target</installDirectory>
  79. </configuration>
  80. </execution>
  81. <!-- 编译angular源码 -->
  82. <execution>
  83. <id>angular cli build</id>
  84. <goals>
  85. <goal>npm</goal>
  86. </goals>
  87. <phase>generate-resources</phase>
  88. <configuration>
  89. <arguments>run build</arguments>
  90. </configuration>
  91. </execution>
  92. </executions>
  93. </plugin>
  94. </plugins>
  95. </build>

4.springboot项目写个类加载默认访问页面及静态资源加载路径:

  1. @Configuration
  2. @EnableWebMvc
  3. public class EAIDefultView extends WebMvcConfigurerAdapter {
  4. private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
  5. "classpath:/META-INF/resources/", "classpath:/resources/",
  6. "classpath:/static/", "classpath:/public/" };
  7. /**
  8. * 默认根路径访问页面
  9. */
  10. @Override
  11. public void addViewControllers(ViewControllerRegistry registry) {
  12. registry.addViewController("/").setViewName("redirect:/index.html");
  13. registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
  14. super.addViewControllers(registry);
  15. }
  16. /**
  17. * 静态资源路径
  18. */
  19. @Override
  20. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  21. if (!registry.hasMappingForPattern("/**")) {
  22. registry.addResourceHandler("/**").addResourceLocations(
  23. CLASSPATH_RESOURCE_LOCATIONS);
  24. }
  25. }
  26. }

5.将angular项目拷贝至src/main目录下面

6.配置angular项目下的angular.json的输出目录至resources/static

7.配置angular项目下的package.json的bulid命令加上base href为 ./ ,避免出现html文件引用相对路径错误

8.springboot项目下运行clean package可以进行打包

9.springboot项目下运行spring-boot:run可以访问angular项目的index.html页面

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/588470
推荐阅读
相关标签
  

闽ICP备14008679号