美文网首页程序员
React 打包 部署在 Tomcat 上

React 打包 部署在 Tomcat 上

作者: 猫爪 | 来源:发表于2019-10-18 11:46 被阅读0次

简单既要

1. 采用 webpack 打包
2. 项目使用了 import {BrowserRouter, Route, Switch} from 'react-router-dom'; 

采用 npm run build

 "scripts": {
    "start": "webpack-dev-server --mode development",
    "build": "webpack --mode production",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

打包生成 在 dist 目录下,直接拖拽到 tomcat 的路径下,如图

屏幕快照 2019-09-05 下午2.37.37.png

WEB-INF 是自己加的,主要防止 404
web.xml 配置一下 很重要

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">

  <error-page>
    <error-code>401</error-code>
    <location>/WEB-INF/jsp/401.jsp</location>
  </error-page>
  <error-page>
    <error-code>403</error-code>
    <location>/WEB-INF/jsp/403.jsp</location>
  </error-page>
<!-- 重要 -->
  <error-page>
    <error-code>404</error-code>
    <location>/index.html</location>
  </error-page>

</web-app>

接下来 代码中的路由配置要加上 文件路径,如下

<Switch>
        <Route path="/dist/buy" exact component={xxxxView}/>
        <Route path="/dist/home"  component={xxxxxxView}/> 
        <Route path="/dist/test"  render={()=>(<div>hello is me.</div>)}/>    
</Switch>

然后 : http://localhost:8080/dist/test 就可以访问了

相关文章

网友评论

    本文标题:React 打包 部署在 Tomcat 上

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