Spring MVC五大核心组件和配置

一,五大核心组件

  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

如图:

配置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>
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>

配置HandlerMapping

 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>

配置ViewResolver

 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>

最终配置结果

 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>

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

原文地址:https://www.cnblogs.com/resultset/p/9311255.html

时间: 2024-10-22 00:02:22

Spring MVC五大核心组件和配置的相关文章

spring mvc+hibernate的基本配置

<?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:p="http://www.springframework.org/schema/p&

Spring MVC 使用tomcat中配置的数据源

Spring MVC 使用tomcat中配置的数据源 配置tomcat数据源 打开tomcat目录下的conf目录,编辑sever.xml目录.在<GlobalNamingResources>标签中添加数据源配置: <Resource name="jdbc/dbsourse" scope="Shareable" type="javax.sql.DataSource" factory="org.apache.tomcat

Spring MVC的web.xml配置详解(转)

出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在web.xml配置监听器ContextLoaderListener(listener-class) ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web.

spring mvc 返回json的配置

转载自:http://my.oschina.net/haopeng/blog/324934 springMVC-servlet.xml 配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

Spring MVC环境搭建和配置

1. 创建Dynamic web project 2. 修改WEB-INF/web.xml,内容如下: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-i

Spring MVC 3.x 基本配置

WEB-INF/web.xml 例1 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.co

spring mvc超强的json支持,你自己根本不需要额外的配置。spring mvc都给你配置好了!!!

SpringMVC层跟JSon结合,几乎不需要做什么配置,代码实现也相当简洁.再也不用为了组装协议而劳烦辛苦了! 2.一.Spring注解@ResponseBody,@RequestBody和HttpMessageConverter Spring 3.X系列增加了新注解@ResponseBody,@RequestBody @RequestBody 将HTTP请求正文转换为适合的HttpMessageConverter对象. @ResponseBody 将内容或对象作为 HTTP 响应正文返回,并

spring mvc 2.5.6配置

兼容公司老版本号项目.必须得用spring mvc2.5.6,那么问题来了. 怎么配置controller都抛出no mapping的错误.经过查文档得出下面配置.仅供參考. servlet-config.xml <? xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=

spring mvc 中web.xml配置信息解释

在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter.最终得出的结论是:listener -> filter -> servlet 同时还存在着这样一种配置节:context-param,它用于向 Servlet