美文网首页前端
Webpack前端整合到SpringMVC项目中

Webpack前端整合到SpringMVC项目中

作者: 宇love雪 | 来源:发表于2019-01-02 10:04 被阅读0次

Webpack前端整合到SpringMVC项目中

背景

容器引擎系统不支持直接部署Nodejs环境,需要结合maven插件在jenkins构建阶段编译Webpack项目。这里我把前端代码整合到SpringMVC项目中,作为一个整体工程进行打包部署。

SpringMVC是已有项目,需要保留旧版本。这次重构前端,并在后端添加单独API模块。

目录结构

|-- src
    |-- frontend (Webpack项目)
    |-- java
    |-- resources
    |-- webapp
|-- pom.xml

如上所示,frontend是Webpack项目,可以单独在Webstorm进行开发运行。如果要在SpringMVC工程中编译Webpack并部署到Tomcat,需要对pom.xml作少许改动,添加相应的maven插件。

编译方式

主要用到maven-war-pluginfrontend-maven-plugin两个插件。

frontend-maven-plugin插件

下载nodejs到指定目录,执行Webpack编译命令。

maven-war-plugin插件

将Webpack编译后的文件放到target指定目录。

<plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <id>install node and npm</id>
            <goals>
                <goal>install-node-and-npm</goal>
            </goals>
        </execution>
        <execution>
            <id>npm install</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <configuration>
                <arguments>install</arguments>
            </configuration>
        </execution>
        <execution>
            <id>npm run build</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <configuration>
                <arguments>run build</arguments>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <!--node版本-->
        <nodeVersion>v10.14.2</nodeVersion>
        <!--node安装路径-->
        <installDirectory>target</installDirectory>
        <!--前端代码路径-->
        <workingDirectory>src/main/frontend</workingDirectory>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <warName>[war包名称]</warName>
        <webResources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/webapp</directory>
            </resource>
            <resource>
                <filtering>true</filtering>
                <!--Webpack编译目标路径-->
                <directory>src/main/frontend/dist</directory>
                <!--拷贝到target指定目录-->
                <targetPath>WEB-INF/web</targetPath>
            </resource>
        </webResources>
    </configuration>
</plugin>

配置文件

sitemesh配置

SpringMVC使用sitemesh+JSP,无法直接访问新增的html及api,需要修改sitemesh配置文件

我的sitemesh配置文件路径为src/main/webapp/WEB-INF/decorators.xml

<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/WEB-INF/views">
   <decorator name="default" page="layouts/default.jsp" />
    <excludes>
        <!--符合下列模式的url不进行sitemesh装饰-->
        <pattern>*.html</pattern>
        <pattern>/api/*</pattern>
    </excludes>
</decorators>

spring配置

访问静态资源时,根据配置的mapping到指定的location下寻找资源文件。

不配置静态资源,访问时会报404 NOT FOUND错误

<!-- 静态资源映射 -->
<!-- Webpack编译后的静态资源目录 -->
<mvc:resources mapping="/static/**" location="/static/,/WEB-INF/web/static/" />
<mvc:resources mapping="/**/*.html" location="/WEB-INF/web/" />
<mvc:resources mapping="*.html" location="/WEB-INF/web/" />

相关文章

  • Webpack前端整合到SpringMVC项目中

    Webpack前端整合到SpringMVC项目中 背景 容器引擎系统不支持直接部署Nodejs环境,需要结合mav...

  • SpringMVC 参数绑定 后端json接收参数

    SpringMVC旧的写法 在SpringMVC项目中,通常是用controller层来接受前端传过来的数据,但是...

  • Webpack原理-入门篇

    引言 现在的前端工程,webpack处于一个很重要的地位。正好公司的前端同学想深入了解一下webpack,所以就整...

  • Snowpack 下使用 AntV/L7 require方法未

    在Web前端的 React 项目中使用 Snowpack 作为编译工具(Webpack 的替代工具),并在项目中引...

  • Webpack初体验

    Webpack是什么? webpack是一个前端资源加载/打包工具,其主要功能为:使用loaders将项目中依赖到...

  • webpack 调试模式(sourcemap) chrome无法

    目前项目中采用的webpack 作为前端构建方式。使用es6+webpack+react的时候发现一些问题 1.在...

  • webpack loader

    说明 webpack默认只能处理JavaScript和JSON文件,但前端项目中,文件类型多种多样.比如.vue、...

  • #ReactApp项目构建流程【3】

    帮助程序员简化流程,专注业务代码的webpack配置 3.1项目中很有用的webpack常用配置 在传统html开...

  • 前端模块化方案学习

    我在目前的项目中遇到了一个匪夷所思的问题:CommonJS规范是同步加载模块资源,为什么前端项目中webpack使...

  • webpack中如何mock数据

    背景: 在react+webpack项目中前端如何mock数据,以模拟整体项目运行流程,不用等待后端人员提供接口。...

网友评论

    本文标题:Webpack前端整合到SpringMVC项目中

    本文链接:https://www.haomeiwen.com/subject/jeialqtx.html