在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