初识SpringMVC(SpringMVC学习笔记一)

1 、还是熟悉的配方,先创建一个父Maven项目(忘记怎么创建项目了就去前面翻笔记),导入通用的配置依赖

<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>SprinMVC_Father</artifactId><packaging>war</packaging><version>1.0-SNAPSHOT</version><name>SprinMVC_Father Maven Webapp</name><url>http://maven.apache.org</url><dependencies><!--导入junit依赖方便后续测试      --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!--导入SpringMVC依赖    --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.1.9.RELEASE</version></dependency><!--导入servlet依赖    --><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><!--导入jsp依赖    --><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2</version></dependency><!--导入jsptl依赖    --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency></dependencies><build><finalName>SprinMVC_Father</finalName></build>
</project>

2、创建子项目,并确定子项目支持web项目

给子项目也导入servlet和jsp的依赖(双重导入确保不会出错,正常的话父项目导入了子项目不导入也可以,这里我们导入两次确保不会出问题)

 <!--导入servlet依赖    --><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><!--导入jsp依赖    --><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2</version></dependency>

3、创建一个helleservlet类继承httpservlet用来处理用户的请求,实现父类的doGet和doPost方法

package com.li.myservlet;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;public class HellServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1、获取前段参数String method = req.getParameter("method");if(method.equals("add")){req.getSession().setAttribute("msg","执行了add方法");}if(method.equals("delete")){req.getSession().setAttribute("msg","执行了delete方法");}//2、调用业务层 暂时没有业务成//3、视图转发或重定向req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}

顺便在web里写个测试页面

 Date: 2024/4/6Time: 16:45To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
${msg}
</body>
</html>

再写个表单测试

<%--Created by IntelliJ IDEA.User: AdministratorDate: 2024/4/6Time: 17:10To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>表单</title>
</head>
<body>
<form action="/hello" method="post">
<input type="text" name="method"><input type="submit" >
</form>
</body>
</html>

 3、写完servlet的第一件事情就是给它去web.xml中注册上

web.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"></web-app>

注册完后的web.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><servlet-name>hello</servlet-name><servlet-class>com.li.myservlet.HellServlet</servlet-class></servlet><servlet-mapping><servlet-name>hello</servlet-name><url-pattern>/hello</url-pattern></servlet-mapping><!--还有一些其他的配置        --><!--可以设置session的超时时间    这里设置15分钟    -->
<!--        <session-config>-->
<!--          <session-timeout>15</session-timeout>-->
<!--        </session-config>--><!--也可以在这里设置欢迎页面  默认的是 index.jsp     -->
<!--        <welcome-file-list>-->
<!--          <welcome-file>xxx.jsp</welcome-file>-->
<!--        </welcome-file-list>-->
</web-app>

 最后目录如下:

3、一切都写完了就配置tomcat

 

3、初识SpringMVC

SpringMVC的原理如下

 

 

4、对 SpringMVC有初步了解之后我们再来创建一次项目

第一步创建视图

 第二步:确保最后发布的项目中导入了SpringMVC的依赖

