First knowledge of Spring MVC

Source: Internet
Author: User

I. Introduction to Spring MVC

1. Spring MVC is also called Spring Web MVC, which is the framework of the presentation layer. Spring MVC is part of the spring framework and is released after Spring3.0.

2.spring MVC Architecture

(1)

(2)

3.spring MVC Request Process

Request---------->dispatcherservlet (Central scheduler/Front Controller)--------->handlermapping (processor mapper)------------> Returns an execution chain---------> transfers the execution chain to the Handleradaptor (processor adapter)------->

Dispatch handler (processor)--------->modelandview---------> give the results to Handleradaptor first, Then forwarded by Handleradaptor to Dispatcherservlet--------->dispatcherservlet Dispatch viewresolver (attempt parser)--------> Submit to Dispatcherservlet---------> Find-------->view to respond (response).

4.SpringMVC first introductory case

Case: The user submits a request, and the server-side processor, after receiving the request, gives a welcome message that is displayed on the page.

01. Importing The JAR Package

   <dependency>      <groupId>org.springframework</groupId>      <ARTIFACTID>SPRING-WEBMVC </artifactId>      <version>4.3.7.RELEASE</version>    </dependency><!--https:// Mvnrepository.com/artifact/org.springframework/spring-context-support--><dependency>    <groupId >org.springframework</groupId>    <artifactId>spring-context-support</artifactId>    <version>4.2.0.RELEASE</version></dependency>

02. Configuration Steps

1〉 Configuring the front-end controller in Web. xml

2〉 Processor Mapper (handlermapping, with default values, so it can be configured without configuration)

3〉 Processor Adapter (Handleradaptor, with default values, so it can be configured without configuration)

4〉 processor

5〉 attempt parser (Viewresovler, with default value, so can not be configured)

03. Register Front-End controller

<web-app>  <display-name>archetype Created web application</display-name>  <servlet >    <servlet-name>springmvc</servlet-name>    <servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class>    <init-param>      < Param-name>contextconfiglocation</param-name>      <param-value>classpath:applicationcontext.xml </param-value>    </init-param>    <load-on-startup>1</load-on-startup>  </ servlet>  <servlet-mapping>    <servlet-name>springmvc</servlet-name>    < Url-pattern>/</url-pattern>  </servlet-mapping></web-app>

04. Custom Processors

Import Org.springframework.web.servlet.modelandview;import Org.springframework.web.servlet.mvc.controller;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class FirstController Implements Controller {public    modelandview handlerequest (HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse) throws Exception {        Modelandview mv = new Modelandview ();        Mv.setviewname ("/index.jsp");  Logical view name        return mv;    }}

05.jsp Page Build-up

<%@ page contenttype= "Text/html;charset=utf-8" language= "Java" iselignored= "false"%><%@ taglib prefix= "C" Uri= "Http://java.sun.com/jsp/jstl/core"%>

06.applicationcontext.xml Configuration

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"       Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:mv= "Http://www.springframework.org/schema/mvc       " xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc.xsd ">    <bean id="/hello.do "class=" Cn.happy.controller.FirstController "></bean>< /beans>

5. Promotion of the first case----) attempt parser

01. In the Applicationcontext.xml, add:

    <!--trying to resolve the parser--    <bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >        <!--prefix--        <property name= "prefix" value= "/" ></property>        <!--suffix        --< Property name= "suffix" value= ". jsp" ></property>    </bean>

02. Modify the logical attempt name in the custom processor:

  Mv.setviewname ("index");  Logical View Name

6. The statement on Urlpattern

01. It is best to be *.do, not to be a/*, (Cannot access dynamic resources such as *.jsp), preferably also do not match/(unable to access static resources), have to match/. Everything from restful programming

02. Cannot be configured AS/*: Dispatcherservlet will request to a dynamic page, that is, a request to the JSP page to jump requests also as a normal controller request. The central scheduler calls the processor mapper to find the appropriate processor for it. Of course I can't find it.  Jap page returns 404 errors. Conclusion:/* blocks dynamic resources.

7. Address three ways that dynamic resources cannot be accessed:

01. Have to match/, use the Tomcat default servlet resolution, add the following code in Web. XML:

 <!--solving static resource scenarios--------<servlet-mapping> <servlet-name>default </servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping > <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </ servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>* .js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</ servlet-name> <url-pattern>*.pdb</url-pattern> </servlet-mapping> <servlet-mapping> < ;servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping 

02. Using MVC's Default-servlet-handler, a request for access to a static resource is mapped to the default Servlet request processor Defaultservletrequesthandler object through handlermapping. The processor calls Tomcat's Defaultservlet to handle access requests for static resources. Of course, you need to introduce MVC constraints.

xmlns:mv= "Http://www.springframework.org/schema/mvc" <!--fix static resource scenario two under  the Mapper--    <MV: Default-servlet-handler/>

03. Using MVC's Resource solution

After spring3.0.4, spring defines a processor Resourcehttprequesthandler that is specifically designed to handle static resource requests. <mvc:resource/> tags are added to resolve static resource unreachable issues.

<!--solving static resource scenarios three--    <mv:resources mapping= "/image/**" location= "/image/"/>

Location: The directory where the static resource resides

Mapping: Request for a resource

  

  

  

  

  

  

 



First knowledge of Spring MVC

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.