欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring MVC五大核心组件和配置

程序员文章站 2022-06-12 21:03:45
一,五大核心组件 1.DispatcherServlet 请求入口 2.HandlerMapping 请求派发,负责请求和控制器建立一一对应的关系 3.Controller 处理器 4.ModelAndView 封装模型信息和视图信息 5.ViewResolver 视图处理器,定位页面 二,Spri ......

一,五大核心组件

  1.DispatcherServlet  请求入口

  2.HandlerMapping    请求派发,负责请求和控制器建立一一对应的关系

  3.Controller       处理器

  4.ModelAndView     封装模型信息和视图信息

  5.ViewResolver    视图处理器,定位页面

二,Spring MVC的编写步骤(访问WEB-INF下的.jsp)

  1.建立项目,导入jar包(ioc mvc)并且拷贝Spring容器中对应的配置文件到src下,并且在WEB-INF下创建一个hello.jsp

  2.在web.xml中配置DispatcherServlet并通过初始化参数contextConfigLocation指定Spring容器对应的配置文件

  3.在Spring配置文件中配置HandlerMapping的实现类SimpleUrlHandlerMapping

  4.写一个控制器类实现Controller接口,控制器方法中返回ModelAndView,在Spring容器中配置控制器

  5.配置ViewResolver的实现类internalResourceViewResolver

如图:

Spring MVC五大核心组件和配置

配置DispatcherServlet

Spring MVC五大核心组件和配置
 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>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   <!-- 配置请求入口 -->
13   <servlet>
14       <servlet-name>SpringMVC</servlet-name>
15       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16       <!-- 配置初始化参数 -->
17       <init-param>
18           <param-name>contextConfigLocation</param-name>
19           <param-value>classpath:applicationContext.xml</param-value>
20       </init-param>
21   </servlet>
22   <servlet-mapping>
23       <servlet-name>SpringMVC</servlet-name>
24       <url-pattern>*.do</url-pattern>
25   </servlet-mapping>
26 </web-app>
View Code

配置HandlerMapping

Spring MVC五大核心组件和配置
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" 
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context" 
 5     xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
 6     xmlns:jee="http://www.springframework.org/schema/jee" 
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xmlns:aop="http://www.springframework.org/schema/aop" 
 9     xmlns:mvc="http://www.springframework.org/schema/mvc"
10     xmlns:util="http://www.springframework.org/schema/util"
11     xmlns:jpa="http://www.springframework.org/schema/data/jpa"
12     xsi:schemaLocation="
13         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
14         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
15         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
16         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
17         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
18         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
19         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
20         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
21         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
22     <!-- 配置请求分发器,让请求和控制器之间建立一一对应关系 -->
23     <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
24         <property name="mappings">
25             <props>
26                 <prop key="/toHello.do">helloController</prop>
27             </props>
28         </property>
29     </bean>
30     <!-- 配置控制器 -->
31     <bean id="helloController" class="com.xcz.controller.ToHelloController"></bean>
32 </beans>
View Code

配置ViewResolver

Spring MVC五大核心组件和配置
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" 
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context" 
 5     xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
 6     xmlns:jee="http://www.springframework.org/schema/jee" 
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xmlns:aop="http://www.springframework.org/schema/aop" 
 9     xmlns:mvc="http://www.springframework.org/schema/mvc"
10     xmlns:util="http://www.springframework.org/schema/util"
11     xmlns:jpa="http://www.springframework.org/schema/data/jpa"
12     xsi:schemaLocation="
13         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
14         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
15         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
16         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
17         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
18         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
19         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
20         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
21         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
22     <!-- 配置视图处理器 -->
23     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
24         <property name="prefix" value="/WEB-INF/"></property>
25         <property name="suffix" value=".jsp"></property>
26     </bean>
27 </beans>
View Code

最终配置结果

Spring MVC五大核心组件和配置
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" 
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context" 
 5     xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
 6     xmlns:jee="http://www.springframework.org/schema/jee" 
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xmlns:aop="http://www.springframework.org/schema/aop" 
 9     xmlns:mvc="http://www.springframework.org/schema/mvc"
10     xmlns:util="http://www.springframework.org/schema/util"
11     xmlns:jpa="http://www.springframework.org/schema/data/jpa"
12     xsi:schemaLocation="
13         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
14         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
15         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
16         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
17         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
18         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
19         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
20         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
21         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
22     <!-- 配置请求分发器,让请求和控制器之间建立一一对应关系 -->
23     <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
24         <property name="mappings">
25             <props>
26                 <prop key="/toHello.do">helloController</prop>
27             </props>
28         </property>
29     </bean>
30     <!-- 配置控制器 -->
31     <bean id="helloController" class="com.xcz.controller.ToHelloController"></bean>
32     <!-- 配置视图处理器 -->
33     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
34         <property name="prefix" value="/WEB-INF/"></property>
35         <property name="suffix" value=".jsp"></property>
36     </bean>
37 </beans>
View Code

最后开启服务,在浏览器上输入localhost:端口号/项目名/toHello.do,看到如下界面,说明配置成功

Spring MVC五大核心组件和配置