Spring MVC Notes

Source: Internet
Author: User

Spring MVC workflow and module capabilities

The workflow for the request processing of Spring Web MVC Dispatcherservlet is as follows:

Work flow

(1) After receiving an HTTP request, Dispatcherservlet selects and invokes the appropriate controller according to the handlermapping.
(2) The controller accepts the request and invokes the appropriate service method based on the use of the GET or POST method. The service method sets the model data based on the defined business logic and returns the view name to Dispatcherservlet.
(3) Dispatcherservlet will get help from viewresolver to define the view for requesting a check.
(4) Once the view is determined, Dispatcherservlet will pass the model data to the view and finally render it in the browser.

module functions
    • Disapatcherservlet: The central controller, as a unified access point, for global process control.
    • Handlermapping: The mapping processor is responsible for telling the central controller to call which controller
    • Controller: Handle the specific business and return the view name to Dispatcherservlet
    • Viewresolver&view: Help Dispatcherservlet check the definition view
    • Interceptor: Interceptors that intercept our defined requests and do the processing work ps:spring MVC struts2 and other MVC frameworks use the front-end Controller mode adapter mode, etc.
Initial learning: Using functions
    1. Core components: Dispatcherservlet Controller handlermapping (mapping processor, responsible for mapping the mapping policy when the central controller is forwarded to the controller) Modelandview Viewresolver Interceptors
    2. Process
      2.1 Configuring the Web. xml file
  
 
  1. <servlet>
  2. <servlet-name>springmvc</servlet-name>
  3. <servlet-class>
  4. org.springframework.web.servlet.DispatcherServlet
  5. </servlet-class>
  6. <init-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>classpath:springmvc.xml</param-value>
  9. </init-param>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>springmvc</servlet-name>
  13. <!-- struts习惯使用/*,在springmvc不管用 -->
  14. <url-pattern>*.do</url-pattern>
  15. </servlet-mapping>

2.2 Configuring the Spring.xml file

  
 
  1. <!-- 一旦有扫描器的定义,mvc:annotation-driven不需要,扫描器中包含驱动-->
  2. <context:component-scan base-package="cn.itcast.controller"/>
  3. <!-- 前缀+ viewName +后缀 -->
  4. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  5. <!-- webroot到某一指定的文件夹的路径 -->
  6. <property name="prefix" value="/WEB-INF/jsp/"/>
  7. <!-- 视图名称的后缀 -->
  8. <property name="suffix" value=".jsp"/>
  9. </bean>

2.3 Adding annotations

    • Add note @controller to indicate that the controller class is adding methods
    • Add @requestmapping ("/xxx.do") to indicate the way to access the control layer @RequestMapping ("/xxxxx") can also be used to declare namespaces on a class

      2.4 Model data Processing

    • The return value of the method can be Modelandview,newmodelandview ("XXX", map), which is equivalent to putting the result data in the request

      • Define the map directly in the parameter list of the method, which is the map inside the Modelandview, which is handled by the view parser
        (The above two methods are not recommended)
      • Recommended: Define the Model object in the parameter list of the method, which is equivalent to putting the result data in the request

      2.5 redirects

      • Controller internal redirection, redirect: Plus the value of requestmapping in the same controllerreturn "redirect:homenews-list.do";
      • Redirection between controllers: You must specify the namespace of the controller and specify the value of requestmapping, redirect: Must be added/, starting from the root directory, for example:return "redirect:/homenews/homenews-list.do";

      2.6 Use of interceptors

      • Configure the Spring MVC configuration file First:
  
 
  1. <!-- 拦截器-->
  2. <mvc:interceptors>
  3. <mvc:interceptor>
  4. <mvc:mapping path="/**/*"/>
  5. <bean class="com.hmaccelerate.interceptors.LoginInterceptor"/>
  6. </mvc:interceptor>
  7. </mvc:interceptors>
    • Then implement the relative interface


From for notes (Wiz)

Spring MVC Notes

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.