SpringMVC工作原理(含案例图解)

SpringMVC工作原理(含案例图解)SpimgMVC工作原理第1步:浏览器发送指定的请求都会交给DispatcherServlet,他会委托其他模块进行真正的业务和数据处理第2步:DispatcherServlet会查找到HandleMapping,根据浏览器的请求找到对应的Controller,并将请求交给目标Controller第3步:目标Controller处理完业务后,返回一个ModelAndView给Dispa…

大家好,又见面了,我是你们的朋友全栈君。

SpimgMVC工作原理

第1步:浏览器发送指定的请求都会交给DispatcherServlet,他会委托其他模块进行真正的业务和数据处理
第2步:DispatcherServlet会查找到HandleMapping,根据浏览器的请求找到对应的Controller,并将请求交给目标Controller
第3步:目标Controller处理完业务后,返回一个ModelAndView给DispatcherServlet
第4步:DispatcherServlet通过ViewResolver视图解析器找到对应的视图对象View
第5步:视图对象View负责渲染,并返回到浏览器

案例分析

下面通过案例图解的方式理解下上面的工作原理

第1步-浏览器请求

这里写图片描述


第2、3步-找到对应Controller

这里写图片描述


第4、5步-解析视图对象,返回浏览器

这里写图片描述


浏览器结果

这里写图片描述

源码

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    <display-name>springmvc</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <!-- spring入口 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 项目启动时,就加载并实例化 -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- 拦截所有不包括jsp的请求 -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

</web-app>

springmvc-servlet.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:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

        <!-- springmvc注解驱动 -->
        <mvc:annotation-driven></mvc:annotation-driven>

        <!-- 开启注解扫描 -->
        <context:component-scan base-package="cn.itcast.controller"></context:component-scan>

        <!-- 配置试图解析器 prefix:指定试图所在目录 suffix:指定视图的后缀名 例如:prifex="/WEB-INF/jsp/",suffix=".jsp",当viewname="test"时,跳转到/WEB-INF/jsp/test.jsp页面 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
</beans>

UserControll类

@Controller
@RequestMapping("user")
public class UserController { 
   
    @RequestMapping("findAllUsers")
    public ModelAndView findAllUsers() {
        ModelAndView mv = new ModelAndView();
        ArrayList<User> users = new ArrayList<User>();
        for (int i = 0; i < 5; i++) {
            User user = new User();
            user.setUsername("zs" + i);
            user.setAge(20 + i);
            user.setIncome(16000.0+i*100);
            user.setIsMarry(false);
            user.setHobby(new String[] { "篮球"+i, "足球"+i });
            users.add(user);
        }
        mv.addObject("users", users);
        mv.setViewName("users");
        return mv;
    }
}

实体类


public class User implements Serializable { 
   

    /** * */
    private static final long serialVersionUID = 1L;

    private String username;
    private Integer age;
    private Boolean isMarry;
    private Double income;
    private String[] hobby;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Boolean getIsMarry() {
        return isMarry;
    }

    public void setIsMarry(Boolean isMarry) {
        this.isMarry = isMarry;
    }

    public Double getIncome() {
        return income;
    }

    public void setIncome(Double income) {
        this.income = income;
    }

    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

    @Override
    public String toString() {
        return "User [username=" + username + ", age=" + age + ", isMarry=" + isMarry + ", income=" + income
                + ", hobby=" + Arrays.toString(hobby) + "]";
    }

}

JSP页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <link rel="stylesheet" type="text/css" href="/css/user.css" /> </head> <body> <table id="customers"> <tr> <th>用户名</th> <th>年龄</th> <th>收入</th> <th>婚姻状态</th> <th>兴趣爱好</th> </tr> <!-- 遍历后台传递的集合数据 --> <c:forEach items="${users}" var="user"> <tr> <td>${user.username}</td> <td>${user.age}</td> <td>${user.income}</td> <!-- 判婚姻状态 --> <td><c:choose> <c:when test="${user.isMarry}">已婚</c:when> <c:otherwise>未婚</c:otherwise> </c:choose> </td> <td> <!-- 再次遍历用户爱好 --> <c:forEach items="${user.hobby}" var="hobby" varStatus="status"> ${hobby} <!-- 如果不是最后一个爱好,则加上逗号,否则就不加 --> <c:if test="${!status.last}">,</c:if> </c:forEach> </td> </tr> </c:forEach> </table> </body> </html>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/146412.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 关于PreferenceActivity的使用和一些问题的解决(自己定义Title和取值)

    关于PreferenceActivity的使用和一些问题的解决(自己定义Title和取值)

    2021年11月28日
    41
  • sublime text3的激活码【中文破解版】

    (sublime text3的激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年4月2日
    96
  • 关于G1收集器

    关于G1收集器G1(GarbageFirst)收集器是Oracle公司开发的一款主要面向服务端的拥有相对可靠的停顿预测模型的垃圾收集器。在垃圾收集器的历史上有着里程碑式的意义。与之前的收集器不同,G1不在基于固定的新生代与老年代的内存分配方式进行垃圾清理,而是使用了基于Region的内存分配的方式进行垃圾清理。这种方式使得G1在进行垃圾清理的时候不需要对整个新生代或老年代甚至整个Java堆进行垃圾清理,这样就极大的减少标记期间的停顿时间。设计思路:面向局部(单个或多个Region)收集内存布局:基于Regi

    2022年5月20日
    31
  • Python3 实例–Python 计算圆的面积

    Python3 实例–Python 计算圆的面积#代码如下:#Python3实例–Python计算圆的面积print(“Python3实例–Python计算圆的面积”)#公式中r为圆的半径。r=float(input())PI=3.14s=PI*(r**2)print(“圆的面积为:{}”.format(s))#运行结果如下:Python3实例–Python计算圆的面积3圆的面积为:…

    2025年6月5日
    0
  • MPLS 虚拟专用网络技术原理与配置

    MPLS 虚拟专用网络技术原理与配置

    2021年4月13日
    165
  • ssh隧道代理上网_ssh加密算法

    ssh隧道代理上网_ssh加密算法putty可以很轻易地建立ssh隧道,实现加密代理。这个方法你需要有一台外部的sshd服务器。在自己的电脑上利用putty连接sshd服务器,建立ssh隧道。在putty中设置连接时选择左侧的SSH->Tunnel,Sourceport为隧道的本地端口,例如填写1080,Destination留空,下方选择Dynamic,点Add按钮。设置如下图所示。然后连接sshd

    2022年9月9日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号