Spring MVC five core components and configurations

Source: Internet
Author: User
Tags aop

One, five core components

1.DispatcherServlet Request Entry

2.HandlerMapping request dispatch, responsible for the request and the controller to establish one by one corresponding relationship

3.Controller processor

4.ModelAndView Package model information and view information

5.ViewResolver view processor, positioning page

Two, Spring MVC writing step (access to the. JSP under Web-inf)

1. Build the project, import the jar package (IOC MVC) and copy the corresponding configuration file from the spring container to SRC, and create a hello.jsp under Web-inf

2. Configure the Dispatcherservlet in Web. XML and specify the corresponding configuration file for the spring container by initializing the parameters Contextconfiglocation

3. Configure the Handlermapping implementation class in the spring configuration file simpleurlhandlermapping

4. Write a controller class to implement the Controller interface, return Modelandview in the controllers method, configure the controller in the spring container

5. Configuring the Viewresolver implementation class Internalresourceviewresolver

:

Configure Dispatcherservlet

1<?xml version= "1.0" encoding= "UTF-8"?>2<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xsi: schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version= " 3.1 ">3<display-name>spring-mvc</display-name>4<welcome-file-list>5<welcome-file>index.html</welcome-file>6<welcome-file>index.htm</welcome-file>7<welcome-file>index.jsp</welcome-file>8<welcome-file>default.html</welcome-file>9<welcome-file>default.htm</welcome-file>Ten<welcome-file>default.jsp</welcome-file> One</welcome-file-list> A<!--configuration Request Entry-- -<servlet> -<servlet-name>SpringMVC</servlet-name> the<servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> -<!--configuration initialization Parameters-- -<init-param> -<param-name>contextConfigLocation</param-name> +<param-value>classpath:applicationContext.xml</param-value> -</init-param> +</servlet> A<servlet-mapping> at<servlet-name>SpringMVC</servlet-name> -<url-pattern>*. Do</url-pattern> -</servlet-mapping> -</web-app>
View Code

Configuration Handlermapping

1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:context= "Http://www.springframework.org/schema/context"5Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc"6Xmlns:jee= "Http://www.springframework.org/schema/jee"7xmlns:tx= "Http://www.springframework.org/schema/tx"8xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"9Xmlns:mvc= "Http://www.springframework.org/schema/mvc"TenXmlns:util= "Http://www.springframework.org/schema/util" OneXmlns:jpa= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA" Axsi:schemalocation=" -http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsd -http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd thehttp//Www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd -http//Www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-4.1.xsd -http//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.1.xsd -http//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPAhttp://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd +http//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd -http//Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd +http//Www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.1.xsd"> A<!--Configure the request dispatcher to establish a one by one correspondence between the request and the controller- at<bean id= "Handlermapping"class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > -<property name= "Mappings" > -<props> -<prop key= "/tohello.do" >helloController</prop> -</props> -</property> in</bean> -<!--Configuring the controller-- to<bean id= "Hellocontroller"class= "Com.xcz.controller.ToHelloController" ></bean> +</beans>
View Code

Configuration Viewresolver

1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:context= "Http://www.springframework.org/schema/context"5Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc"6Xmlns:jee= "Http://www.springframework.org/schema/jee"7xmlns:tx= "Http://www.springframework.org/schema/tx"8xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"9Xmlns:mvc= "Http://www.springframework.org/schema/mvc"TenXmlns:util= "Http://www.springframework.org/schema/util" OneXmlns:jpa= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA" Axsi:schemalocation=" -http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsd -http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd thehttp//Www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd -http//Www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-4.1.xsd -http//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.1.xsd -http//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPAhttp://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd +http//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd -http//Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd +http//Www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.1.xsd"> A<!--configuration View processor-- at<beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > -<property name= "prefix" value= "/web-inf/" ></property> -<property name= "suffix" value= ". JSP" ></property> -</bean> -</beans>
View Code

Final configuration Results

1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:context= "Http://www.springframework.org/schema/context"5Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc"6Xmlns:jee= "Http://www.springframework.org/schema/jee"7xmlns:tx= "Http://www.springframework.org/schema/tx"8xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"9Xmlns:mvc= "Http://www.springframework.org/schema/mvc"TenXmlns:util= "Http://www.springframework.org/schema/util" OneXmlns:jpa= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA" Axsi:schemalocation=" -http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsd -http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd thehttp//Www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd -http//Www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-4.1.xsd -http//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.1.xsd -http//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPAhttp://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd +http//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd -http//Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd +http//Www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.1.xsd"> A<!--Configure the request dispatcher to establish a one by one correspondence between the request and the controller- at<bean id= "Handlermapping"class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > -<property name= "Mappings" > -<props> -<prop key= "/tohello.do" >helloController</prop> -</props> -</property> in</bean> -<!--Configuring the controller-- to<bean id= "Hellocontroller"class= "Com.xcz.controller.ToHelloController" ></bean> +<!--configuration View processor-- -<beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > the<property name= "prefix" value= "/web-inf/" ></property> *<property name= "suffix" value= ". JSP" ></property> $</bean>Panax Notoginseng</beans>
View Code

Finally open the service, enter localhost on the browser: Port number/project name/tohello.do, see the following interface, indicating that the configuration is successful

Spring MVC five core components and configurations

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.