SpringMvc五大组件 执行流程 编写步骤

五大组件

1 DispatcherServlet 控制器 请求的入口

2 HandlerMapping 控制器 请求的派发

3 Controller 控制器 处理具体业务

4 ModelAndView 模型和视图 封装数据信息视图信息

5 ViewResolver 视图处理器 将模型和视图返回对应 页面

执行流程

DispatcherServlet 接受所有请求 根据 不同的请求给到 HandlerMapping 对请求以及控制器做一一对应的关系 根据某个请求 对应到 Controller Controller业务处理后生成对应的ModelAndView 给到 ViewResolver 返回对应的页面以及数据

编写步骤

1 导入 ioc mvc 包
2 在web.xml中编写 DispatcherServlet 控制器 接受请求
3 在spring配置文件中写 HandlerMapping 以键值对的形式 写入 请求路径 以及类名
4 编写controller类写对应的业务逻辑
5 在Spring配置文件中写ViewResolver 组件信息 属性 包括前缀路径 后缀写 对应的 文件后缀

在web.xml中编写 DispatcherServlet 控制器 接受请求

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
    //控制器的名称
        <servlet-name>Mvc</servlet-name>
        //控制器的类路径
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
        //控制器的属性 在spring中定义
            <param-name>contextConfigLocation</param-name>
            //定义在类路径下 app.xml中
            <param-value>classpath:app.xml</param-value>
        </init-param>
    </servlet>
    
    <servlet-mapping>
    
        <servlet-name>Mvc</servlet-name>
        //接收所有.do请求
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

在spring配置文件中配置 HandlerMapping 以键值对的形式 写入 请求路径 以及类名 还有 ViewResolver

<?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:context="http://www.springframework.org/schema/context" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
	

//配置请求派发器  作用是将请求以及处理请求的控制器做到对应的关系
	<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		//属性值  mappings
		<property name="mappings">
			<props>
			//键值对的形式写入 请求地址以及 对用的controller类
				<prop key="/toHello.do" >helloController</prop>
			</props>
		</property>
	</bean>

//具体的Controller类
	<bean id="helloController" class="com.test.TestController">
	</bean>

//ViewResolver 将模型和视图返回对应的页面 
	<bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	//后缀属性 对应的什么文件
		<property name="suffix" value=".html"></property>
		//前缀对应的文件地址
		<property name="prefix" value="/WEB-INF/"></property>
	</bean>
</beans>

编写Controller类 需要实现Controller接口


public class TestController implements Controller {
    @Override
    public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
    //生成模型和视图
        ModelAndView mov = new ModelAndView();
        //写入视图名称 ViewResolver会将当前的名称 合并到他对应的前缀后缀 读取文件
        mov.setViewName("hello");
        return mov;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值