打开 idea,File -> New -> Project
这里可以选择一个 archetype,即框架模板。这里我们直接点击下一步:
输入 GroupId 与 AtrifactId,这是 Maven 项目的必填项,接着输入项目名称,最后点击【完成】,这样项目就新建好了。
File->Settings->Compiler->Java Compiler
这里选择 jdk8:
File->Settings -> Editor -> File Encodings
这部分是重点!因为新建好的项目,只有经过配置,才能正常地开展开发工作。
File → Project Structure → Project
指定项目的 SDK 、编译语言水平以及编译结果的输出路径:
File → Project Structure → Modules
点击或新建某个模块,在此配置编译水平、源代码目录、资源目录、测试目录、测试资源目录以及需要被排除的构建目录。
**注意:**Sources 目录必须指定为源代码的上一级目录(比如这里的 java),否则会出现某些不可预知的错误。
新建一个完整的 webapp 文件夹,内部包含 WEB-INF 子文件夹以及 web.xml 配置文件:
因为我们新建的是 webapp 项目啊O(∩_∩)O~
让 idea 关联上 web.xml 文件:file -> Project strcuture-> Facets ->+ -> Web
注意:这里的 Path 与 Web Resource Directory 都需要指定到我们刚刚新建的 webapp 文件夹!
File -> Project Settings -> Modules -> + -> Spring
然后选择spring 配置文件(如果把 spring 的配置文件拷贝到项目中,它会自动被 idea 检测到哦 O(∩_∩)O~):
File -> Project Settings -> Atrifacts -> + -> Web Application:Exploded
选择当前项目后,就可以啦。
点击 idea 主界面中间的那个【向下箭头】图标,先打开应用服务器配置页面,然后点击 【+】 -> Tomcat Server -> Local
最后给这个应用服务器取一个名字,并指定需要运行的应用容器:
注意:如果安装了多个 JDK,那么需要在 Use alternative JRE 中指定需要实用的 JRE!
接着为它关联上我们刚才新建的文件包(Deployment):
这里的 Application context 指的是应用启动后的 Web 项目根目录!
<properties>
...
properties>
<dependencies>
...
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>2.3.1version>
<configuration>
<source>1.7source>
<target>1.7target>
<encoding>utf8encoding>
configuration>
plugin>
plugins>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*include>
includes>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>system.propertiesinclude>
includes>
<filtering>truefiltering>
resource>
resources>
build>
<profiles>
<profile>
<id>devid>
<activation>
<activeByDefault>trueactiveByDefault>
activation>
<build>
<filters>
<filter>profile-development.propertiesfilter>
filters>
build>
profile>
<profile>
<id>testid>
<activation>
<activeByDefault>falseactiveByDefault>
activation>
<build>
<filters>
<filter>profile-test.propertiesfilter>
filters>
build>
profile>
profiles>
如果会自动弹出浏览器窗口,就说明启动成功!如果没有,可以重新配置一次应用服务器,然后再试一试!