初识SpringMVC

一、MVC简介 

Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发,Spring Web MVC也是要简化我们日常Web开发的 Servlet进行Web开发。

二、MVC的设计模式

初识SpringMVC

 三、例题

  1、使用普通方式配置

 (1)导入SpringMVC依赖

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.2.0.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>4.2.0.RELEASE</version>
</dependency>

  (2)创建编程控制器类

package com.springmvc.controller;

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

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

/**
 * 编程控制器
 */
public class FirstController implements Controller {
    /**
     * @param httpServletRequest    请求对象
     * @param httpServletResponse   响应对象
     * @return ModelAndView Model是用来传递数据用的   View就是所需要跳转的页面
     * @throws Exception
     */
    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView mv=new ModelAndView();
        //携带给页面数据
        mv.addObject("user","张三");
        //指定跳转页面地址
        mv.setViewName("index");
        return mv;
    }
}

  (3)创建applicationContext.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:aop="http://www.springframework.org/schema/aop"
       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 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 <!--将Controller注入到容器当中   id就是浏览器请求地址-->
    <bean id="/firstController" class="com.springmvc.controller.FirstController"></bean>
</beans>

  (4)配置Web文件

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  <!--中央调度器-->
  <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:applicationContext-mvc.xml</param-value>
    </init-param>
    <!--控制初始化时机-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

  (5)编写index.jsp页面

<%@page language="java" pageEncoding="UTF-8" contentType="text/html; UTF-8" isELIgnored="false" %>
<html>
<body>
<h2>SpringMVC第一个案例</h2>
<img src="/img/liubin.png">
${user},I like SpringMVC, I‘m coming
</body>
</html>

  (6)运行结果

  初识SpringMVC

  2、使用注解方式配置

  (1)创建编程控制器类

package com.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SecondController {
    @RequestMapping("/firstRequest")
    public ModelAndView firstRequest(){
        ModelAndView mv=new ModelAndView();
        //携带给页面数据
        mv.addObject("user","还迷呢");
        //指定跳转页面地址
        mv.setViewName("index");
        return mv;
    }

    @RequestMapping("/secondRequest")
    public ModelAndView secondRequest(){
        ModelAndView mv=new ModelAndView();
        //携带给页面数据
        mv.addObject("user","弟弟");
        //指定跳转页面地址
        mv.setViewName("index");
        return mv;
    }
}

  (2)修改applicationContext.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:aop="http://www.springframework.org/schema/aop"
       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 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 <!--配置包扫描器-->
    <context:component-scan base-package="com.springmvc"/>
    <!--Spring支持SpringMVC-->
    <mvc:annotation-driven/>
</beans>

  (3)运行结果如下

    初识SpringMVC

   3、拦截图片的三种方式

  (1)在web文件中配置

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.gif</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
  </servlet-mapping>

(2)在applicationContext.xml文件中从Spring3.0.4版本提供资源放行的方式

<mvc:resources mapping="/**" location="/img"/>

(3)在applicationContext.xml文件中利用DefaultServlet放行资源

<mvc:default-servlet-handler/>

相关推荐