Spring MVC notes and simple understanding

Source: Internet
Author: User

What is 1.SPRINGMVC?

Spring Web MVC is a lightweight web framework that implements the request-driven type of the Web MVC design pattern based on Java, that is, the idea of using the MVC architecture pattern, decoupling the WEB layer from responsibility, and based on the request-driven approach is to use the request-response model, The purpose of the framework is to help us simplify our development, and Spring Web MVC also simplifies our daily web development.

1.1 Springmvc Frame composition

1. Dispacherservlet Front Controller

Responsible for the user request and response, is a central controller (dispatch), call Handlermapping Find handler, call Handleradapter processor Adapter execution handler, call Viewresolver view parser to parse the view, The Dispacherservlet facilitates the framework expansion and reduces the coupling between the various components.

2. Handlermapping Processor Mapper

No need to develop, use the framework to provide

Responsible for finding handler (common action), using XML configuration or annotations to request handler classes and requests

Handler the corresponding configuration of the link address.

3. Handleradapter Processor Adapter

No need to develop, use the framework to provide

The SPRINGMVC provides a number of adapters, the annotation adapter supports annotation development, and the original servlet adapter (handerrequestadapter) supports the execution of the original servlet.

is responsible for executing handler, the adapter is to follow certain rules to execute handler, call handler return the result is modelandview,modelandview to encapsulate model and view (models data and view)

4. Viewresolver View Parser

No need to develop, use the framework to provide

Responsible for the view resolution, return to view, the framework executes many types of view (Jstlview, Pdfview ... )

5, Handler processor (need to develop)

Need to develop, according to the rules of the adapter development, such as the adapter requires handler implementation of an interface, handler must implement the interface.

6, View view page (JSP) (need to develop)

Need to develop, jsp+jstl. For view versatility It is recommended to use a third-party framework, Jstl.

The processor mapper, processor adapter, and view resolver are called Springmvc Big Three.

Introduction to HTTP Protocol 1.1 HTTP Introduction/http SSL

HTTP (hypertext Transfer Protocol, Hypertext Transfer Protocol ①) is a communication on the World Wide Web

The protocol scheme that is used. HTTP has many applications, but most notably for Web browsers and Web services

Duplex communication between the services.

1.2 HTTP Messages

Message composition

Start line

The first line of the message is the starting line, which is used in the request message to indicate what to do and in the response message.

What happened to the state.

• Header Field

The starting line is followed by 0 or more header fields. Each header field contains a first name and a value for

For easy parsing, separated by a colon (:). The header ends with a blank line. Add a first

The Department field is as simple as adding a new line.

• Body

The empty line is followed by an optional message body, which contains all types of data. The request body includes

The data to be sent to the Web server, and the data to be returned to the client is loaded in the response body. From

The beginning and the header are both textual and structured, and the body is different, and the body can contain any

Binary data (slices, video, audio tracks, software programs). Of course, the body can also contain

Text

1.3 Request Instance

1.4 Some common HTTP methods

GET send a named resource from the server to the client

Put stores data from the client to a named server resource

Delete the named resource from the server

Post sends client data to a server gateway application

HEAD only sends the HTTP header in the named resource response

1.5 HTTP status Code

Common Status Codes

OK. Document returned correctly

302 Redirect (redirected). Go to other places to get resources

404 Not Found (found). Unable to find this resource

405 Method Not allowed (methods not allowed)

406 not acceptable (unacceptable) clients can specify parameters that describe which types of entities you want to accept. This code can be used when the server does not have a resource that matches a guest-acceptable URL

The request has a method that is not supported by the requested URI. In response

An allow header should be included to inform the client of the requested

What methods are used for resource support

1.6 Status Code classification

Overall scope defined scope category

100~199 100~101 Information

200~299 200~206成 Gong

300~399 300~305 redirect

400~499 400~415 Client Error

500~599 500~505 Server Error

1.7 Relationship with SPRINGMVC (path of Struts2 request)

Processor Mapper:

Processor adapter: Use the following adapter

As can be seen from the above code, if handler implements the Controller interface, it can complete the execution work.

View Resolver

If you have a jstl jar package under Classpath, you do not need to configure the following

Fourth step: Write the handler processor (write action)

Follow the rules required by the adapter

Configure Action-servlet.xml with the following code:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:context= "Http://www.springframework.org/schema/context"4 xmlns:p= "http://www.springframework.org/schema/p"5 Xmlns:mvc= "Http://www.springframework.org/schema/mvc"6 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"7 xsi:schemalocation= "Http://www.springframework.org/schema/beans8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd9 Http://www.springframework.org/schema/contextTen http://www.springframework.org/schema/context/spring-context.xsd One Http://www.springframework.org/schema/mvc A http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> -  -     <BeanID= "U9controller"name= "/u9.action"class= "Com.u9vip.springmvc.controller.U9Controller"></Bean> the          <!--View Resolver - -          <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> -           <!--prefix name - -          < Propertyname= "prefix"value= "/web-inf/u9vip/"></ Property> +           <!--suffix name - -           < Propertyname= "suffix"value= ". jsp"></ Property> +          </Bean> A </Beans>

Fifth step: Writing JSP view page

To establish welcome.jsp in the web-inf/jsp directory, the main code is as follows:

<%@ Pagelanguage= "Java"Import= "java.util.*"pageencoding= "UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML>        <Head><title>My JSP ' u9firstspringmvc.jsp ' starting page</title>        </Head>        <Body>hello,spring MVC ${username}<BR>        </Body></HTML>

Sixth step: Start the Tomcat run project

start the server and enter the Http://localhost:8080/SpringMVC02/u9.action access Test .

Brief analysis of the execution process

(1) After starting the server, when we send the u9.action request to the server, it is first intercepted by the front-end controller Dispatcherservlet configured in Web. Xml.

(2) The front controller forwards this request to the back-end controller, The following analysis of the handoff process: when looking for a mapping processor in Action-servlet.xml that can perform a u9.action request, there is no mapping processor that can handle this request, then the default mapping processor is used Beannameurlhandlermapping:this is the default implementation used by the Dispatcherservlet, along with Defaultannotationhandlermapping (on Java 5 and hi Gher). We also need to note that the bean name for this backend controller must start with "/" and be configured with a dispatcherservlet mapping. Wildcard configuration is supported at the same time beanname. For example, if you configure: *.action, you can successfully access the U9controller class when you access U9vip.action.

(3) The Beannameurlhandlermapping processor will look for a bean instance named "U9.action" in the Spring container. When this instance is found, the bean is used as the back-end controller to handle this request. It also adds itself to the map processor chain and passes this request to the processor chain.

(4) The back-end controller is processed and returned to the view.

Spring MVC notes and simple understanding

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.