In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to configure SpringMvc", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to configure SpringMvc.
Servlet interpretation
Students who are familiar with Servlet programming know that in order for the request we send to be processed, we usually need to write a Servlet to inherit HttpServlet, so as to process our request. For example, our request goes like this: http://localhost:8080/test, at this point, we need to write a TestSeervlet to process the request, and then rewrite the doGet or doPost methods in it to handle our specific logic. For example, query database, query cache, etc., as to how our request matches our Servlet, we will not explain too much here. Later, we will thank the Tomcat source code separately. To make this piece clear, what we are talking about now is mainly SpringMvc, that is, the request has been received by Tomcat. Next, let's take a look at how SpringMvc takes over this request.
SpringMvc review
Anyone who has used SpringMvc probably knows that after we have configured the running environment for Spring and SpringMvc, we only need to write a XXXController in our annotated scanned package to process one of our XXX requests, such as the following code. This is a very simple piece of SpringMvc code. When we access the request through the browser http://localhost:8080/test/hello, we will see the corresponding result in the browser, so a simple application will run.
````java [@ RestController] (https://my.oschina.net/u/4486326)@RequestMapping(value="/test")public class TestController {@ GetMapping (value= "/ hello") public String hello () {return "Hello SpringMvc";}} SpringMvc run process
Through the example of HelloWorld above, we find that SpringMvc is much easier to use than native Servlet. Next, let's take a look at how SpringMvc handles a request. Of course, the first step is to set up the environment.
1. System environment and some instructions
The idea+maven+tomcat system development environment is not a key point. There are two ways to build an Spring and SpringMvc environment, one is xml and the other is annotations. Of course, in order to correspond to the later SpringBoot, I use the annotated version here, because SpringBoot uses a lot of annotations, so I suggest you switch from xml configuration to annotation configuration. This is also beneficial to the later study of Boot and Cloud. Of course, I am also here to talk about why the SpringMvc source code, rather than SpringBoot, in fact, SpringBoot is not a new technology, a large number of its configuration is just the use of Spring left us some extension points and its own configuration file, in addition to some condition notes, you can do some automatic configuration, so I personally think that the core source code when Spring and SpringMvc and Tomcat and other content. Of course, if there is still time to follow, some content about these aspects will be written.
Configuration file
After we first use idea to create a web project, and introduce the local tomcat into idea, configure the access path and port of the project, this process is relatively simple, I will not go into details here. Next, let's configure the contents of pom.xml as follows.
Org.springframework spring-webmvc 5.3.5 javax.servlet javax.servlet-api 4.0.1 provided org.junit.jupiter junit-jupiter-api ${junit.version} test org.junit.jupiter junit-jupiter-engine ${junit.version} test
These are the dependencies. You can refer to my directory structure for building.
The first is a parent project, of course, it only manages child projects, does not write code, the type is pom, and then a common module, I put some basic dependencies in this to facilitate later maintenance, and then our web environment when springmvc-demo1, is writing code for this module is followed by a configuration of tomcat, the access path I configured is the root path.
At this point, I believe you have a deeper understanding of "how to configure SpringMvc", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 265
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.