一、IDEA配置maven

当导入maven项目的时候,首先需要按照如图进行maven的配置:指定使用的maven包、指定本地仓库和maven的settings.xml文件。
然后在IDEA右侧栏,点击maven图标,在滑出的maven的界面选择Reimport All maven project下载jar包

二、使用maven创建JavaWeb项目

1.创建javaWeb项目

  • 打开IDEA,点击File —>>>New Project—>>弹出new Project界面
  • 点击Next,在New Module界面设置
  • 在此页面可以看到配置的maven信息、创建项目的信息
  • 设置项目的名称和路径信息
  • 点击Finish,就可以创建好项目,如果创建的项目的目录结构是不符合标准的maven结构,我们需要进行设置。

    • 对于java目录,是需要设置Source Root目录。设置之后,maven编译的时候可以转为classes文件。可以右键点击Mark Directory as进行设置。
    • 我们此时创建的项目的webapp,鼠标点击右键,不能创建jsp、html文件,显然这是有问题的。我们需要进行如下设置:点击File—>>>Project Structure,在弹出的页面进行设置:

    • 通过上述的设置,我们发现
      1.webapp的目录颜色改变了;2.webapp目录下新建Jsp、html文件了

2.pom文件

<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"><!-- pom文件的版本 --><modelVersion>4.0.0</modelVersion><!-- 企业域名倒序 --><groupId>com.oneHeart</groupId><!-- 项目名称 --><artifactId>maven_web2</artifactId><!-- 版本 snapshot快照版,在项目每次启动时会自动检查更新 release:1.1 --><version>1.0-SNAPSHOT</version><!-- 打包格式 jar:java project的打包,.jar文件 war:web project打包,打包为.war文件,可以直接在应用服务器中执行
pom:父级项目的打包格式 --><packaging>war</packaging><!-- 名称 --><name>maven_web2 Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><!--定义常用的依赖的版本project.build.sourceEncoding:代替UTF-8
--><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><!-- 依赖管理 一般出现在父级项目中,他用来规范继承关系的项目中使用的依赖的版本信息, 如果父项目中dependencyManagement,子项目中是不需要指定版本信息的 --><!-- <dependencyManagement> dependencies如果定义在dependencyManagement中 规范父级别依赖
</dependencyManagement> --><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><!--scope:范围 用于指定依赖的生命周期的 compile:编译、测试、运行有效,默认使用compile provided:编译、测试有效,运行无效(servlet依赖)
test:编译,测试有效,运行无效(junit,spring-test) runtime:测试、运行有效 system:与本机的系统变量相关,一致性差(不建议使用),编译测试有效。
import:从其他 pom 文件导入时有效 --><scope>test</scope></dependency></dependencies><build><!--項目最終部署的名稱--><finalName>maven_web2</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><!--配置插件:maven生命周期与一键构建使用的插件对应:clean、compile、test、package、install、deploy--><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin><!-- 配置JDK --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8.0_201</source><target>1.8.0_201</target></configuration></plugin><!-- tomcat插件,maven默认集成的tomcat6,tomcat6与JDK8搭配使用的时候会有版本冲突 --><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><configuration><port>9991</port></configuration></plugin></plugins></pluginManagement></build>
</project>

3.创建Servlet

@WebServlet("/helloServlet")
public class HelloServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("HelloServlet......");req.getRequestDispatcher("/hello.jsp").forward(req,resp);}
}

4.创建hello.jsp

在webapp目录下创建hello.jsp

5.运行Tomcat,访问Servlet


6.访问Servlet

浏览器输入:http:localhost:8080/maven_web2/helloServlet,则会跳转到hello.jsp页

7.遇到的问题

<1>webapp目录下无法创建jsp、html文件

解决措施:参照 3.创建Servlet进行将webapp设置成web

<2> Class com.oneHeart.Servlets.HelloServlet is not a Servlet

具体描述:javax.servlet.ServletException: Class com.oneHeart.Servlets.HelloServlet is not a Servlet。编译的时候不出错,运行项目的时候出现的错误,创建的Servlet被提示 不是Servlet。
原因:maven集成了tomcat,我们可以查看本地tomcat 的依赖包,发现包含:servlet-api、jsp-api,而我们在maven 的pom.xml文件中添加了相同名称 servlet-api、jsp-api 的jar包(创建Servlet的时候需要),
而在项目运行的时候,则我们添加的包会和maven集成的tomcat包相冲突。
解决的办法:我们需要将pom文件中,我们添加的 servlet-api、jsp-api 包只在编译之时生效,而不在运行的时候生效,避免发生jar包冲突。我们可以使用 provided 指定jar包作用范围为编译,只在编译时候生效。

<scope> provided </scope>

<3>.严重: Failed to initialize end point associated with ProtocolHandler [“http-bio-8080”]

java.net.BindException: Address already in use: JVM_Bind :8080
端口被占用异常,原因:之前启动了maven项目,未关闭。而后重新启动,就会出现端口被占用异常。

<4>.org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 1 in the generated java file
The type java.io.ObjectInputStream cannot be resolved. It is indirectly referenced from required .class files

