相识不易,要懂珍惜----------Spring Mvc

一、Spring Mvc简介

    Spring Mvc也叫Spring Web Mvc,属于表现层额框架。Spring Mvc是Spring框架的一部分,是在Spring3.0后发布的。

二、Spring的架构图

相识不易,要懂珍惜----------Spring Mvc

  解析:就是在Spring架构中Spring Web Mvc是属于Web模块的一部分。

三、Spring Mvc请求流程

相识不易,要懂珍惜----------Spring Mvc

文字描述

相识不易,要懂珍惜----------Spring Mvc

四、案列书写

  DispatcherServlet(*调度器)、Contrller(处理器)、View(视图)-------------->需要手动编写代码

  HandleMapping(处理器映射器)、HandleAdaptor(处理器适配器)、ViewResolver(试图解析器)------------>不需要配置,因为它在spring-web-4.2.0.RELEASE.jar中的相识不易,要懂珍惜----------Spring Mvc配置里面有系统给的默认的配置。

  项目所有的jar

    相识不易,要懂珍惜----------Spring Mvc

  1、在web.xml中配置   

<!-- 配置*调度器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定配置文件applicationContext.xml -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <!--Tomcat启动的时候,Servlet对象已经到内存!!!! >0数字 0或者是负数,和你没有设置是一样的 -->
        <load-on-startup>1</load-on-startup>
    </servlet>


    <servlet-mapping>
        <!-- 这个名字可以任意取 -->
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

  2、创建一个*调度器(前端控制器)cn.happy.yxj.controller的包下

import javax.naming.ldap.Control;
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 MyController implements Controller{

    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        ModelAndView mv=new ModelAndView();
        mv.addObject("mag", "第一个项目SpringMvc");

      //原始
      /*mv.setViewName("WEB-INF/jsp/index.jsp");*/
      //视图解析器分为前缀和后缀
      mv.setViewName("index");

     return mv;
    }

}

  3、在配置中配置一个处理器

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

      <!--WEB-INF/jsp/ 视图解析器 -->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <!-- 前缀 -->
      <property name="prefix" value="WEB-INF/jsp/"></property>
      <!-- 后缀 -->
      <property name="suffix" value=".jsp"></property>
      </bean>
      <!-- 注册一个控制器-->
      <bean ></bean>

</beans>

  4、创建一个普通的view视图就是一个index.jsp

  5、将Tomcat启动在页面显示你的文字就代表你的配置成功了!!!!

浅浅的理解,待等更新......