Maybe the article I wrote before into the SpringMVC world, from SpringMVC entry to the first SpringMVC entry program in the SpringMVC architecture is not so elegant, detailed and delicate, so I write this article a little more elegant, detailed and delicate, as to how elegant, detailed and delicate it is, I think as long as there is a servlet basis should be able to understand (ha ha, seems a bit exaggerated), anyway should probably be suitable for small white (I am not Daniel, I am also small white QnQ)

First, let’s take a look at the conventions of data interaction that we’ve seen up to now.

Normal servlet flow: request – URL -pattern – to the corresponding servlet to process

If you want to use SpringMVC now, instead of a regular servlet, how do you tell your application? – How to get SpringMVC involved in an application:

Springmvc requires a servlet that comes with Springmvc

All requests are intercepted and handed over to SpringMVC with the following configuration:

 <servlet>
  	<servlet-name>springDispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  			<param-name>contextConfigLocation</param-name>
  			<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springDispatcherServlet</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
Copy the code

Take a look at the picture below:

Blocking rule

< url-pattern > .action < /url-pattern >

/* /user: intercepts requests starting with /user. /user/abc.do: intercepts only requests ending with. Action: intercepts only requests ending with.action

The action, for example:

As for init-param content, find the source code… (Don’t talk nonsense directly look at the picture haha)

/WEB-INF/springDispatcherServlet-servlet.xml

Of course, the project is compatible with both the springMVC version of servlets and the regular version of servlets

 <servlet-mapping>
  	<servlet-name>springDispatcherServlet</servlet-name>
  	<url-pattern>.action</url-pattern>
  </servlet-mapping>

  <servlet>
  	<servlet-name>springDispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  			<param-name>contextConfigLocation</param-name>
  			<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
Copy the code

Create a common class with the suffix handler. There are several ways to make a common class unusual: Interfaces/classes, annotations, configuration, while springMVC is in vogue with annotations, so use annotations, pay special attention to @requestMapping as it will become very common!!

Class component scanning

View parser

Here, basically done, so to the test procedures of the jittery, nervous and exciting!

Pan.baidu.com/s/1JRdk1RsP…

Congratulations to everybody, everybody, who is now on the path to SpringMVC’s no return! A from the entry to the road to the ground (words seem not very good to hear, but after all, learning programming, programming is old!

If this article helped you at all, please give it a thumbs up. Thanks

Finally, if there is insufficient or improper place, welcome to criticize, grateful! If you have any questions welcome to leave a message, absolutely the first time reply!

Welcome everyone to pay attention to my public number, discuss technology together, yearning for technology, the pursuit of technology, good come is a friend oh…