Springmvc Getting Started

Source: Internet
Author: User

1 MVC Pattern Review

Spring MVC is an MVC -based Web application framework.

MVC is a design pattern, the application of MVC in B/s system:

To perform the steps:

    1. Request, request to c,c in MVC to receive the request and cannot process the request, need to give the request to M
    2. The model is used to process the request, and all the things used to deal with it are models, Pojo,action,service,dao
    3. After processing, the results are returned to C
    4. c requires some good-looking, rendered pages to respond, which is used in the V view
    5. The controller renders the view, populating the model data into the request domain
    6. When the request is ready, you can response.

Note: The model cannot populate the view with the data under the B/s system

Contact with SPRINGMVC: controllers, models, views how are these implemented in spring?

2 Spring MVC Framework

To perform the steps:

1 initiating a request to the front segment controller (dispatcherservlet)

2 Front-end controller request Handlermapping Find handler can be found based on XML configuration, annotations

3 Processor Mapper Handlermapping back to the front-end controller Handler

4 Front-end controller calls the processor adapter to perform handler

5 Processor adapter to perform handler

6 Handler The adapter returns Modelandview after execution

7 Processor Adapter Return to front end controller Modelandview,modelandview is an underlying object of the SPRINGMVC framework, including model and view

8 Front-End controller requests the view resolver to parse the view and parse it into a real view (JSP) based on the logical view name

9 View resolver returns view to front Controller

10 front-end controller for view rendering, view rendering fills the model data (in the Modelandview object) to the request field

11 front-end controller responds to user results

3 SPRINGMVC Main components

1 Front-end controller Dispatcherservlet (no program development required)

Function: Receive request, response result, equivalent to transponder, CPU

Reduces the coupling between other components.

2 Processor Mapper handlermapping (no program development required)

Function: Find handler based on the URL requested

3 Processor Adapter Handleradapter (no program development required)

Function: Execute handler According to the specific rules (Handleradapter requirements)

4 Processor Handler ( requires program development )

Note: When writing handler, follow the handleradapter requirements so that the adapter can execute correctly handler

5 View Parser View resolver (no program development required)

Function: Perform a view resolution, resolve to a real view based on the logical View name (view)

6 views View ( requires programmer to develop JSP)

View is an interface that implements classes that support different view types

41 Simple SPRINGMVC Case 4.1 configuring a front-end controller
1   <!--Configuring the SPRINGMVC front-end controller -2   <servlet>3       <Servlet-name>Springmvc</Servlet-name>4       <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>5    <Init-param>6         <Param-name>Contextconfiglocation</Param-name>7         <Param-value>Classpath:/*-servlet.xml</Param-value>8     </Init-param>9     <Load-on-startup>1</Load-on-startup>Ten   </servlet> One    A   <servlet-mapping> -       <Servlet-name>Springmvc</Servlet-name> -        <Url-pattern>/</Url-pattern> the   </servlet-mapping>

4.2 Configuration Processor: Configurator

1 <!--  -2<class = " Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter "/>

4.3 Configuration Processor: Mapper

1 <!--  - 2   <class= " Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping "/>

4.4 Configuring the View resolver

1 <!--Viewresolver -2 <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">3   < Propertyname= "Viewclass"value= "Org.springframework.web.servlet.view.JstlView"/>4   < Propertyname= "prefix"value= "/web-inf/jsp/"/>5   < Propertyname= "suffix"value= ". jsp"/>6 </Bean>

4.5 Processor Writing

1 <!-- Handler  - 2 <  name= "/hello"  class= "Com.inspur.handler.HelloWorldController" />

Java Code

1  Public classHelloworldcontrollerImplementsController {2 @Override3      PublicModelandview HandleRequest (httpservletrequest request,4HttpServletResponse response)throwsException {5        //back to Modelandview6Modelandview mv =NewModelandview ();7        //setattribute equivalent to request, which takes data through a message in a JSP page8Mv.addobject ("message", "Hello world!");9        //sets the logical view name that the view resolver resolves to a specific view page based on that nameTenMv.setviewname ("Hello"); One        returnMV; A     } -}

4.6 jsp Writing

1 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%>2${message}

4.7 Visits

Http://localhost:8080/springmvctfirst/hello

Springmvc Getting Started

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.