Spring MVC, Spring Learning Notes

Source: Internet
Author: User
Spring MVC, Spring Learning notes

Original link: http://leoluo.top/2017/11/21/Spring/
blog:why so serious
Github:leoluo22
CSDN: My CSDN 0x00 preface

Why do I have to learn spring?

It used to be Django, but the company was using spring. Come to the company after the company set up a good shelf to write code, also do not care about the various configuration and configuration files. But there's a skill game on the weekends that requires a spring frame, so I start all over again and copy and paste the various configuration files. As a result, stuck in the landing, killing not get the value I put in the session. 0x01

Spring MVC is all called the Spring Web MVC Framework, which provides a model-view-controller (MVC) architecture. MVC can scatter different parts of the application (input logic, business logic, UI logic) and make them less coupled. Model: Encapsulation Application data, usually made up of POJO . View: The data that is responsible for rendering model, usually produces HTML files that the browser can parse. Controller: Responsible for handling user requests, constructing the appropriate model and passing it to view rendering. 0x02 Dispatcherservlet

The Spring MVC framework revolves around a dispacherservlet design, Dispacherservlet is used to handle all HTTP requests and responses. As shown in the following illustration:

The following is an HTTP processing process sent to Dispacherservlet: after receiving an HTTP request, Dispacherservlet asks Handlermapping to invoke the appropriate controller Controller receives the request and then invokes the appropriate service method. The service method sets the model data based on the defined business logic and returns the view name to Dispacherservlet dispacherservlet query Viweresolver to select the view defined for the request when Viwe is good, Dispacherservlet the model data bed to View,view rendering to the browser

All of the above components, such as Handlermapping,controller and Viewresolver, are part of the webapplicationcontext. 0x03 Required Configuration

You need to map the request that you want Dispatcherservlet to process through the URL in web.xml . The following example is used to declare and map the helloweb dispatcherservlet.

 <?xml version= 1.0 encoding= "UTF-8"?> <display-name>spring mvc</display-name> <web-app ID = "webapp_id" version= "2.4" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "HTTP://XMLNS.JCP.ORG/XM" L/ns/javaee "xsi:schemalocation=" Http://xmlns.jcp.org/xml/ns/javaee Http://xmlns.jcp.org/xml/ns/javaee/web-app _3_1.xsd "> <servlet> <servlet-name>HelloWeb</servlet-name> <SERVLET-CLASS>ORG.SPRINGFR
Amework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>HelloWeb</servlet-name> <URL-PATTERN>*.J sp</url-pattern> </servlet-mapping> </web-app> 

Web.xml in the Webcontent/web-inf folder you are applying. When the helloweb Dispatcherservlet is initialized, the framework attempts to webcontent/web-inf from a file under the servlet-name]servlet.xml The file is loaded into the context of the application. This time our file is called hellowebservlet.xml .

Next, the label indicates which dispatcherservlet the URL will be processed by. All HTTP requests here end with the . JSP will be helloweb dispatcherservlet.

If you do not want the default [servlet-name]servlet.xml filename and path, you can add the servlet listener in your web.xml Contextloaderlistener To customize the file name and path.

<web-app ... >

<!-------------------dispatcherservlet definition goes here----------------> ...
.
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /web-inf/helloweb-servlet.xml,
    </param-value>
</context-param>

<listener>
    <listener-class>
        Org.springframework.web.context.ContextLoaderListener
    </ Listener-class>
</listener>

Now, take a look at the required configuration for Helloweb-servlet.xml .

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:util= "Http://www.springframework.org/schema/util" xmlns:
   Mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema/beans 
   Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/context 
   Http://www.springframework.org/schema/context/spring-context-3.0.xsd Http://www.springframework.org/schema/util Http://www.springframework.org/schema/util/spring-util-3.0.xsd Http://www.springframework.org/schema/mvc http:// Www.springframework.org/schema/mvc/spring-mvc.xsd "> <!--Sweep package--> <context:component-scan base-package=" com.springmvc.* "></context:component-scan> <bean class=" org. springframework.web.servlet.view.InternalResourceViewResolver "> <!--develop the path--> <property name of the page = "prefix" value= "/web-inf/pages/" ></property> <!--file suffix--> <property name= "suffix" val   
 Ue= ". jsp" ></property> </bean> </beans>
[Servlet-name]-servlet.xml is used to create the defined beans

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.