Idea下使用maven搭建SSM(一):SpringMVC
阿新 • • 發佈:2018-12-09
Spring MVC是一款基於MVC架構模式的輕量級Web框架,其目的是將Web開發模組化,對整體架構進行解耦,簡化Web開發流程。
下面我們使用idea和maven搭建一個Spring MVC專案。
1,建立專案:File-New-Project
點選"Next"
選擇maven配置,在idea中默認了配置,選擇好後點擊"Next"
專案命名後點擊"Finish"
2,專案建立好後,idea會下載預設配置檔案等,完成後開啟pom.xml,在配置中加入Spring MVC所需的jar包:
<?xml version="1.0" encoding="UTF-8"?> <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>com.SpringMVC</groupId> <artifactId>SpringMVC</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>SpringMVC 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> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <spring.version>5.0.0.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</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-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <finalName>SpringMVC</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.0.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.7.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.0</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> </plugins> </pluginManagement> </build> </project>
點選Import Changes,下載所需的jar包。
完成後在src/main/webapp下新建資料夾:“Java”,“Resource”。
右鍵點選Java資料夾,Mark Directory as->Sources Root,
右鍵點選Resource資料夾,Mark Directory as -> Resources Root。
3,在src/main/webapp/WEB-INF下建立資料夾"views",在"views"資料夾中新增jsp檔案“Index”:
在Resource資料夾中新建一個xml檔案,命名為“dispatcher-servlet”(前端控制器),
配置dispatcher-servlet.xml:
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--開啟上下文註解--> <context:annotation-config/> <!--啟動註解驅動的springmvc功能--> <mvc:annotation-driven/> <!--啟動包掃面功能--> <context:component-scan base-package="cn.com.controller"/> <!--檢視解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--解析view字首--> <property name="prefix" value="/WEB-INF/views/" /> <!--解析view字尾--> <property name="suffix" value=".jsp" /> </bean> </beans>
配置src/main/webapp/WEB-INF/web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<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:dispatcher-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
在Java資料夾下建立包"cn.com.controller",並在包下建立一個java類"Home"。
編寫Home類:
package cn.com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/Home")
public class Home {
@RequestMapping("/Index")
public String Index(){
return "Index";
}
}
4,配置Tomcat:
大功告成!
執行Tomcat
目錄結構:
CSDN資源下載:https://download.csdn.net/download/wkjiteye/10838104