SpringMVC概述、SpringMVC 的入门

news2024/9/20 17:00:56

1.MVC介绍

 MVC是一种设计模式,将软件按照模型、视图、控制器来划分:

  • M:Model,模型层,指工程中的JavaBean,作用是处理数据

    JavaBean分为两类:

    • 一类称为数据承载Bean:专门存储业务数据的,如 Student、User 等

    • 一类称为业务处理Bean:指 Service 或 Dao 对象,专门用于处理业务逻辑和数据访问。

  • V:View,视图层,指工程中的html或jsp等页面,作用是与用户进行交互,展示数据

  • C:Controller,控制层,指工程中的servlet,作用是接收请求和响应浏览器

MVC的工作流程:

 用户通过视图层发送请求到服务器,在服务器中请求被Controller接收,Controller调用相应的Model层处理请求,处理完毕将结果返回到Controller,Controller再根据请求处理的结果找到相应的View视图,渲染数据后最终响应给浏览器

MVC与三层架构的关系:

三层架构的分层模式是典型的上下关系,上层依赖于下层。但MVC作为表现模式是不存在上下关系的,而是相互协作关系。

三层是基于业务逻辑来分的,而mvc是基于页面来分的。

2.Spring MVC介绍

  • Spring MVC 是Spring框架的一个模块,是一个基于 MVC 设计模式的轻量级 Web 开发框架,本质上相当于 Servlet。

  • SpringMVC 是 Spring 为表示层开发提供的一整套完备的解决方案。在表述层框架历经 Strust、WebWork、Strust2 等诸多产品的历代更迭之后,目前业界普遍选择了 SpringMVC 作为 Java EE 项目表述层开发的首选方案。 

 3.SpringMVC 的入门

3.1环境搭建

 3.1.1创建工程

 3.1.2添加web支持

1.右键项目选择Add framework support...  

 2.添加web支持

 3.效果

  • 注意:

    1. 不要先添加打包方式

    2. 将web目录要拖拽到main目录下,并改名为webapp

 3.1.3pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>SpringMVC</artifactId>
        <groupId>com.by</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SpringMVC_day01</artifactId>
    <!--打包方式-->
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
<!--        IOC依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
<!--        web依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
<!--servlet依赖-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- 配置Tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!--端口号-->
                    <port>8080</port>
                    <!--项目名-->
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 3.2入门案例

3.2.1index.jsp 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <a href="/hello">hello</a>
  </body>
</html>>

 3.2.2controller

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public ModelAndView hello() {
        //ModelAndView对象封装了模型数据和视图名称
        ModelAndView mv = new ModelAndView();
        //添加数据,request.setAttribute(“hello”,”hello springmvc!!”)
        mv.addObject("hello", "欢迎你 springmvc");
        //设置逻辑视图路径
        mv.setViewName("success");
        //返回数据和视图
        return mv;
    }
}

 3.2.3springmvc.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:p="http://www.springframework.org/schema/p"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 配置创建 spring 容器要扫描的包 -->
    <context:component-scan base-package="com.by"></context:component-scan>

    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--开启springmvc注解支持:配置HandlerMapping和HandlerAdapter-->
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

3.2.4 success.jsp

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
	<h2>${msg}</h2>
</body>
</html>

 3.2.5web.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">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置初始化参数,用于读取 SpringMVC 的配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <!-- 表示容器在启动时立即创建servlet对象 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

 3.2.6测试

访问:http://localhost:8080/hello

 4.总结

一、Spring MVC的介绍
    1、mvc设置模式的介绍
        M(model):模型层,
                 数据模型:User、Student,装数据
                 业务模型:service、dao,负责处理业务
        V(view):视图层,html和jsp,负责和用户交互
        C(controller):控制层,servlet,负责接受请求和响应
    2、springmvc的介绍
        springmvc是spring框架的一个模块,是一个基于mvc设计模式的web框架,本质上相当于servlet
