初识Spring MVC

1.什么是Spring MVC?

Spring MVC属于SpringFrameWork的后续产品,它提供了构建 Web 应用程序的全功能 MVC 模块,与Struts2一样是一种优秀MVC框架,不同的是自Spring2.5引入了注解式controller及Spring 3以后的不断完善,使得采用Spring MVC框架开发结构清晰明了,效率大大提高。

度娘说:Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架,如Struts1,Struts2等。

2.Spring MVC请求流程

图一:

图二:

主要流程解释:

Spring MVC中前端的控制器就是DispatcherServlet这个Servlet来掌管着用户的请求及最后的系统回应。这个DispatcherServlet同具体的业务逻辑一点都不着边,而是把所有的事情委派给控制器去做(Controller),当然DispatcherServlet是知道该把当前的事情交个那个控制器去做;然后当控制器把事情都做完了后,这个时候轮到视图(View)上场了,简单的理解好比我们做PPT,那么这里的视图好比PPT里面的模板,它可以把数据以不同的展现形式交给客户,可以是jsp、xml、json等等。

3.Spring MVC的一个简单实例

步骤与源码介绍:

1.引入我们需要的jar包

  在原有Spring jar包基础上添加2个jar包

   1.

   包含支持UI模板,邮件服务,缓存Cache等方面的类

   2.

    对Spring mvc的实现(Spring MVC的核心包)

2.在web.xml中注册中央调度器

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name></display-name>

    <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启动就初始化 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>
View Code

3.定义自己的处理器控制器

package cn.zhang.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 MyController implements Controller  {

    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ModelAndView mv=new ModelAndView();
        mv.addObject("m","呵呵");
        mv.setViewName("WEB-INF/jsp/one.jsp");    
        //返回model或视图
        return mv;
    }

}
View Code

4.在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: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">
    <!-- 配置视图解析器 -->
    <bean id="/hello.do" class="cn.zhang.controller.MyController"></bean>

</beans>
View Code

5.在WEB-INF/jsp/one.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>我的第一个SpringMVC</title>
  </head>
  <body>
    Hello!friends!
  </body>
</html>
View Code

6.在浏览器输入http://localhost:8080/SpringMvc_one/hello.do访问

效果:

原文地址:https://www.cnblogs.com/zhangzongle/p/6039471.html