idea解决tomcat插件启动项目问题
前言:今天在idea中使用maven项目构建工具搭建了一个web项目,在pom.xml中导入了tomcat插件,启动项目测试时发现控制台显示build success,但浏览器访问不了。奇怪的很。没有掌握tomcat7插件的正确打开方式。现在从头开始记录这次问题,先从搭建环境开始。
一、创建一个maven项目
二、添加webapp文件夹
注意:直接新建一个webapp文件夹,文件夹上没有那个蓝色的小圆点,解决方式如下
三、导入web.xml文件
<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/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"></web-app>
四、pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>Demo</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><dependencies><!--servlet--><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope> <!--在编译期间使用,在运行期间不使用--></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>javax.servlet.jsp-api</artifactId><version>2.3.3</version><scope>provided</scope></dependency></dependencies><build><!--maven插件--><plugins><!--jdk编译插件--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target><encoding>utf-8</encoding></configuration></plugin><!--tomcat插件--><plugin><groupId>org.apache.tomcat.maven</groupId><!-- tomcat7的插件, 不同tomcat版本这个也不一样 --><artifactId>tomcat7-maven-plugin</artifactId><version>2.1</version><configuration><!-- 通过maven tomcat7:run运行项目时,访问项目的端口号 --><port>8080</port><!-- 项目访问路径 本例:localhost:9090, 如果配置的aa, 则访问路径为localhost:9090/aa--><path>/</path></configuration></plugin></plugins></build></project>
在运行项目的时候, 出现了两个小问题。
使用插件启动控制台显示build success但浏览器无法访问,但是使用本地tomcat启动项目,又没有啥问题。
原因:没有配置项目打包的方式
2.使用插件启动webapp文件放置的位置
必须放在src/main文件夹下,放在其他地方找不到。这是使用插件时,需要注意的地方
3.使用插件启动项目的方式
方式一:
方式二:
