本次我們使用idea構建springmvc項目
本案例的代碼github地址:
1.首先我們可以創建maven項目,file-> new ->project->maven,現則createa form archetype,這樣可以生成maven的一些構建插件
2.寫項目名,finish即可
3.項目結構整體如下
4.改造成我們常用的maven結構,在main下新建java包和resources包,把java包標記成根路徑,resources標記成資源根路徑包
4.導入springmvc的pom,整體pom如下
<?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.zy</groupId> <artifactId>springmvc-demo2</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>springmvc-demo2 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.1.6.RELEASE</spring.version> </properties> <dependencies> <!--spring 核心包--> <!-- spring start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- spring end --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--日志--> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.8.0-alpha0</version> <scope>test</scope> </dependency> <!--j2ee相關包 servlet、jsp、jstl--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> </dependencies> <build> <finalName>springmvc-demo2</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.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> </plugins> </pluginManagement> </build> </project>
4.1這里我們只導入了 spring-mvc的依賴,他會自動導入spring的相關包,打開maven導圖你可以看到springmvc會自動依賴spring相關的jar,也就是說導入了mvc的包,他會自動把spring相關的包也導入進來,這就是maven的依賴管理的好處
5.在web.xml中寫DispatcherServlet,這是springmvc的servlet,指定了springmvc的配置文件地址在classpath:spring-mvc.xml,spring容器使用ContextLoaderListener制定了spring配置文件在classpath:applicationContext.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>
<!--spring的配置文件地址--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath: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>
<!--springmvc的配置文件地址--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param>
<!--啟動時間,跟服務器一起啟動--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name>
<!--攔截所有請求--> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
6.springmvc得配置文件
<?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"> <!--此文件負責整個mvc中的配置--> <!-- 配置注解驅動 可以將request參數與綁定到controller參數上 --> <mvc:annotation-driven/> <!--靜態資源映射--> <!--本項目把靜態資源放在了webapp的statics目錄下,資源映射如下--> <!--<mvc:resources mapping="/css/**" location="/static/css/"/>--> <!--<mvc:resources mapping="/js/**" location="/static/js/"/>--> <!--<mvc:resources mapping="/image/**" location="/static/images/"/>--> <mvc:default-servlet-handler /> <!--這句要加上,要不然可能會訪問不到靜態資源,具體作用自行百度--> <!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前后綴(如果最后一個還是表示文件夾,則最后的斜杠不要漏了) 使用JSP--> <!-- 默認的視圖解析器 在上邊的解析錯誤時使用 (默認使用html)- --> <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/><!--設置JSP文件的目錄位置--> <property name="suffix" value=".jsp"/> </bean> <!-- 自動掃描裝配,掃描controller包的注解,加入到容器中 --> <context:component-scan base-package="com.zy.controller"/> </beans>
7.application配置文件
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.zy"> <!--application父容器不掃描controller注解,這樣兩個配置文件各自各負責各自的--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
8.根據springmvc.xml配置文件,在WEB-INF下新建views包和index.jsp
9.新建com.zy.controller包,並寫一個controller類進行
10.配置tomcat服務器
11.啟動tomcat服務器,http://localhost:8080/hello,這是測試jsp頁面的,進行測試
12.在瀏覽器輸入剛才controller的請求地址,看結果
12.順利完成springmcx基於xml配置的構建。
相關代碼github地址: