Spring-spring MVC (11)

Source: Internet
Author: User

The springmvc framework is mainly centered around the dispatcherservlet core. It intercepts requests and assigns them to the corresponding processor for processing, and then returns the results to users. Including annotation-driven controllers, request and response information processing, view parsing, localization parsing, file uploading parsing, exception handling, form binding, etc.


Springmvc is implemented based on model2. Using the processor to separate models, views, and controls, springmvc achieves loose layer coupling between different technical levels and improves system flexibility, reusability, and maintainability. Model1 integrates the presentation logic with the business logic, which is highly coupled and not easily scalable.


Overall architecture of spring MVC:


Dispatcherservlet is at the core of the framework and coordinates different components to complete request processing and response.

1. First, the client initiates an HTTP request. The web server receives the request. If the Request Path matches the dispatcherservlet (configured in Web. XML), the Web Container submits the request to dispatcherservlet for processing.

2. After receiving the request, dispatcherservlet finds the processing handler for the request based on the request information (URL, HTTP method, request parameter, Cookie, etc.) and handlermapping configuration ). We can regard handlermapping as a routing controller and handler as the target host. In fact, spring MVC does not define a handler interface separately. handler is just a virtual concept, and any object can be used as a request processor.

3. After dispatcherservlet finds the corresponding handler Based on handlermapping,Handleradapter is used to encapsulate handler and then a unified adapter interface is used to call handler.. Handleradapter acts as an adapter and calls various handler methods using a unified interface.

Example: simplecontrollerhandleradapter. Java

public class SimpleControllerHandlerAdapter implements HandlerAdapter {public boolean supports(Object handler) {return (handler instanceof Controller);}public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {return ((Controller) handler).handleRequest(request, response);}public long getLastModified(HttpServletRequest request, Object handler) {if (handler instanceof LastModified) {return ((LastModified) handler).getLastModified(request);}return -1L;}}

Controller. Java

public interface Controller {ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;}

4. After the handler processor completes business logic processing, a modelandview is returned to dispatcherservlet. modelandview contains the view logic name and model data (k-V Key-value pairs)

5. modelandview contains the "logical view name" instead of the real view object. dispatcherservlet parses the logical view name to the real view object with viewresolver.

6. After obtaining the view object view, dispatcherservlet uses this view object to render the model data in modelandview.

7. Finally, return the information to the client. There are multiple methods, such as HTML, XML, JSON string, or image.


Instance:

1. Configure web. XML, specify the spring configuration file corresponding to the business layer, and define dispatcherservlet

<servlet><servlet-name>mvcDispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>mvcDispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping>

Container initialization automatically loads the/WEB-INF/mvcDispatcher-servlet.xml configuration file to start the spring container (sub-container) for the web layer)

2. Write a processor

/*** Class helloworldcontroller. JAVA Implementation Description: Todo class implementation description ** @ author onlyone 10:07:43 */public class hellocontroller implements controller {@ override public modelandview handlerequest (httpservletrequest req, httpservletresponse resp) throws exception {// 1. Collect parameters // 2. Bind parameters to the command object // 3. Call the service object // 4. Select modelandview mv = new modelandview () on the next page (); // Add model data to any pojo object mV. addobject ("refundobject", "he LLO Tom! "); // Set the logical view name. The view parser parses the specific view page mV. setviewname (" hello "); Return MV ;}} Based on the name ;}}

3. Write view objects

Hello. jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

4. Configure the spring MVC configuration file and use the controller and view parser to take effect.

MvcDispatcher-servlet.xml

<! -- Handlermapping --> <Bean class = "org. springframework. Web. servlet. handler. beannameurlhandlermapping"/> <! -- Handleradapter --> <Bean class = "org. springframework. Web. servlet. MVC. simplecontrollerhandleradapter"/> <! -- Viewresolver --> <Bean class = "org. springframework. web. servlet. view. internalresourceviewresolver "> <property name =" viewclass "value =" org. springframework. web. servlet. view. jstlview "/> <property name =" prefix "value ="/WEB-INF/JSP/"/> <property name =" suffix "value = ". JSP "/> </bean> <! -- Processor --> <bean name = "/Hello" class = "com. Alibaba. China. hellocontroller"/>

Reference: http://jinnianshilongnian.iteye.com/blog/1752171

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.