第三步:在web.xml配置文件中配置我们的DispatchServlet
<?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">
<!--配置DispatchServlet:这个是SpringMVC的核心:请求分发器,前端控制器--><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--DispatchServlet要绑定一个Spring-mvc的配置文件  其实就是spring的配置文件,只是spring可以做很多事情,我们细分下来叫Spring-mvc的配置文件--><init-param><param-name>contextConfigLocation</param-name><!--这里配置文件取名字就是按照上面的servlet-name的名字取的xxx-servlet   --><param-value>classpath:springmvc-servlet.xml</param-value></init-param><!--设置启动级别为1 --><load-on-startup>1</load-on-startup></servlet><!--SpringMVC中/ 和 /* 的区别/ :只匹配所有的请求,不会去匹配jsp页面/* :匹配所有的请求,包括jsp页面--><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>
第四步:编写springmvc的配置文件,其实就是spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--处理器映射器 处理器映射器不止有一种--><bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--处理器适配器 这个和上面的处理映射器在后期的开发中直接注解就可以实现,不用死记,知道流程就可以--><bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--视图解析器这个必须熟练掌握 后面还会学习一些模板引擎如 Thymeleaf Freemarker--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"><!--需要配置两个参数,一个前缀,一个后缀-->
<!--  前缀    --><property name="prefix" value="/WEB-INF/jsp/"/>
<!--  后缀   --><property name="suffix" value=".jsp"/></bean></beans>
第五步:编写Controller
package com.li.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 HelloController implements Controller {@Overridepublic ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {ModelAndView modelAndView = new ModelAndView();//       假设一个业务代码String result = "HelloSpringmvc";modelAndView.addObject("msg",result);//        视图跳转modelAndView.setViewName("test");return modelAndView;}
}

4、真实的开发:(一般不会像上面这样用)

第一步:配置web.xml配置文件
<!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><!--配置DispatchServlet:这个是SpringMVC的核心:请求分发器,前端控制器--><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--DispatchServlet要绑定一个Spring-mvc的配置文件  其实就是spring的配置文件,只是spring可以做很多事情,我们细分下来叫Spring-mvc的配置文件--><init-param><param-name>contextConfigLocation</param-name><!--这里配置文件取名字就是按照上面的servlet-name的名字取的xxx-servlet   --><param-value>classpath:springmvc-servlet.xml</param-value></init-param><!--设置启动级别为1 --><load-on-startup>1</load-on-startup></servlet><!--SpringMVC中/ 和 /* 的区别/ :只匹配所有的请求,不会去匹配jsp页面/* :匹配所有的请求,包括jsp页面--><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>
第二步:创建并配置springmvct配置文件 
<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 --><context:component-scan base-package="com.li.controller"/><!-- 让Spring MVC不处理静态资源 --><mvc:default-servlet-handler /><!--支持mvc注解驱动在spring中一般采用@RequestMapping注解来完成映射关系要想使@RequestMapping注解生效必须向上下文中注册DefaultAnnotationHandlerMapping和一个AnnotationMethodHandlerAdapter实例这两个实例分别在类级别和方法级别处理。而annotation-driven配置帮助我们自动完成上述两个实例的注入。--><mvc:annotation-driven /><!-- 视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"id="internalResourceViewResolver"><!-- 前缀 --><property name="prefix" value="/WEB-INF/jsp/" /><!-- 后缀 --><property name="suffix" value=".jsp" /></bean></beans>
第三步:创建controlle类
package com.li.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
//@Controller是为了让Spring IOC容器初始化时自动扫描到;
//@RequestMapping是为了映射请求路径,这里因为类与方法上都有映射所以访问时应该是/HelloController/hello;@Controller@RequestMapping("/HelloController")public class HelloController {//真实访问地址 : 项目名/HelloController/hello@RequestMapping("/hello")
//        方法中声明Model类型的参数是为了把Action中的数据带到视图中;public String sayHello(Model model){//向模型中添加属性msg与值,可以在JSP页面中取出并渲染model.addAttribute("msg","hello,SpringMVC");
//            方法返回的结果是视图的名称hello,加上配置文件中的前后缀变成WEB-INF/jsp/hello.jsp。return "hello";}}
第四步:常见视图层(注意要在springmvct配置文件中配置的路径下创建视图层)

这里的路径是:/WEB-INF/jsp/

<%--Created by IntelliJ IDEA.User: AdministratorDate: 2024/4/13Time: 16:29To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
${msg}
</body>
</html>

总结:

实现步骤其实非常的简单:

  1. 新建一个web项目
  2. 导入相关jar包
  3. 编写web.xml , 注册DispatcherServlet
  4. 编写springmvc配置文件
  5. 接下来就是去创建对应的控制类 , controller
  6. 最后完善前端视图和controller之间的对应
  7. 测试运行调试.

使用springMVC必须配置的三大件:

处理器映射器、处理器适配器、视图解析器

通常,我们只需要手动配置视图解析器,而处理器映射器处理器适配器只需要开启注解驱动即可,而省去了大段的xml配置

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

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

相关文章

Vitis HLS 学习笔记--ap_int.h / ap_fixed.h(1)

目录 目录 1. 概述 2. 简要规则 3. 浮点运算的复杂性 2.1 对阶 3.2 尾数运算 3.3 规格化和舍入 3.4 特殊值的处理 4. 示例&#xff08;ap_fixed.h&#xff09; 5. 量化模式&#xff08;ap_fixed.h&#xff09; 5.1 AP_SAT* 模式会增加资源用量 1. 概述 ap_int.h 和…

如何将三方库集成到hap包中——通过IDE集成cmak构建方式的C/C++三方库

简介 cmake构建方式是开源三方库的主流构建方式。DevEco Studio目前以支持cmake的构建方式。本文将通过在IDE上适配cJSON三方库为例讲来解如何在IDE上集成cmake构建方式得三方库。 创建工程 在开发进行三方库适配以及napi接口开发前&#xff0c;我们需要创建一个三方库对应的…

【opencv】示例-points_classifier.cpp 使用不同机器学习算法在二维空间中对点集进行分类...

#include "opencv2/core.hpp" // 包含OpenCV核心功能的文件 #include "opencv2/imgproc.hpp" // 包含OpenCV图像处理功能的文件 #include "opencv2/ml.hpp" // 包含OpenCV机器学习模块的文件 #include "opencv2/highgui.hpp" // 包含O…

【vue】slot 匿名插槽 / 具名插槽

slot父组件向子组件传递数据 匿名插槽–直接写 具名插槽–指定名称 父组件中 子组件中&#xff1a; 代码 App.vue <template><h2>App.vue</h2><!-- 匿名插槽 --><Header><a href"1234567890.com">1234567890</a>&…

LLM大语言模型微调方法和技术汇总

本文详细介绍了机器学习中的微调技术&#xff0c;特别是在预训练模型上进行微调的方法和技术。文章首先解释了什么是微调&#xff0c;即在预训练模型的基础上&#xff0c;通过特定任务的数据进行有监督训练&#xff0c;以提高模型在该任务上的性能。随后&#xff0c;详细介绍了…

C++格式化输出开源库fmt入手教程

fmt项目快速上手指南 1. cmake环境配置 include(FetchContent) FetchContent_Declare(fmtGIT_REPOSITORY https://github.com/fmtlib/fmtGIT_TAG 10.0.0GIT_SHALLOW TRUE) # 1. 下载fmt库 FetchContent_MakeAvailable(fmt)add_executable(fmt_guide main.cpp) # 2. 链接fmt库…

【opencv】示例-minarea.cpp 如何寻找一组随机生成的点的最小外接矩形、三角形和圆...

// 包含OpenCV库的高GUI模块和图像处理模块的头文件 #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp"// 包含标准输入输出流的头文件 #include <iostream>// 使用命名空间cv和std&#xff0c;这样我们就可以直接使用OpenCV和标准库的…

Android开发:Camera2+MediaRecorder录制视频后上传到阿里云VOD

文章目录 版权声明前言1.Camera1和Camera2的区别2.为什么选择Camera2&#xff1f; 一、应用Camera2MediaPlayer实现拍摄功能引入所需权限构建UI界面的XMLActivity中的代码部分 二、在上述界面录制结束后点击跳转新的界面进行视频播放构建播放界面部分的XMLActivity的代码上述代…

WebLogic-XMLDecoder(CVE-2017-10271)反序列化漏洞分析及复现

&#x1f36c; 博主介绍&#x1f468;‍&#x1f393; 博主介绍&#xff1a;大家好&#xff0c;我是 hacker-routing &#xff0c;很高兴认识大家~ ✨主攻领域&#xff1a;【渗透领域】【应急响应】 【Java、PHP】 【VulnHub靶场复现】【面试分析】 &#x1f389;点赞➕评论➕收…

OpenCV轻松入门(六)——简单图片处理【马赛克、毛玻璃、浮雕效果】

马赛克效果 马赛克指现行广为使用的一种图像&#xff08;视频&#xff09;处理手段&#xff0c;此手段将影像特定区域的色阶细节劣化并造成色块打乱的效果&#xff0c;因为这种模糊看上去有一个个的小格子组成&#xff0c;便形象的称这种画面为马赛克。其目的通常是使之无法辨…

Unity让地图素材遮挡人物

点击编辑/项目设置/图形&#xff0c;透明度排序模式设置自定义轴&#xff0c;透明度排序轴Y设置为1其他为0。 此时人物和地图素材的图层排序相等&#xff0c;当人物的高度大于地图素材时&#xff0c;人物则被遮挡。

C# 图形化的导航界面的设计方法

目录 一、涉及到的知识点 1、BackColor属性 2、FlatStyle属性 3、TextlmageRelation属性 4、其它共性设计 二、设计实例 1、 Resources.Designer.cs 2、Form1.Designer.cs 3、Form1.cs 4、运行结果 图形化导航用于代替文字导航&#xff0c;比如对Button控件 进行图形…

222 基于matlab的天线线性阵列分布

基于matlab的天线线性阵列分布。运用遗传算法&#xff0c;对天线的庞斑进行优化&#xff0c;得到最佳的线性阵列的分布。输出迭代曲线&#xff0c;主平面方向图&#xff0c;阵元放置位置。程序已调通&#xff0c;可直接运行。 222 天线线性阵列分布 - 小红书 (xiaohongshu.com…

底层开发必知的三个内存结构概念

大家好&#xff0c;今天给大家介绍底层开发必知的三个内存结构概念&#xff0c;文章末尾附有分享大家一个资料包&#xff0c;差不多150多G。里面学习内容、面经、项目都比较新也比较全&#xff01;可进群免费领取。 在底层开发中&#xff0c;以下是三个关键的内存结构概念&…

常见的数据结构

链表 链表&#xff1a;适用于插入删除多、读少的场景。 链表在新增、删除数据都比较容易&#xff0c;可以在 O(1) 的时间复杂度内完成。 但对于查找&#xff0c;不管是按照位置的查找还是按照数值条件的查找&#xff0c;都需要对全部数据进行遍历。这显然就是 O(n) 的时间复杂…

爱奇艺APP Android低端机性能优化

01 背景介绍 在智能手机市场上&#xff0c;高端机型经常备受瞩目&#xff0c;但低端机型亦占据了不可忽视的份额。众多厂商为满足低端市场的需求&#xff0c;不断推出低配系列手机。另外过去几年的中高端机型&#xff0c;随着系统硬件的快速迭代&#xff0c;现已经被归类为低端…

【大语言模型】轻松本地部署Stable Diffusion

硬件要求&#xff1a; 配备至少8GB VRAM的GPU&#xff0c;如果你的电脑只有CPU&#xff0c;请看到最后。根据部署规模&#xff0c;需要足够的CPU和RAM。 软件要求&#xff1a; Python 3.7或更高版本。支持NVIDIA GPU的PyTorch。Hugging Face的Diffusers库。Hugging Face的Tr…

4.2 面向对象程序设计-类的继承实验

本文仅供学习交流&#xff0c;严禁用于商业用途&#xff0c;如本文涉及侵权请及时联系将于24小时内删除 目录 1.实验内容 2.实验原理 2.1类的继承 2.2 继承的优点和缺点 2.3 继承的方式 3.实验代码 1.实验内容 创建一个父类CalcTime&#xff0c;在父类中依次定义用于保存…

Pods/Nodes

&#x1f4d5;作者简介&#xff1a; 过去日记&#xff0c;致力于Java、GoLang,Rust等多种编程语言&#xff0c;热爱技术&#xff0c;喜欢游戏的博主。 &#x1f4d8;相关专栏Rust初阶教程、go语言基础系列、spring教程等&#xff0c;大家有兴趣的可以看一看 &#x1f4d9;Jav…

初步学习node.js文件模块

环境已安装好&#xff1b; 写一个read1.js如下&#xff1b; var fs require("fs"); var data ;// 创建一个流 var stream1 fs.createReadStream(test1.jsp); stream1.setEncoding(UTF8);// 绑定data事件 stream1.on(data, function(mydata) {data mydata; });/…