Springmvc introduction: Springmvc is also called Spring Web mvc, which belongs to the framework of the presentation layer . Spring MVC is part of the spring framework and is released after Spring3.0
Spring structure diagram
SPRINGMVC Request Flowchart
SPRINGMVC Request Flowchart Language:
request-------->dispatcherservler (central scheduler/front controller)-----> handlermapping (processor mapper)------> Returns an execution chain-----> transfers the execution chain to the Handleradaptor (processor adapter)-----> Dispatch handler (processor)-----> modelandview-----> Give the result to Handleradaptor first and then handleradaptor to dispatcherservler---------> Dispatcherservler Dispatch Viewresolver (view resolver)----> hand over to dispatcherservler-------> look for View---->view respond ( Response)
Springmvc's first introductory case
The user submits a request, and the Server-side processor, after receiving the request, gives a welcome message that is displayed on the Page.
First Step: import the jar package
Add 2 jar packages based on the original Springjar package
Spring-webmvc-4.2.0.release.jar effect: The implementation of spring MVC
Spring-context-support-4.2.0.release.jar Role: contains classes that support UI templates, mail services, cache caches, and more
To analyze the configuration steps:
1. Configuring the front-end controller in Web. XML
2. Processor Mapper (handlermapping)
3. Processor Adapter (handleradaptor)
4. Processor
5. View Resolver (viewresolver)
Registering the Central Scheduler
On Urlpattern's statement:
Preferably paired with *.do.
Cannot match/* cannot access dynamic resources such as *.jsp
It's best not to be paired/unable to access static resources
Have to match/restful programming rest
question:/* 404 problems with the match method
Explanation: ⅰ: cannot be configured as/*
Dispatcherservlet will request to a dynamic page, that is, a jump request to a JSP page as a normal controller Request.
The central scheduler calls the processor mapper to find the appropriate processor for it. Of course I can't find it. JSP page will report 404 error conclusion:/* will intercept dynamic resources
Ⅱ: scenario one: using the Tomcat default servlet solution
Have to match/explain: middle: Red callout third in configuration
Scenario Two: using Mvc's Default-servlet-handler
The access request to the static resource is mapped through handlermapping to the default servlet request processor Defaultservletrequesthandler Object. The processor calls Tomcat's Defaultservlet to handle access requests for static Resources. of course, you need to introduce MVC constraints
Method Three: use Mvc's resource to solve
After Spring3.0.4, Spring defines the processor Resourcehttprequesthandler that is specifically designed to handle requests for static Resources. <mvc:resources/> tags are added to resolve static resource unreachable Issues.
Location: directory of static resources Mapping: request for resources Note: requires TOMCAT7 support
<?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/javaeehttp//java.sun.com/xml/ns/javaee/web-app_2_5.xsd "><display-name></display-name> <!--1. Configuring the Central scheduler (front controller)-<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <!--3.1 Specifying profiles applicationcontext.xml-<init-param> <param-name>contextconfiglocation</par Am-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <!--3When The. 2Tomcat starts, The service object is already in memory and the number is greater than or equal to 1, 0, or negative, as with no settings.-<load-on-startup>1</load-on-startup> </servlet> <!--the first, You can view pictures--<servlet-mapping> <servlet-na Me>springmvc</servlet-name> <url-pattern>*. do</url-pattern> </servlet-mapping> <!--the Second configuration and <MVC in Applicationcontext.xml:default-servlet-handler/> with <!--<servlet-mapping> <SERVLET-NAME>SPRINGMVC</SERVLET-NAME&G T <url-pattern>/</url-pattern> </servlet-mapping> <!--the third type of configuration<servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-patt ern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*. jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping > <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping > <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping > <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping>-<welcome-fil E-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
Configuring the Central Scheduler Code
Custom Processors:
package Cn.happy.controller;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.servlet.modelandview;import org.springframework.web.servlet.mvc.Controller;//how can a class be a controller? public classYcontroller Implements controller{//Processor//HandleRequest Processing Requests//modelandview return value type publicmodelandview handlerequest (httpservletrequest request, httpservletresponse Response) throws Exception { Modelandview MV=NewModelandview (); Mv.addobject ("msg","a day Off tomorrow ."); //work together//mv.setviewname ("web-inf/jsp/index.jsp");Mv.setviewname ("Index"); returnmv; }}
Custom Processors
JSP page Setup:
Import= "java.util.*" pageencoding= "utf-8"%><%== Request.getscheme () + "://" + Request.getservername () + ":" +request.getserverport () +path+ "/"; %><! DOCTYPE HTML public "-//w3c//dtd HTML 4.01 transitional//en" > First SPRINGMVC program <!--page How to get to ${msg} </body>
index.jsp PageIn large Configuration: register processor and view resolver
Visit page:
Profile development--beannameurlhandlermapping
Profile development--simpleurlhandlermapping
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/schema/beans"XMLNS:AOP= "http://www.springframework.org/schema/aop"Xmlns:tx= "http://www.springframework.org/schema/tx"Xmlns:context= "http://www.springframework.org/schema/context"Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc= "http://www.springframework.org/schema/mvc"xsi:schemalocation="http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp//Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--configuring Processor Mapper--<!--<beanclass= "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" > </bean>-<!--register Processor Mapping Cumshot-<beanclass= "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > <property name= "mappings" > <props> <prop key= "/hello.do" >firstController</prop> <prop ke y= "/say.do" >firstController</prop> </props> </property> < /bean> <!--register Processing Mapper--<!--<beanclass= "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > <property name= "urlmap" > <map> <entry key= "/hello.do" > <value>firstcontroller</value> ; </entry> <entry key= "/say.do" > <value>firstController</value> </entry> </map> </property> </bean>-- <bean id= "firstcontroller"class= "cn.happy.controller.YController" ></bean> <!--process/request path not reachable by static resources--<!-- <mvc:default-servlet-handler/>-<!--scenario four: TOmcat7 support-<!--<mvc:resources location= "/images/" m apping= "/images/**" ></mvc:resources> <!--register processor Request Address-<!--the configuration needs and controller Mv.setviewname Full Path--<bean id= "/hello.do"class= "cn.happy.controller.YController" ></bean> <!--view resolver Viewresolver-<beanclass= "org.springframework.web.servlet.view.InternalResourceViewResolver" > <!--prefix jstl struts2-<prop Erty name= "prefix" value= "/web-inf/jsp/" ></property> <!--suffix--<property name= "suffix" value= " . JSP "></property> </bean> </beans>
applicationcontext.xml Large configuration file Springmvc Getting Started case and request Flowchart (for preliminary configuration of processor or view resolver or processor mapper, etc.)