SpringMVC源码(一):SpringMVC介绍使用

Spring官网的MVC模块介绍:

SpringWebMVC是基于ServletAPI构建的原始Web框架,从一开始就已包含在Spring框架中。正式名称“SpringWebMVC”来自其源模块的名称(spring-webmvc),但它通常被称为“SpringMVC”

  1. 从Servlet到SpringMVC

    最典型的MVC就是JSP+servlet+javabean的模式

  2. 传统Servlet:SpringMVC源码(一):SpringMVC介绍使用
    缺点

    1. xml配置 开发效率低
    2. 必须继承父类、重写方法 侵入强
    3. 同一个Servlet中处理模块功能分发方法繁琐
    4. 参数解析 和 数据响应 麻烦(pojo<->json)
    5. 调整页面麻烦

SpringMVC 是在Servlet的基础上做了封装

SpringBoot 是在SpringMVC的基础上做了封装

SpringMVC 使用

src/main/webapp/WEB-INF/web.xml

<?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">

    <!--spring 基于web应用的启动-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--全局参数:spring配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-core.xml</param-value>
    </context-param>


    <!--配置前端控制器、核心调度器 加载spring容器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <!--/ 除了jsp所有请求都会被匹配-->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

spring配置文件 src/main/resources/spring-core.xml

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

    <!--扫描所有除了controller包的其他包-->
    <context:component-scan base-package="com.mx.xml">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>
``

springmvc配置文件 src/main/resources/spring-core.xml
```xml
<?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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--扫描包-->
    <context:component-scan base-package="com.mx.xml"></context:component-scan>

    <mvc:annotation-driven></mvc:annotation-driven>

    <!--默认视图解析器 -  配上前缀和后缀  简化 逻辑视图名称-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" name="viewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

   <!--
   <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
        <property name="mappings">
            <props>
                <prop key="/simpleController">simpleController</prop>
            </props>
        </property>
    </bean>

    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                &lt;!&ndash; json转换器 属于HandlerAdapter,单独配置是没用的&ndash;&gt;
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
    -->
</beans>

controlller - 方式1 继承 org.springframework.web.servlet.mvc.Controller

@Component
public class SimpleController implements Controller {

    @Override 
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

        System.out.println("SimpleController Working.");
        ModelAndView modelAndView = new ModelAndView("a");
        modelAndView.addObject("source","SimpleController");
        return modelAndView;
    }
}

controlller - 方式2 继承 org.springframework.web.servlet.mvc.HttpRequestHandler

@Component("/httpRequestHandler")
public class HttpRequestController implements HttpRequestHandler {
    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("HttpRequestController.........");
        //为返回页面设置数据
        request.setAttribute("source", "HttpRequestController");
        //设置返回页面
        request.getRequestDispatcher("/WEB-INF/jsp/a.jsp").forward(request, response);

    }
}

controlller - 方式3 注解方式

@Controller
@RequestMapping("/request")
public class RequestMappingController {

    @RequestMapping("/mapping")
    public ModelAndView mapping(){
        ModelAndView modelAndView = new ModelAndView("a");
        modelAndView.addObject("source","RequestMappingController");
        return modelAndView;
    }
}
上一篇:SpringMVC工作原理


下一篇:springmvc-02-注解开发