SpringMVC初步学习

①DispatcherServlet(请求分配器,在web.xml中配置)

②HandlerMapping(请求映射器,eg:SimpleUrlHandlerMapping)

③Controller

④ViewResolver(View)(结果视图解析器,eg:UrlBasedViewResolver)

SpringMVC 的工作流程:

    某些请求(eg:以“.do”结尾的请求)通过①进行分发给③处理,分发时是通过②所配置的映射表进行的;③处理完之后,返回一个ModelAndView对象(其中包含了视图逻辑字符串,类似于struts2),再经过④的解析(前后缀拼接),响应视图。

<?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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <!-- 对处理请求和controller进行映射 -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="start.do">startController</prop>
            </props>
        </property>
    </bean>
    
    <!-- controller的配置 -->
    <bean id="startController" class="com.yawn.controller.StartController"></bean>
    
    <!-- 对视图分派器进行配置 -->
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
        <!-- 最后展示的页面是/WEB-INF/jsp/start.jsp -->
    </bean>

</beans>
package com.yawn.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class StartController implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        System.out.println("----->>>>>>>>>>>>---");
        return new ModelAndView("start");
    }

}


你可能感兴趣的:(springMVC)