yunsan-pc/TOMCAT_DEPLOY_README.md

3.3 KiB
Raw Blame History

Tomcat 部署配置说明

问题原因

Vue Router 使用 history 模式,刷新页面时 Tomcat 会尝试查找对应的物理路径,导致 404 错误。

解决方案

方案一:配置 Tomcat rewrite 规则(推荐)

  1. 下载 rewrite.jar

  2. 创建 WEB-INF/rewrite.configwdk/WEB-INF/ 目录下创建 rewrite.config 文件,内容如下:

    # 所有非文件请求都转发到 index.html
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.*$ /wdk/index.html [L]
    
  3. 修改 WEB-INF/web.xmlwdk/WEB-INF/web.xml 中添加 rewrite 过滤器:

    <?xml version="1.0" encoding="UTF-8"?>
    <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_4_0.xsd"
             version="4.0">
    
        <display-name>Yun3v2 Application</display-name>
    
        <!-- Rewrite Filter -->
        <filter>
            <filter-name>rewriteFilter</filter-name>
            <filter-class>org.apache.rewrite.RewriteFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>rewriteFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <!-- Welcome File -->
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
    </web-app>
    

方案二:使用 hash 模式(简单但 URL 不美观)

修改 src/router/index.js

import { createRouter, createWebHashHistory } from 'vue-router'

const router = createRouter({
  history: createWebHashHistory(),  // 使用 hash 模式
  routes
})

这样 URL 会变成:http://localhost:8080/wdk/#/home

方案三:配置 Tomcat 全局 error-page

wdk/WEB-INF/web.xml 中添加:

<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
         version="4.0">
    
    <display-name>Yun3v2 Application</display-name>
    
    <!-- 404 错误页面重定向到 index.html -->
    <error-page>
        <error-code>404</error-code>
        <location>/index.html</location>
    </error-page>
    
    <!-- Welcome File -->
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

部署步骤

  1. 重新构建项目

    npm run build
    
  2. 复制文件到 Tomcat

    dist/ → tomcat/webapps/wdk/
    
  3. 创建 WEB-INF 目录和配置文件

    tomcat/webapps/wdk/WEB-INF/
    ├── web.xml
    └── rewrite.config (如果使用方案一)
    
  4. 重启 Tomcat

推荐方案

  • 生产环境使用方案一rewrite 规则)
  • 快速测试使用方案三error-page
  • 内网简单部署使用方案二hash 模式)