准备工作:
IDEA 14编辑器、Maven项目管理器、Tomcat容器
一、新建maven工程
(1) File->New->Project->Maven->Create from archetype(maven-archetype-webapp) 点击"Next"
(2) 输入GroupId、ArtifactId、Version 点击"Next"
(3) 选择Maven版本、配置文件、仓库地址 点击"Next"
(4) 输入Project name 点击"Finish"
注:第2步输入的GroupId、ArtifactId会体现在pom.xml中,由GroupId和ArtifactId来确定唯一的项目。第4步输入的Project name会体现在顶级文件夹名(项目名)上。
二、配置Tomcat
(1) Run->Edit Configurations->"+"->Tomcat Server(Local)
(2) 输入Name
(3) Server选项卡保留默认值
(4) Deployment选项卡->"+"->"Artifact…"->"ArtifactId:war exploded"->OK
注:如果第4步没有"Artifact…"选择,则直接保存现有的配置,然后右键"pom.xml"->Maven->Reimport重新引入下maven文件,再编辑Tomcat,Deployment选项卡就出现了"Artifact…"
三、项目结构
最后文件夹结构如下图所示:
IDEA 14编辑器、Maven项目管理器、Tomcat容器
一、新建maven工程
(1) File->New->Project->Maven->Create from archetype(maven-archetype-webapp) 点击"Next"
(2) 输入GroupId、ArtifactId、Version 点击"Next"
(3) 选择Maven版本、配置文件、仓库地址 点击"Next"
(4) 输入Project name 点击"Finish"
注:第2步输入的GroupId、ArtifactId会体现在pom.xml中,由GroupId和ArtifactId来确定唯一的项目。第4步输入的Project name会体现在顶级文件夹名(项目名)上。
二、配置Tomcat
(1) Run->Edit Configurations->"+"->Tomcat Server(Local)
(2) 输入Name
(3) Server选项卡保留默认值
(4) Deployment选项卡->"+"->"Artifact…"->"ArtifactId:war exploded"->OK
注:如果第4步没有"Artifact…"选择,则直接保存现有的配置,然后右键"pom.xml"->Maven->Reimport重新引入下maven文件,再编辑Tomcat,Deployment选项卡就出现了"Artifact…"
三、项目结构
最后文件夹结构如下图所示:
其中,".idea"文件夹是IDEA编辑器自带的文件夹,可忽略。
四、配置spring库maven
在pom.xml里加入:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.1.2.RELEASE</spring.version>
<mysql.verison>5.1.6</mysql.verison>
<c3p0.verison>0.9.1.2</c3p0.verison>
<ibatis.verison>2.3.0</ibatis.verison>
<log4j.verison>1.2.16</log4j.verison>
<servlet.version>2.4</servlet.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
</dependencies>
五、配置web.xml
在webapp/WEB-INF/web.xml下加入:
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-core.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
六、配置spring
在resources文件夹下建立一个名为spring-core.xml的文件,文件名和地址是由web.xml里<init-param><param-name>为contextConfigLocation的<param-value>决定的。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
default-autowire="byName">
</beans>