二、springmvc入门案例
        1.index.jsp
            <a href="/hello">hello</a>
        2.controller
            @Controller
            public class HelloControler{

                @RequestMapping("/hello")
                public ModelAndView hello(){
                    ModelAndView mv = new ModelAndView();
                    mv.addObject("msg", "欢迎你Spring MVC");
                    mv.setViewName("success");
                    return mv;
                }
            }
        3、配置springmvc要扫描的包
            <context:component-scan base-package="com.by.controller"></context:component-scan>
        4、配置视图解析器
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <!--jsp的目录-->
                    <property name="prefix" value="/WEB-INF/pages/"></property>
                    <!--jsp的扩展名-->
                    <property name="suffix" value=".jsp"></property>
            </bean>
        5、web.xml
            <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:springmvc.xml</param-value>
                </init-param>
                <!--tomcat启动就创建加载DispatcherServlet-->
                <load-on-startup>1</load-on-startup>
            </servlet>
            <servlet-mapping>
                <servlet-name>springmvc</servlet-name>
                <!--/只能拦截路径,而 / *能够拦截路径和页面-->
                <url-pattern>/</url-pattern>
            </servlet-mapping>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1370047.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

12、DolphinScheduler

1、DolphinScheduler简介 1.1、 DolphinScheduler概述 Apache DolphinScheduler是一个分布式、易扩展的可视化DAG工作流任务调度平台。致力于解决数据处理流程中错综复杂的依赖关系&#xff0c;使调度系统在数据处理流程中开箱即用。 1.2、 DolphinScheduler核心架构 Dolph…

Unity 踩坑记录 AnyState 切换动画执行两次

AnySate 切换动画 Can Transition To Self 将这个勾选去掉&#xff01;&#xff01;&#xff01;

九州金榜如何让孩子在家庭教育中更优秀

​ 每个人在出生时就有上天恩赐的两份礼物&#xff0c;一份是血脉相连的亲情&#xff0c;一份是家庭的关爱与教育。 最早接触的人就是父母&#xff0c;最早接触的教育就是家庭教育&#xff0c;这对孩子的影响极为深远。 这种家庭教育相比较学校教育&#xff0c;不仅有言传教…

springCould中的Config-从小白开始【10 】

目录 &#x1f32d;1.spring cloud Config是什么&#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️ &#x1f953;2.能干什么&#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️ &am…

借用GitHub将typora图片文件快速上传CSDN

前情概要 众所周知&#xff0c;程序员大佬们喜欢用typora软件写代码笔记&#xff0c;写了很多笔记想要放到CSDN上给其他大佬分享&#xff0c;但是在往csdn上搬运的时候&#xff0c;图片总是上传出错&#xff0c;一张一张搞有很麻烦&#xff0c;咋如何搞&#xff1f; 废话不多…

conda新建、配置python3.8虚拟环境,torch-cuda1.8,torchtext0.9.0,huggingface安装transformers库

起因是我在用bert的时候&#xff0c;导包报错 Python 环境缺少 importlib.metadata 模块。importlib.metadata 是 Python 3.8 引入的模块&#xff0c;而我的环境中使用的 Python 版本为 3.7。所以我得重新配置一个python3.8的环境 准备工作 在开始菜单找到anaconda prompt(an…

唠一唠Java线程池

第1章&#xff1a;引言 大家好&#xff0c;我是小黑&#xff0c;咱们今天来聊聊Java线程池&#xff0c;如果没有线程池&#xff0c;每个线程都需要手动创建和销毁线程&#xff0c;那将是多么低效和耗资源啊&#xff01; 线程池的核心作用就是复用已创建的线程&#xff0c;减少…

什么是MOM,与MES系统的差异是什么

MOM基本概念介绍 由于绝大多数MES只关注生产执行的核心作用, 对维护管理、质量管理和库存管理的重视程度有限,有时甚至缺少这部分功能, 并且未提高到与生产运营相类似的复杂程度, 难以充分满足现代制造企业对其制造运营区域的业务管理需求, 进而直接影响对企业的运营管理效果。…

自带恒压恒流环路的降压型单片车充专用芯片

一、基本概述 XL2009是一款高效降压型DC-DC转换器&#xff0c;固定180KHz开关频率&#xff0c;可以提供最高2.5A输出电流能力&#xff0c;具有低纹波&#xff0c;出色的线性调整率与负载调整率特点。XL2009内置固定频率振荡器与频率补偿电路&#xff0c;简化了电路设计。 PWM …

AArch64 memory management学习(二)

