What is Spring MVC
The Spring MVC Framework is an MVC framework that separates data, business, and presentation well by implementing Model-view-controller patterns. From this perspective, Spring MVC is very similar to structs and Structs2. The design of Spring MVC revolves around Dispatcherservlet, Dispatcherservlet is responsible for distributing requests to specific handler. Requests are processed through configurable hander mappings, view resolution, locale, and theme resolution, and go to the corresponding view. The overall process of Spring MVC request processing is as follows:
Spring MVC has two usages based on the annotation version and the base. XML version, but now the enterprise-level development is basically using the annotation version, no other reason, is convenient. So the code example that follows is based on the annotation version, and you want to know that friends based on the. xml version of spring MVC can query themselves online.
Spring MVC Environment Builds
To get started with a spring MVC environment, the first thing to build a Java Web project, the project name I built, called SPRINGMVC, is to build a spring MVC environment with basic functionality, the jar package that must be introduced is beans, Context, core, expression, Web, Webmvc, and commons-logging.
Then, add some content to Web.xml:
<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" 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 htt P://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <display-name></display-name> <welcome -file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> < !--the listener activates spring--> <listener> <listener-class>org.springframework.web.context.conte When the Web container is started Xtloaderlistener</listener-class> </listener> <!--handle the buffer leaks caused by the use of Javabeans,introspector, it is recommended to configure this supervisor Listener--> <listener> <listener-class>org.springframework.web.util.introspectorcleanuplistener</ Listener-class> </listener> <!--Configure the setting of Springmvcdispatcherservlet and configure The Mapping--> ≪servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework .web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextcon
Figlocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet
-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
The
Two listener are not required, but the servlet is necessary, url-pattern for developers to choose which paths need to be handled by spring MVC. Then under the classpath under our agreed name springmvc-servlet.xml write an XML file:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://www.springframework.org/schema/beans" xmlns:mvc= "Http://www.springframework.org/schema/mvc" Xmlns:cont ext= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmln
s:tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-4.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC
Http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-4.2.xsd "> <context:annotation-conf IG/> <context:component-scan base-package= "Com.xrq.controller"/> <!--configuration View parser--> ; Bean class= "ORG.SPRINGFRAmework.web.servlet.view.InternalResourceViewResolver "> <!--webroot to a specified folder file path--> <prope
Rty name= "prefix" value= "/"/> <!--view name suffix--> <property name= "suffix" value= ". jsp"/> </bean> </beans>
In addition, as spring is used, Tomcat starts by default to find Applicationcontext.xml under Web-inf, so put an empty applicationcontext.xml under Web-inf:
<?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-4.2.xsd ">
</beans>
Write a Java POJO for handling specific requests:
@Controller
@RequestMapping (value = "/test") Public
class TestController
{
@RequestMapping
Public String dispatchtest ()
{
System.out.println ("Enter testcontroller.dispatchtest");
return "Test";
}