Start by creating a Maven project, and then complete the following configuration in turn:
Add the following basic configuration to Pom.xml and use MAVEN to load the jar we need:
1<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"2xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" >3<modelVersion>4.0.0</modelVersion>4<groupId>com.imooc</groupId>5<artifactId>springmvc</artifactId>6<packaging>war</packaging>7<version>0.0.1-SNAPSHOT</version>8<NAME>SPRINGMVC Maven webapp</name>9<url>http://maven.apache.org</url>Ten One<properties> A<!--declares the version number of each dependent package and does not need to be declared when the dependency is described below. -<commons-lang.version>2.6</commons-lang.version> -<slf4j.version>1.7.6</slf4j.version> the<spring.version>4.1.3.RELEASE</spring.version> -</properties> - -<!--dependency management to manage dependencies between jars-- +<dependencyManagement> -<dependencies> +<dependency> A<groupId>org.springframework</groupId> at<artifactId>spring-framework-bom</artifactId> -<version>${spring.version}</version> -<type>pom</type> -<scope>Import</scope> -</dependency> -</dependencies> in</dependencyManagement> - to<dependencies> +<dependency> -<groupId>org.springframework</groupId> the<artifactId>spring-webmvc</artifactId> *<!--Since the version number is already declared above, there is no need to declare-- $</dependency>Panax Notoginseng -<dependency> the<groupId>commons-lang</groupId> +<artifactId>commons-lang</artifactId> A<!--here to directly refer to the version number stated above-- the<version>${commons-lang.version}</version> +</dependency> - $<!--about the log--- $<dependency> -<groupId>org.slf4j</groupId> -<artifactId>slf4j-log4j12</artifactId> the<version>${slf4j.version}</version> -<exclusions>Wuyi<exclusion> the<artifactId>slf4j-api</artifactId> -<groupId>org.slf4j</groupId> Wu</exclusion> -</exclusions> About</dependency> $ -<dependency> -<groupId>javax.servlet</groupId> -<artifactId>javax.servlet-api</artifactId> A<version>3.0.1</version> +</dependency> the -</dependencies> $ the<build> the<!--plugins-- the<plugins> the<plugin> -<groupId>org.eclipse.jetty</groupId> in<artifactId>jetty-maven-plugin</artifactId> the<version>9.2.2.v20140723</version> the<!--<groupId>org.apache.tomcat.maven</groupId> About<artifactId>tomcat7-maven-plugin</artifactId> the<version>2.2</version> the<configuration> the<url>http://localhost:8080/webapp/index.jsp</url> +</configuration> -</plugin> the</plugins>Bayi</build> the</project>View Code
Add the following basic configuration to Web. xml:
1<?xml version= "1.0" encoding= "UTF-8"?>2<web-app version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4Xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee5http//java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">6 7<display-name>spring MVC study</display-name>8<!--Spring application context, understanding hierarchical ApplicationContext--9<context-param>Ten<param-name>contextConfigLocation</param-name> One<!--spring's own configuration file-- A<param-value>/WEB-INF/configs/spring/applicationContext*.xml</param-value> -</context-param> - the<listener> -<listener-class> - Org.springframework.web.context.ContextLoaderListener -</listener-class> +</listener> - +<!--use the following servlet and servlet-mapping tags to configure the front-end controller to the web, A<servlet> at<servlet-name>mvc-dispatcher</servlet-name> -<servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class> -<!--dispatcherservlet corresponding context configuration, default is/web-inf/$servlet-name$-Servlet.xml -If there is no Init-param, the default configuration file above is loaded by default-- -<init-param> -<param-name>contextConfigLocation</param-name> in<!--Create the following XML file in the/web-inf/corresponding directory-- -<param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value> to</init-param> +<load-on-startup>1</load-on-startup> -</servlet> the<servlet-mapping> *<!--the servlet-name here is consistent with the servlet-name in the servlet tag above-- $<servlet-name>mvc-dispatcher</servlet-name>Panax Notoginseng<!--Mvc-dispatcher Intercept all requests-- -<!--here Url-pattern Specifies the basis for different URL sources-- the<url-pattern>/</url-pattern> +</servlet-mapping> A</web-app>
View Code
Create the Mvc-dispatcher-servlet.xml and add the following basic configuration:
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context"4Xmlns:mvc= "Http://www.springframework.org/schema/mvc"5xsi:schemalocation="6http//Www.springframework.org/schema/beans7http//www.springframework.org/schema/beans/spring-beans.xsd8http//Www.springframework.org/schema/context9http//www.springframework.org/schema/context/spring-context.xsdTenhttp//Www.springframework.org/schema/mvc Onehttp//www.springframework.org/schema/mvc/spring-mvc.xsd "> A -<!--Activate @required @Autowired @PostConstruct @PreDestroy @Resource and other annotations, -Enables annotations to be used in classes to instantiate objects-- the<context:annotation-config/> - -<!--dispatcherservlet context, search only for @controller labeled classes, do not search for classes of other labels-- -<context:component-scan base- Package= "Com.imooc.mvcdemo" > +<context:include-filter type= "Annotation" -expression= "Org.springframework.stereotype.Controller"/> +</context:component-scan> A at<!--let Dispatcherservlet enable annotation-based handlermapping- -<mvc:annotation-driven/> - - -<!--Configure Viewresolver to tell the servlet which viewresolver to get the view, which is used here jstlview-- -<Bean in class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > -<property name= "Viewclass" toValue= "Org.springframework.web.servlet.view.JstlView"/> +<!--configure its prefix prefix, which means that the JSP file is configured with a suffix suffix in the/web-inf/jsps/directory, indicating that its file type is. JSP-- -<property name= "prefix" value= "/web-inf/jsps/"/> the<property name= "suffix" value= ". jsp"/> *</bean> $ Panax Notoginseng -</beans>
View Code
Create the Applicationcontext.xml and add the following basic configuration:
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context"4Xmlns:mvc= "Http://www.springframework.org/schema/mvc"5xsi:schemalocation="6http//Www.springframework.org/schema/beans7http//www.springframework.org/schema/beans/spring-beans.xsd8http//Www.springframework.org/schema/context9http//www.springframework.org/schema/context/spring-context.xsdTenhttp//Www.springframework.org/schema/mvc Onehttp//www.springframework.org/schema/mvc/spring-mvc.xsd "> A -<context:annotation-config/> - the<!--There is no need to manage controllers-- -<context:component-scan base- Package= "Com.imooc.mvcdemo" > -<context:exclude-filter type= "Annotation" -expression= "Org.springframework.stereotype.Controller"/> +</context:component-scan> -</beans>
View Code
At this point, the configuration file is loaded and the front controller is added:
1 PackageCom.imooc.mvcdemo.controller;2 3 ImportOrg.springframework.stereotype.Controller;4 Importorg.springframework.web.bind.annotation.RequestMapping;5 6 @Controller7@RequestMapping ("/hello")8 Public classHellomvccontroller {9 Ten@RequestMapping ("/mvc") One //This can be accessed by the browser after @requestmapping configuration Localhost:8080/hello/mvc A PublicString Hellomvc () { - //back to home.jsp - return"Home"; the } -}View Code
When all is done, run Jetty:run and enter the URL in the browser to get the results.
Graphic:
Webapplicationcontext can have multiple, which is what our context, listener, loads as a contextual information, which provides us with some of the components and services that all of our applications use publicly, namely service and DAO layers, These services should be shared by the entire application, and should not be confined to a dispatcherservlet context.
A context that is related to a particular dispatcherservlet can also have multiple dispatcherservlet. These different dispatcherservlet can be used to distribute different requests.
Create the first SPRINGMVC with Maven