提示 该博客主要为个人学习&#xff0c;通过阅读官网手册整理而来&#xff08;个人觉得阅读官网的英文文档非常有助于理解各个IP特性&#xff09;。若有不对之处请参考参考文档&#xff0c;以官网文档为准。AArch64 memory management学习一共分为两章&#xff0c;这是第二章。…

Transformer从菜鸟到新手(五)

引言 上篇文章我们在单卡上完成了完整的训练过程。 从本文开始介绍模型训练/推理上的一些优化技巧&#xff0c;本文主要介绍多卡并行训练。 下篇文章将介绍大模型推理常用的缓存技术。 多卡训练 第一个要介绍的是利用多GPU优化&#xff0c;因为在单卡上训练实在是太慢。这…

ORACLE索引失效和sql优化

全部都是在开发中碰到的真实问题&#xff0c;后续会一直更新本条帖子&#xff1a; 场景一&#xff1a; 使用了函数&#xff0c;导致索引失效 交易日期过滤数据时使用了to_date写法&#xff0c;导致日期字段查询未走索引 将控制台输出的sql 粘贴到plsql 按F5打开解释计划执行窗口…

【Android】 ConstraintLayout实操

由于最近比较悠闲&#xff0c;重新学习了constraintlayout&#xff0c;看着官网学的&#xff0c;官网网站如下&#xff1a;https://developer.android.com/training/constraint-layout?hlzh-cn#alignment 其实之前也小小的学过一波constraintlayout&#xff0c;不过因为用线性…

C#,入门教程(13)——字符(char)及字符串(string)的基础知识

上一篇&#xff1a; C#&#xff0c;入门教程(12)——数组及数组使用的基础知识https://blog.csdn.net/beijinghorn/article/details/123918227 字符串的使用与操作是必需掌握得滚瓜烂熟的编程技能之一&#xff01;&#xff01;&#xff01;&#xff01;&#xff01; C#语言实…

Copilot 插件的使用介绍:如何快速上手

GitHub Copilot 本文主要介绍如何通过脚本工具激活 GitHub Copilot 插件&#xff0c;提供安装及激活图文教程&#xff0c;大家按下面操作即可激活GitHub Copilot插件&#xff0c;免费使用Ai编码工具 一、GitHub Copilot 介绍 GitHub Copilot 是由 GitHub 和 OpenAI 共同开发的…

Namp端口扫描

在 CentOS 7 上安装 Nmap 的步骤如下&#xff1a; 打开终端&#xff1a; 作为 root 用户&#xff0c;您已经具备执行安装命令的权限。 使用 Yum 安装 Nmap&#xff1a; 在终端中&#xff0c;输入以下命令来安装 Nmap&#xff1a;sudo yum install nmap如果您已经是 root 用户&…

浅谈配电能源管理系统应用在钢铁行业

叶根胜 安科瑞电气股份有限公司 上海嘉定 201801 摘要&#xff1a;能源管理系统在钢铁行业的应用不仅是钢铁行业信息化、数据化、智能化发展的重要体现&#xff0c;也是钢铁行业实现智能制造和精细化管理的重要举措。有鉴于此&#xff0c;从能源管理系统在钢铁行业的应用意义…

计算机体系结构超标量及分支预测学习记录

1.CPIIdeal CPI Structural stalls Data hazard stalls Control stalls 超标量通过动态调度&#xff0c;进一步降低CPI&#xff0c;通过每个周期发射多条指令来执行 2.猜测执行就是为了在Tomasulo算法的基础上&#xff0c;进一步解决控制冲突&#xff1b; 猜测执行算法与T…

Python(32):字符串转换成列表或元组,列表转换成字典小例子

1、python 两个列表转换成字典 字符串转换成列表 列表转换成字典 column "ID,aes,sm4,sm4_a,email,phone,ssn,military,passport,intelssn,intelpassport,intelmilitary,intelganghui,inteltaitonei,credit_card_short,credit_card_long,job,sm4_cbc,sm4_a_cbc" …

Vue入门三(表单控制|购物车案例|v-model进阶|与后端交互)

文章目录 一、表单控制二、购物车案例三、v-model进阶四、与后端交互跨域问题解决&#xff0c;三种交互方法跨域问题详解1-CORS&#xff1a;后端代码控制&#xff0c;上面案例采用的方式1) 方式一&#xff1a;后端添加请求头2) 方式二&#xff1a;编写中间件3) 方式三&#xff…