Elegant taught you to write your first program SpringMVC

Possible before writing the article into the SpringMVC world, from the entry into SpringMVC SpringMVC architecture in the first springMVC entry procedures explain not so elegant, delicate, sophisticated, and therefore specifically write this little elegant, delicate, delicate some of the articles, as elegant, delicate, delicate to what extent, I think as long as the basic servlet should be able to understand (ha ha, it seems a bit overblown), anyway, it should probably could fit white (I am not a big cow, I am also a white QnQ)

First, look at the regular routine of the traditional data exchange so far, ah, or the original taste, delicious!

Here Insert Picture Description
Secondly, we must clear springMVC MVC is to act in which role, and now I would say that the first SpringMVC program is jsp -> Servlet (Springmvc) -> Jsp write process, at a glance, is a collection layer! So start from the servlet.

Common servlet process: request -url-pattern - to give the corresponding servlet processing

If you now want to use springmvc, instead of the normal servlet, how to inform the program? - How to make springmvc interventional procedures:

Springmvc need to configure a servlet that comes Springmvc

By following configuration intercepts all requests to the SpringMVC treatment:

 <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>
复制代码

Detailed look:

Here Insert Picture Description
Which should be noted the following blocking rules :

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

/: All requests, attention is not / * / user: interception begin with / user requests /user/abc.do: only intercepts the request .action: only interception at the end .action request

.action example:

Here Insert Picture Description

As for init-param content by source found ... (no bullshit direct look Tu Haha)

Here Insert Picture Description
Springmvc profile configuration specifies path through the init-param attribute, if omitted, default path must be placed: /WEB-INF/springDispatcherServlet-servlet.xml
Here Insert Picture Description
Here, test yourself, is there any way to know the default path is /WEB-INF/springDispatcherServlet-servlet.xml the first to think. Actually very simple, we just put a delete init-param configuration, run Tomcat, error, look abnormal, the abnormal flow down you will find very obvious tell us the default path, as shown below:
Here Insert Picture Description

Of course, the project is compatible with springMVC version of the ordinary version of the Servlet and servlet

 <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>
复制代码

Next, create a common suffix handler class, in order to turn an ordinary class change is not common in several ways: interface / class, annotations, configuration, and springMVC with the annotation look stylish, so the use of notes, special attention @RequestMapping usage, because then it will be very common! ! !

Here Insert Picture Description
Of course, do not forget to give this class component scans , or springMVC do not know it, oh, for the convenience of scanning directly where the whole package
Here Insert Picture Description
Here, servlet is complete, followed by a JSP

Here Insert Picture Description
The next back springMVC.xml configuration, again a good thing to learn is that InternalResourceViewResolver view resolver , the first observation of the source code, as shown below:
Here Insert Picture Description
Here Insert Picture Description
This time, when you write a request, write directly to success when the default will give you resolved to the figure below;
Here Insert Picture Description

Here, basically it's done, so the test program of the link scared, tense and exciting!

Here Insert Picture Description
Well, the last analysis together to analyze the whole process of entry procedures Our first springMVC it!
Here Insert Picture Description
what? To source. . . The amount of good it has been packaged to the source network disk What? I never used Baidu network disk? Turn right out ---- Autumn Mountain duel
Here Insert Picture Description
Links: pan.baidu.com/s/1JRdk1RsP... extraction code: 5d78

Here, congratulations to you, congratulate you, now everybody perfectly embarked on a road of no return SpringMVC! One does not seem nice to be buried from the entry road (the words, but after learning programming Well, live and programmed it to the old!

If this article there is a little bit of help to you, then please point a chant praise, thank you ~

Finally, if there is insufficient or is not correct, please correct me criticism, grateful! If you have questions please leave a message, the absolute first time to reply!

I welcome you to focus on the public number, to explore technology, yearning technology, the pursuit of technology, said good pots Friends is coming Oh ...

Here Insert Picture Description

Guess you like

Origin juejin.im/post/5dddc649e51d45331c4802f8