在vue-cli中路由模式是“hash”的話網頁地址會加個“#”不美觀,所以通常用的都是“history”模式,但是tomcat中不支持此模式,如果輸入網頁地址的話會顯示404,所以我們進行如下配置:在你的項目目錄下新建一個WEB-INF文件夾(我的項目是直接在webapps下的ROOT文件下新建)。
然後再在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_3_1.xsd"
version="3.1">
<!--定義了WEB應用的名字-->
<display-name>
byyearth
</display-name>
<!--聲明WEB應用的描述信息-->
<description>web</description>
<!--報錯文件的設置-->
<error-page>
<error-code>404</error-code>
<location>/</location>
</error-page>
</web-app>
然後就是在tomcat中conf文件夾下server.xml進行域名配置
其中端口8088是網站的端口號,如果想在瀏覽器中不輸入端口而直接訪問的話就設置爲80端口(默認訪問端口)。
<Connector port="8088" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
找到如下標籤進行域名配置,在defalutHost屬性中填入你的域名。
<Engine name="Catalina" defaultHost="www.byy.cn">
在name屬性中填入你的域名
<Host name="www.byy.cn" appBase="webapps"
unpackWARs="true" autoDeploy="true">
在Host標籤內添加,docBase是網頁實際存放位置的根目錄,path是項目在目錄下的位置。
<Context doBase="" path="/" reloadable="true"/>
在打開C:\Windows\System32\drivers\etc下的hosts文件,添加本機地址和域名如127.0.0.1 www.byy.cn
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 www.byy.cn