筆者基於Itellij Idea IDE環境java
打開Create New Project -> Maven -> Next -> {GroupId、ArtifactId、Version} -> Next -> {Project name} -> Finishgit
建立好的maven工程以下圖所示:github
在maven的約定中,src/main/java存放工程代碼,src/main/resources存放配置文件,src/test/java存放測試代碼,src/test/resources存放測試依賴配置web
如缺失相關目錄可自行建立spring
<?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.demo</groupId> <artifactId>springmvc</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <spring.version>4.2.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </project>
1.新建src/main/webapp/WEB-INF/web.xmlapache
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2.新建src/main/webapp/WEB-INF/dispatcher-servlet.xml(該文件名與web.xml中的servlet-name對應,即{servlet-name}-servlet.xml,用於spring mvc的配置)瀏覽器
<?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"> <!-- 包掃描路徑,實現註解驅動Bean的解析與注入 --> <context:component-scan base-package="com.demo" /> <!-- 註解使用的前置配置 --> <mvc:annotation-driven /> </beans>
3.新建src/main/webapp/WEB-INF/applicationContext.xml(與web.xml中的contextConfigLocation對應,用於spring的配置)spring-mvc
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
建立src/main/java/com.demo.controller.DemoController.java類tomcat
package com.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(path = "demo") public class DemoController { @RequestMapping(path = "index", method = RequestMethod.GET) @ResponseBody public String index() { return "Hello springmvc."; } }
註解說明:mvc
@Controller:類註解,標識這個類是一個控制器類
@RequestMapping:類註解&方法註解,類註解可省略,path屬性表示被標註的方法能夠處理的URI,示例中index()方法會處理/demo/index請求;method屬性標註能夠處理的請求類型,示例中僅處理Get請求,如省略則可處理POST等其它類型請求
@ResponseBody:方法註解,標識會把方法返回直接返回給頁面
第一個簡單的spring mvc示例就寫好了,進行下一步
打開Run...配置,或用快捷鍵control+option+R,如第一次配置,請選擇Edit Configuration...,點擊左上角"+"圖標選擇Maven(如沒有選擇Defaults標籤下Maven後再點擊"+"圖標),填寫Command line(指定maven命令)與Name(下次Run複用)
Run,工程包springmvc-1.0-SNAPSHOT.war就打出來了,以下圖
在Intellij Idea中配置好tomcat,啓動,瀏覽器輸入localhost:8080/demo/index,第一個spring mvc程序就出來了:)