Springboot部署war项目文件到Tomcat静态文件(css、js和图片等)加载不出来的解决方法

1,Springboot打包war文件,配置文件(application.properties)设置

-配置启动端口号
server.port=8066
-配置项目访问路径(如果不需要项目访问路径,请注释掉,我这里是注释掉的)
#server.servlet.context-path=/elec

2,配置war网页HTML(js请求地址)访问地址

//我这里是全局地址
//项目访问总入口
var appLocalHostUrl="http://localhost:8066/abc";

2,网页HTML静态文件访问地址

   <link rel="stylesheet" href="/static/css/login.css" media="all">
   <script src="/static/js/login.js"  charset="utf-8"></script>

3,把war文件放进tomcat目录webapps(tomcat>webapps)

Springboot部署war项目文件到Tomcat静态文件(css、js和图片等)加载不出来的解决方法_第1张图片

4,修改tomcat配置文件(server.xml)

Springboot部署war项目文件到Tomcat静态文件(css、js和图片等)加载不出来的解决方法_第2张图片

5,在配置文件(server.xml)Host 标签添加如下:

	<Context path="/static/" docBase="/elec/WEB-INF/classes/static/"></Context>

说明:
(1)/static/ :项目静态网页文件地址
Springboot部署war项目文件到Tomcat静态文件(css、js和图片等)加载不出来的解决方法_第3张图片

(2)/elec/WEB-INF/classes/static/:项目实际war包解决后,静态文件地址

Springboot部署war项目文件到Tomcat静态文件(css、js和图片等)加载不出来的解决方法_第4张图片
(3)完整版server.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.
-->
<!-- Note

你可能感兴趣的:(运维技术,spring,spring,boot)