Spring MVC Framework Configuration

Source: Internet
Author: User

1. Overview

Spring MVC role: Used to enable front-end browser interaction with later programs

Spring MVC is a spring-based MVC framework, so-called MVC (Model,controller,view), the whole spring MVC role is based on spring's model (data) in the Controller (daemon), view Interaction between (front-end browser)

As for the advantages and disadvantages of spring MVC, it is not a good idea to evaluate

2. Referenced jar package

Since spring-based core jar package (beans,context,core,expression,commons-logging) is a must; Spring MVC has a related jar package (WEB,WEBMVC), the package (AOP) is not a must, but if it is based on annotations, it is necessary to use it for packet scanning.

  

3. Configuration files

The configuration file is the initialization file that is explicitly configured to execute the program. The configuration file is as follows:

Springmvc-config.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.3.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-4.3.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-4.3.xsd ">    <!--<context:component-scan base-package= "Com.itheima.controller"/> -    <!--Configuring processor handle, mapping "/firstcontroller" requests -    <Beanname= "/firstcontroller"class= "Com.itheima.controller.FirstController" /><!--<Mvc:annotation-driven/>--! >    <!--Processor Mapper to find the name of the processor handle as a URL -    <Beanclass= "Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />    <!--processor adapter, configuring a call to the HandleRequest () method in the processor -    <Beanclass= "Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />    <!--View Resolver -    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">    </Bean></Beans> 

Xml

<?XML version= "1.0" encoding= "UTF-8"?><Web-appxmlns= "Http://xmlns.jcp.org/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version= "3.1">    <servlet>        <!--Configure front-end filters -        <Servlet-name>Springmvc</Servlet-name>        <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>        <!--load configuration file when initializing -        <Init-param>            <Param-name>Contextconfiglocation</Param-name>            <Param-value>Classpath:springmvc-config.xml</Param-value>        </Init-param>        <!--indicates that the container loads the servlet as soon as it starts -        <Load-on-startup>1</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Springmvc</Servlet-name>        <Url-pattern>/</Url-pattern>    </servlet-mapping></Web-app>

  

Picture Source (http://www.cnblogs.com/davidwang456/p/4090058.html)

  

Step: 1, client initiated access, blocked by spring MVC's front-end controller (dispatcherservlet)

2, the Interceptor will find the Mapper (handlermapping), let the mapper find the specific bean based on the URL, for example, if the URL "/firstcontroller" then found the corresponding bean, and feedback to Dispatcherservlet

3, Dispatcherservlet will go to the found bean to the adapter (Handleradapter), the adapter to call the corresponding handler (executing the method in the bean)

4, after the completion of execution, the results will be returned to Dispatcherservlet, and then by Dispatcherservlet to the View parser (Viewreslover)

5, the view resolution is completed, then to Dispatcherservlet, and then to view rendering (such as JSP). Finally, the rendered result is fed back to the client

4. Controller class

 PackageCom.itheima.controller;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.Controller;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** Controller Class*/ Public classFirstcontrollerImplementscontroller{@Override PublicModelandview HandleRequest (httpservletrequest request, HttpServletResponse Respo NSE) {//Create a Modelandview objectModelandview Mav =NewModelandview (); //to add data to a model objectMav.addobject ("msg", "This is my first spring MVC program"); //Set logical view nameMav.setviewname ("/web-inf/jsp/first.jsp"); //returns the Modelandview object        returnMav; }}

The adapter used in the example (Simplecontrollerhandleradapter) requires that the Controller interface be implemented by the handler

5. JSP

<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Starter Program</title></Head><Body>${msg}</Body></HTML> 

Testing can be done by

PS: The above code for the Black Horse Video tutorial code, their own manual knocking.

Disclaimer: This article is a beginner spring MVC used to record notes, completely small white, understanding is more superficial, if there is danale willing to advise, greatly appreciated

Spring MVC Framework Configuration

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.