IDEA社区版使用Maven + Tomcat开发Web项目

本文介绍如何在IntelliJ IDEA社区版中利用Maven搭建Web项目,并配置Tomcat进行开发与调试。通过详细步骤指导,包括创建Maven项目、配置Tomcat插件、添加依赖及设置目录结构等内容。
部署运行你感兴趣的模型镜像
  由于IDEA的社区版没有集成Web application的原因,加之学生党消费不起专业版,所以通过Google加上自己的使用技巧,成功的使用IDEA社区办进行Web项目的开发和调试

步骤:
1、创建一个Project:

  • 在Welcome to IntelliIDEA 中选择: Create new Project

这里写图片描述

2、在New Project窗体中:

  1. 选择Maven,勾选Create from archetype
  2. 在archetype中选择:org.apache.maven.archetype:maven-archetype-webapp
    这里写图片描述

3、点击下一步(next):

  • 填写:
    • GroupId : testGroupID
    • ArtifactId : TestArtifactId
    • 这里写图片描述

4、Next :

  • 在Project中添加一个参数 archetypeCatalog=internal 若不加该参数,在maven生成骨架的时候将会非常慢甚至卡住
    这里写图片描述

5、Next:

  • project Name : 项目名称(TestProjectName)
  • 这里写图片描述

6、新建完Maven项目后,开始配置Maven的Tomcat插件

  • 在pom.xml中加入Tomcat7的插件

     <!-- pom.xml -->
     <build>
         <plugins>
             <plugin>
                 <groupId>org.apache.tomcat.maven</groupId>
                 <artifactId>tomcat7-maven-plugin</artifactId>
                 <version>2.1</version>
                 <configuration>
                     <port>8080</port>
                     <path>/</path>
                     <uriEncoding>UTF-8</uriEncoding>
                     <server>tomcat7</server>
                 </configuration>
             </plugin>
         </plugins>
     </build>
  • 点击菜单栏中的Run,选择Edit Configurations

  • 在Run/Debug Configuartion中 : 点击左上角的加号,选择Maven,填写(name:tomcat7 Command Line : tomcat7:run )
  • 这里写图片描述

7、点击右上角的绿色三角型箭头,等待tomcat启动,可以在下方看到tomcat的信息(如果是第一次启动,需要等待从Maven中下载插件)

8、当显示如下时,表示启动成功:
这里写图片描述

9、此时的目录结构:
这里写图片描述

  • 这是Maven的默认目录结构,其他没有创建的目录在需要时可自行创建
  • Maven的通用目录结构时:

     ```
     ${basedir}
     |--pom.xml
     |--src
     |     |-- main
     |     |          `-- java
     |     |          `-- resource
     |     |          `-- filters
     |     |
     |     |-- test 
     |     |         `-- java
     |     |         `-- resource
     |     |         `-- filters
     |     |
     |     |-- it
     |     |-- assembly
     |     |-- site
     |-- LICENSE.txt
     |-- NOTICE.txt
     |-- README.txt
     ```
    
  • src/main/java : 项目源代码所在的目录,该位置中的代码在编译后会出现在WEB-INF/classes中
  • src/main/resources : 项目的资源文件所在的目录
  • src/main/filters : 项目的资源过滤文件所在的目录
  • src/main/webapp : 该目录时Web应用源代码所在的目录,如 html文件、web.xml文件
  • src/test/java : 测试代码所在的目录
  • src/test/resources : 测试相关的资源文件所在的目录
  • src/test/filter : 测试相关的资源过滤文件所在目录
  • src/it : 集成测试代码所在的目录
  • src/assembly : 组件(Assembly)描述符所在目录
  • src/site : 站点文件
  • LICENSE.txt : 项目许可证
  • NOTICE.txt : 该项目依赖的库文件的注意事项
  • README.txt : 项目的readme文件

9、添加servlet-api.jar,jsp-api.jar的依赖

  • 若不添加servlet-api, jsp_api依赖,则在编译Web项目时将会出现”javax.servlet.http”不存在等错误
  • 在pom.xml的<dependencies> 元素下添加:

    ....
    <dependency>
     <groupId>javax.servlet</groupId>
         <artifactId>servlet-api</artifactId>
         <version>2.5</version>
         <scope>provided</scope>
     </dependency>
     <dependency>
         <groupId>javax.servlet.jsp</groupId>
         <artifactId>jsp-api</artifactId>
         <version>2.1</version>
         <scope>provided</scope>
     <dependency>
     ...

10、至此,已经可以在IDEA社区版中编写Web项目了

  1. 在src/main中创建java目录,并把java目录设置为Make as sources
  2. 创建一个servlet类:com.test.Hello, 覆盖service()方法,并写上System.out.println(“test”);
  3. 在web.xml中注册servlet
  4. 在右边的maven中,点击clean、compile、package后,点击右上角的三角形启动tomcat
  5. 在浏览器中打开servlet的网址,若配置成功则会在控制台看到”test”输出

11、参考的博文:

  1. IDEA Community(社区版) 使用Maven创建Web工程 并部署tomcat
  2. Maven学习-目录结构
  3. eclipse之程序包javax.servlet.http不存在

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

### IntelliJ IDEA Community Edition Maven Configuration and Resources For configuring Maven within the IntelliJ IDEA Community Edition, several key points are essential to ensure a smooth setup process. The configuration involves setting up project structure as well as ensuring that the correct plugins or configurations are applied. When using IntelliJ IDEA Community Edition with Maven projects, one can configure the environment by adding artifacts through `File->Project Structure->Artifacts->Add->Jar->From modules with dependencies`[^1]. This method allows users to package applications into executable JAR files directly from the IDE without needing external scripts or complex build commands. Additionally, when dealing specifically with Maven integration inside IntelliJ IDEA Community Edition, it is important to note how versions of Jetbrains products including this IDE follow a specific format denoted as 【type:version:buildVersion】, which helps in identifying compatible plugin versions for use alongside your development tools[^2]. To further enhance productivity while working on Maven-based projects: - Ensure that the Maven home path configured under `Settings/Preferences | Build, Execution, Deployment | Build Tools | Maven` matches either an embedded version provided by JetBrains or a custom installation. - Utilize appropriate lifecycle mappings such as those offered by maven-shade-plugin or maven-assembly-plugin depending upon packaging requirements. ```xml <build> <plugins> <!-- Example Plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals><goal>shade</goal></goals> </execution> </executions> </plugin> </plugins> </build> ``` This approach ensures seamless interaction between IntelliJ IDEA's interface elements like artifact creation wizards and underlying build processes driven by Apache Maven. --related questions-- 1. How does one set up continuous integration pipelines for Java applications developed using IntelliJ IDEA? 2. What best practices should be followed during large-scale enterprise application deployments involving multiple microservices built with Spring Boot? 3. Can you provide guidance on optimizing performance settings within IntelliJ IDEA for handling very large codebases efficiently?
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值