Spring Learning Note five----spring MVC

Source: Internet
Author: User

  1. Spring MVC's usual execution process is when a Web request is sent to the spring MVC Application,dispatcher servlet to receive this request, and the controller is found through handlermapping. Delegate this request to a controller's handler method processing, this handler method finishes processing the request, returns a Modelandview to Dispatcher Servlet,dispatcher The servlet uses the view Name, requests the view Resolver,view Resolver returns the corresponding View;dispatcher servlet to the view, passes the model to the view, and returns the response;
  2. For each controller inside the handler Method, it can receive the following parameters:
    • An object of type HttpServletRequest or httpservletresponse;
    • Any type of request parameter, using the @requestparam callout, the values of these request parameters come from the requests parameter in the URL;
    • Any type of path Variable, annotated with @pathvariable, the values of these parameters come from a portion of the request path, and the premise of using this is that you must be in this handler A placeholder is used inside the @requestmapping of the method callout;
    • object of model type; You can add attributes to this object;
    • Any type of cookie attribute value, using @cookievalue notation;
    • Map or Modelmap type of object, Handler method can be used to add attributes to the model;
    • Errors or Bindingresult type of object, Handler method is used to access the validation results of the model object;
    • Sessionstatus type of object, Handler method can be used to set the completion of a session;
    Handler method can have two types of return values:
    • A string that represents the name of the view;
    • The other is void, which depends on the parameters received by the handler method, if it receives the parameters of the Modelandview type, and the view name is set in this parameter, then this view name will be used by the subsequent view resolver. Otherwise, spring will be based on the URL configured in @requestmapping, the last part of the URL will be the default view name, if the URL configured in @requestmapping the last part is a wildcard (*), it will be the current handler Method name as the view name;
  3. In a SPRINGMVC-based Web application, there are typically two types of configuration files:
    • One is the Web application configuration file (XML), which you need to specify in theone or moreA servlet instance and a URL-to-servlet mapping relationship:
      <servlet>    <Servlet-name>Servlet name</Servlet-name>    <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>    <Load-on-startup>1</Load-on-startup></servlet><servlet-mapping>    <Servlet-name>Servlet name</Servlet-name>    <Url-pattern>/</Url-pattern></servlet-mapping>
      In larger applications, it is particularly important to specify multiple Dispatcherservlet instances, which allows each dispatcherservlet to serve different functions, with different and completely isolated spring Container;
    • Another is the spring MVC configuration file, in the case of unspecified, each dispatcherservlet will go to the Web-inf directory to load a name {servlet name}- Servlet.xml spring configuration file; You can also specify by a servlet named contextconfiglocation parameter;
      You can also specify a configuration file for root application, which will be accessed or replicated by the bean in each dispatcherservlet configuration file. This configuration file must be loaded with a contextloaderlistener type of listener, which you can specify by using a context parameter called Contextconfiglocation. By default, this file is/web-inf/applicationcontext.xml.
  4. Before you create a controller for the Web application, you need to add the <context:component-scan base-package= "..." in the application context configuration file/> Go to scan all the tags have @controller and @requestmapping annotations class, or add @componentscan annotation on your config class, and set the Basepackages property;
    In addition, we need to have spring MVC pre-register some beans, so we need to add <mvc:annotation-driven to the application context configuration file/> You can also add @enablewebmvc annotation to your configuration class to achieve the same effect;
  5. After handler method returns the view name, you need to add a viewresolver bean in the application context configuration file, which is responsible for parsing a view name into the implementation of a view;
  6. If we were to map a request to a method in the controller, we would have to label the method with @requestmapping annotation and specify the path to the map in this annotation, which could be a definite path, You can also include wildcards or some placeholders for path variable, or if you don't specify a path in @requestmapping annotation, the default handler Method will use the path specified in the @requestmapping annotation configured on the controller, and you can also qualify the request Type in @requestmapping;

Spring Learning Note five----spring MVC

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.