Spring MVC五大核心组件和配置
2018-07-16 02:39:50来源:博客园 阅读 ()
一,五大核心组件
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,看到如下界面,说明配置成功
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Spring系列.ApplicationContext接口 2020-06-11
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- 给你一份超详细 Spring Boot 知识清单 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash