Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of SpringMVC?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article "what is the role of SpringMVC" in addition to programmers, most people do not quite understand, today, in order to make you better understand "what is the role of SpringMVC", summed up the following content, with a certain reference value, the detailed steps are clear, the details are handled properly, I hope you can get something through this article, let's take a look at the specific content.

1. Detailed introduction of SpringMVC

Through a starter example, we probably know what SpringMVC does, so what exactly is it?

Spring Web MVC is a Java-based lightweight Web framework that implements the request-driven type of Web MVC design pattern. Even if the idea of MVC architecture pattern is used, the responsibility of the web layer is decoupled, based on request-driven refers to the use of request-response model, the purpose of the framework is to help us simplify development, Spring Web MVC is also to simplify our daily Web development.

In contrast, component-based, event-driven Web frameworks, such as Tapestry, JSF, and so on, are not covered here.

Spring Web MVC is also an implementation of the service-to-worker model, but it can be optimized. The front-end controller is the DispatcherServlet; application controller, which is actually split into processor mapper (Handler Mapping) for processor management and view parser (View Resolver) for view management; page controller / action / processor is the implementation of Controller interface (including only ModelAndView handleRequest (request, response) methods) (can also be any POJO class); supports Locale parsing, Theme parsing and file upload, etc. Provides very flexible data validation, formatting, and data binding mechanisms; provides contractual programming support with powerful conventions over configuration (convention-first principle).

2. SpringMVC processing request flow

Step 1: the user sends a request to the front-end controller (DispatcherServlet).

Step 2: the front-end controller requests HandlerMapping to find Handler, which can be found according to xml configuration and comments.

Step 3: processor mapper HandlerMapping returns Handler to the front-end controller

Step 4: the front-end controller calls the processor adapter to execute the Handler

Step 5: the processor adapter executes Handler

Step 6: return ModelAndView to the adapter after the Handler execution is complete

Step 7: the processor adapter returns ModelAndView to the front controller

ModelAndView is an underlying object of the SpringMVC framework, including Model and View

Step 8: the front controller requests that the parser attempt to parse the view

Parse the real view based on the logical view name.

Step 9: attempt to return view to the front controller by the parser

Step 10: front-end controller for view rendering

Is to populate the model data (in the ModelAndView object) into the request domain

Step 11: the front controller responds to the result to the user

Let's explain some of the components that appear above:

1. Front-end controller DispatcherServlet (does not need programmer development). Function: receive requests, respond to results, equivalent to transponders, central processing units. With DispatcherServlet, the coupling between other components is reduced. 2. Processor mapper HandlerMapping (no programmer development is required). Function: look up the Handler based on the requested url. 3. Processor adapter HandlerAdapter (no programmer development is required). Function: execute Handler according to specific rules (rules required by HandlerAdapter). 4. Processor Handler (to be developed by programmers). Note: Handler is written in accordance with the requirements of HandlerAdapter so that the adapter can correctly execute the Handler5, the view parser ViewResolver (no programmer development is required). Function: view parsing, according to the logical view name parsing into the real view (view) 6, view View (need programmers to develop jsp). Note: View is an interface, and the implementation class supports different View types (jsp, freemarker, pdf... (ps: it doesn't need to be developed by programmers, it needs to be configured by programmers themselves.

It can be concluded that the only work we need to develop is the writing of the processor Handler and the writing of views such as JSP pages. Maybe you don't quite understand the terms such as front-end controller, processor mapper, and so on, so let's introduce them in detail next.

3. Configure front-end controller

Configure the following in the web.xml file:

SpringMVC_01 springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc.xml springmvc * .do 4, configure processor adapter

Configure in the springmvc.xml file. Used to constrain the Handler classes that we need to encode.

The first configuration: Controller must be implemented when writing Handler

We can check the source code:

The second configuration: HttpRequestHandler must be implemented when writing Handler

5. Write Handler

Configure in the springmvc.xml file. Generally speaking, it is the requested URL to do some business logic processing to some method of the Handler class that we write here.

We explained above that two processor adapters constrain Handler, so we will write two Handler through the above two configurations

The first one: implement the Controller interface

Package com.ys.controller; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller; public class HelloController implements Controller {@ Override public ModelAndView handleRequest (HttpServletRequest request, HttpServletResponse response) throws Exception {ModelAndView modelView = new ModelAndView (); / / similar to request.setAttribute () modelView.addObject ("name", "Zhang San") ModelView.setViewName ("/ WEB-INF/view/index.jsp"); return modelView;}}

Second: implement the HttpRequestHandler interface

Package com.ys.controller; import java.io.IOException; import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.springframework.web.HttpRequestHandler; public class HelloController2 implements HttpRequestHandler {@ Override public void handleRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setAttribute ("name", "Zhang San"); request.getRequestDispatcher ("/ WEB-INF/view/index.jsp") .forward (request, response) }}

Summary: usually we use the first way to write Handler, but the second method does not return a value, we can modify the corresponding content through response, such as returning json data.

Response.setCharacterEncoding ("utf-8"); response.setContentType ("application/json;charset=utf-8"); response.getWriter () .write ("json string")

So which one to use will be judged according to the actual situation.

5. Configure the processor mapper

Configure in the springmvc.xml file. Generally speaking, it is how the requested URL can be recognized by SpringMVC to execute the Handler we wrote in the previous step.

The first method:

In this configuration, the requested URL must be the http://localhost:8080/ project name / hello.do

The second method:

Hello1 hello2

The URL requested for this configuration can be http://localhost:8080/ project name / hello1.do, or http://localhost:8080/ project name / hello2.do

Summary: the above two processor mapper configurations can coexist, and the front-end controller will correctly determine which Handler is used by url.

6. Configure the view parser

The first configuration:

If so, the path + jsp page name + ".jsp" must be returned in Handler

The second configuration:

If this matches, then in Handler, you only need to return the name of the jsp page under the view folder.

7 、 DispatcherServlet.properties

Above we explained the various configurations, some people may ask so many configurations, if less configuration is the same, then it can not be run, then we can not configure it? The answer is yes, SpringMVC provides us with a DispatcherServlet.properties file. The system will first load the configuration in this file. If we do not, then the configuration of this file will be used by default; if we do, then we will give priority to the configuration we configured manually.

Before SpringMVC runs, the contents of the DispatcherServlet.properties file are loaded first, so let's take a look at what's inside.

We can see from the above that if we do not make various configurations manually, then there will be default.

①, processor adapter default: org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter

②, processor mapper default: org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

③, view parser default: org.springframework.web.servlet.view.InternalResourceViewResolver

Thank you for your reading. I hope you have a certain understanding of the key question of "what is the role of SpringMVC?" the specific usage still needs to be understood by everyone through hands-on experiments. Try it quickly. If you want to read more articles on relevant knowledge points, welcome to follow the industry information channel!

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: 0

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report