问题描述:使用maven启动了Servlet之后,在浏览器访问对应的Servlet,Servlet可以正常访问,但是跳转页面会出现错误。
原因:maven集成的tomcat默认是tomcat6.0.29(如图所示),而java的jdk使用的是1.8,所以猜测这应该是tomcat与jdk版本不兼容引起的
解决措施:使用高版本的tomcat,在pom.xml文件中进行配置,配置好之后可以从Maven的Plugins执行tomcat7:run的命令。

<build><plugings><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><port>8080</port><path>/</path></configuration></plugin></plugings>
</build>

<5>live Template,IDEA代码模板设置

三、使用mave创建Java项目

1.java项目与javaWeb项目的创建区别:

使用maven创建java项目可以参考创建javaweb项目。

二、IDEA创建Maven项目相关推荐

  1. IDEA创建Maven项目

    一.设置IDEA默认Maven配置  进入IDEA设置界面File -> Settings   在左上角搜索 Maven 在点击Build Tools下的Maven  设置成自己电脑下载Mave ...

  2. IntelliJ IDEA 14 创建maven项目二

    前言: 在我的idea14使用maven创建web工程文章介绍了如何运用idea14创建maven项目--但有瑕疵,如下: 今天在群里交流才得知起因: 原来一直这样创建的--但结果都一样,均出现上面的 ...

  3. Maven安装和配置及eclipse创建Maven项目

    提示:使用Maven需要先安装jdk. 下载安装Maven 一.下载最新版的Maven,下载地址:http://maven.apache.org/download.cgi 二.将Maven下载到E:\ ...

  4. 使用IDEA创建Maven项目和Maven使用入门(配图详解)

    本文详解的讲解了使用IDEA创建Maven项目,及Maven的基础入门. 1.打开IDEA,右上角选择File->New->Project 2.如图中所示选择Maven(可按自己所需添加, ...

  5. 用idea建立jsp项目_用idea创建maven项目,配置tomcat详解

    用idea创建maven项目,配置tomcat详解,电脑上得有jdk1.7,或者1.8,然后就是maven3.x吧,再有就是tomcat7以上 下面就直接开始看图啦: 这个我刚刚开始没注意细看,原来w ...

  6. eclipse创建maven项目的创建

    更多免费教学文章请关注这里 操作一.点击file->Project 操作二.在搜索框中搜索maven 点击Maven Project->Next 操作三.红框处打勾后,点Next 操作四. ...

  7. eclipse 创建maven项目 出现Could not calculate build plan错误解决

    目录 一.问题描述 ​ 二.问题原因 三.具体解决方案(仅供参考): 1.创建本地Maven仓库 下载maven 解压 配置环境变量 ​ 测试是否安装成功 创建本地maven仓库 2.eclipse配 ...

  8. IDEA快速创建maven项目详细步骤

    目录 前言必读: 1.Maven和Maven Archetype区别? 2.创建maven项目之前的步骤(必看) 一.创建maven 1.打开idea--->文件--->新建---> ...

  9. Eclipse创建Maven项目报错处理Could not resolve archetype

    Eclipse创建Maven项目报错处理 1.错误说明 错误一:Could not resolve archetype org.apache.maven.archetypes:maven-archet ...

  10. (一)使用IDEA创建Maven项目和Maven使用入门(配图详解)

    本文详解的讲解了使用IDEA创建Maven项目,及Maven的基础入门. 1.打开IDEA,右上角选择File->New->Project 2.如图中所示选择Maven(可按自己所需添加, ...

最新文章

  1. 物联网的编年史1974-2025 你都知道多少?
  2. 人工智能和自主系统在美军联合职能中的应用
  3. 6.2 IP子网划分
  4. RedHat7.0更新yum源(踩过的坑)
  5. 安卓飞机大战(三) 弹出对话框
  6. Android SQLite 数据库详细介绍
  7. .classpath文件有什么用_干货分享:Windows目录结构剖析,C盘目录常见文件夹都有什么用?...
  8. mysql5.6主从复制(读写分离)方案_MySQL5.6主从复制(读写分离)方案
  9. Python笔记-方差分析之多因素方差分析
  10. misc on starcraft----starcraft2
  11. 论文笔记_SLAM_VINS-Mono: A Robust and Versatile Monocular Visual-Inertial State Estimator
  12. LeetCode之最大连续1的个数
  13. 清华 计算机 学神 李凌,清华大学计算机科学与技术系 2018 年接收推荐免试.PDF...
  14. FP7195大功率零压差全程无频闪调光DC-DC恒流芯片
  15. 工厂模式及什么时候用工厂模式
  16. cisco 2821 路由器的端口映射
  17. QA之道知多少(一) 初出茅庐
  18. MP2451使用注意事项
  19. html歌曲朋友圈,深夜听歌朋友圈的文案
  20. UCOS操作系统——信号量与互斥信号量(九)

热门文章

  1. 县域冠状动脉粥样硬化性心脏病分级诊疗
  2. java异常[java.util.regex.patternsyntaxexception dangling meta character ‘+‘ near index]解决
  3. kali安装java8
  4. SAT词汇背诵需“点面结合”
  5. 【高等代数】线性空间的定义
  6. JAVA正则表达式(详细,转载内容)
  7. ROS学习-tf介绍
  8. 各省2020年初级会计考试政策调整汇总
  9. python协程入门介绍
  10. 如何选择合适的高尔夫球杆?