Spring MVC is a web member of Spring family. It is a lightweight Web framework based on Java that implements the request-driven type of Web MVC design idea, that is, it uses the idea of MVC architecture pattern to decouple the responsibilities of The Web layer. Request-based refers to the use of a request-response model. The purpose of the framework is to help simplify development, and Spring MVC is to simplify our daily Web development.
Spring MVC is an implementation of the service-to-worker idea. The front-end controller is DispatcherServlet; The application controller is divided into Handler Mapping for processor management and View Resolver for View management. Support Locale parsing and file uploading. Provides very flexible data validation, formatting, and data binding mechanisms; Provides powerful convention over configuration (convention over convention) programming support by contract.
Maven webApp SpringMVC environment JAR package depends on configuration web. XML (front-end controller configuration) servlet-context. XML configuration page controller writing add view page Build Eclipse + JDK1.7 + Maven + Jetty
Create Maven webApp to establish springmvc01 project and adjust the web environment.
Springmvc environment JAR package dependencies
The < 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/maven-v4_0_0.xsd" > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. XXX < / groupId > < artifactId > springmvc01 < / artifactId > <packaging> War </packaging> <version>0.0.1-SNAPSHOT</version> <name> Springmvc01 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> The < version > 4.12 < / version > < scope > test < / scope > < / dependency > <! -- spring web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> < version > 4.3.2. RELEASE < / version > < / dependency > <! -- spring mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> < version > 4.3.2. RELEASE < / version > < / dependency > <! -- web servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> The < version > 3.0.1 < / version > < / dependency > < / dependencies > <! <build> <finalName> Springmvc01 </finalName> <resources> <resource> <directory>src/main/resources</directory> </resource> </resources> <plugins> <! Plugins </groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> < Configuration > <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> The < version > 6.1.25 < / version > < configuration > < scanIntervalSeconds > 10 < / scanIntervalSeconds > <contextPath>/springmvc01</contextPath> </configuration> </plugin> </plugins> </build> </project>Copy the code
Configure web.xml (Front-end controller configuration)
<? The XML version = "1.0" encoding = "utf-8"? > < web - app id = "WebApp_ID" version = "3.0" XMLNS = "http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <! <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:*.xml</param-value> </context-param> <! -- Enable Spring container environment context listening --> <listener> <listener class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <! Utf-8 --> <filter> <description>char encoding filter</description> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <! -- Servlet request dispatcher --> <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:servlet-context.xml</param-value> </init-param> <! <load-on-startup>1</load-on-startup> </ Servlet > <servlet-mapping> <servlet-name>springMvc</servlet-name> <! Do request --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>Copy the code
To launch our springMvc environment, we have not configured the MVC framework yet. The servlet-context.xml file is referenced above in web.xml.
The servlet - context. The XML configuration < 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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <! <context:component-scan base-package="com.xxx.controller" /> <! MVC request mapping handler and adapter configuration --> < MVC :annotation-driven/> <! <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="contentType" value="text/html" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>Copy the code
If garble is returned: Configure the message converter
<! < MVC :message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value="text/html; Charset =UTF-8"/> </bean> </ MVC :message-converters> </ MVC :message-converters> @requestMapping ("/hello") public ModelAndView hello(){ModelAndView mv=new ModelAndView(); mv.addObject("hello", "hello spring mvc"); mv.setViewName("hello"); return mv; Create a new JSP folder under WEB-INF. JSP <% @page language=" Java "import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; % > <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < HTML > <head> <base href="<%=basePath%>"> <title>My JSP 'hello.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <! ${hello} </body> </ HTML >Copy the code
To start the Jetty server, right-click the project → Run as → Maven build → Goals and enter Jetty :run.
If it launches successfully, the browser (preferably the powerful Chrome or Firefox, programmers’ favorites!!) To access the address http://localhost:8080/springmvc01/hello
The final effect is as follows:
The SpringMVC environment is now set up.
What does extending Spring MVC help us do to make it very easy to design a clean Web layer; Developing a cleaner Web layer; Naturally integrated with the Spring framework (such as IoC container, AOP, etc.); Provides powerful convention over configuration programming by contract support; Simple Web layer unit test; Support flexible URL to page controller mapping; It is very easy to integrate with other view technologies, such as Velocity, FreeMarker, and so on, because the Model data is not placed in a specific API, but in a Model (Map data structure implementation, therefore easy to use by other frameworks); Very flexible data validation, formatting, and data binding mechanisms that can be used with any object for data binding without having to implement frame-specific apis; Support flexible localization and other parsing; Simpler exception handling; Support for static resources; Restful style is supported.