当前位置: 首页 > news >正文

Mvc、Springmvc框架

一.Mvc:

1.概念:

MVC它是一种设计理念。把程序按照指定的结构来划分: Model模型 、View视图 、Controller控制层;

 结构图:

 二.Springmvc:

1.概念:

springmvc框架它是spring框架的一个分支。它是按照mvc架构思想设计的一款框架。。springmvc的主要作用: 接收浏览器的请求数据,对数据进行处理,然后返回页面进行显示.

2.为什么学springmvc?

 

 3.如何使用springmvc?⭐

准备工作:创建一个maven的web工程

 第一步:在创建好的maven的web工程中把依赖引入

  <dependencies>
    <!--springmvc依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.15.RELEASE</version>
    </dependency>
  </dependencies>

第二步:创建springmvc配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--第二步:springmvc的配置文件--><!--包扫描  扫描的是@Controller 和 @RequestMapping扫描范围:扫描指定的包和子包可以到com.zyl--><context:component-scan base-package="com.zyl.controller"/><!--开启注解驱动--><mvc:annotation-driven/><!--放行静态资源--><mvc:default-servlet-handler/><!--视图解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀--><property name="prefix" value="/views/"/><!--后缀--><property name="suffix" value=".jsp"/></bean></beans>

 第三步:注册公共的servlet ---[DispatcherServlet](这里注意web.xml版本太低不行,需要替换掉之前的4.0版本。看着改)

<?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"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
<!--第四步: 注册公共的servlet-DispatcherServlet--><!--注册公共servlet-->
<servlet><servlet-name>spring01</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--加载 读取  spring 配置文件--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring01.xml</param-value></init-param>
</servlet><servlet-mapping><servlet-name>spring01</servlet-name><!--访问任何路径都要经过servlet--><url-pattern>/</url-pattern></servlet-mapping></web-app>

第四步:.编写controller类

基本完成搭建框架步骤!!!!

springmvc的流程:

 *     1. 客户端发生请求http://localhost:8080/hello/index
 *     2. 来到tomcat服务器。
 *     3. springmvc的前端控制器DipatcherServlet接受所有的请求。
 *     4. 查看你的请求地址和哪个@RequestMaping匹配。
 *     5. 执行对应的方法。方法会返回一个字符串。
 *     6. 把该字符串经过视图解析器拼接。 /前缀/字符串.后缀
 *     7. 拿到拼接的地址,找到对应的网页。 /views/hello.jsp
 *     8. 渲染该网页给客户

三、处理静态资源

静态资源就是网页中见到: js,css,image,html都是静态资源。

 

四、 接受参数

1. 接受少量参数

删除操作---值传递id.

 2.接受大量参数

添加操作 修改操作。应该封装一个实体类。必须保证实体类的属性要和参数的名字一致。

(封装一个实体类就行了)

五、 解决乱码

只能使用过滤器。

1.自己编写过滤器

package com.zyl.filter;import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;/*** @className: MyFilter* @author: Zyl* @date: 2024/12/14 15:04* @Version: 1.0* @description:*/
@WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {System.out.println("过滤器初始化");}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {servletResponse.setCharacterEncoding("UTF-8");servletRequest.setCharacterEncoding("UTF-8");filterChain.doFilter(servletRequest,servletResponse);}@Overridepublic void destroy() {}
}

注意使用过滤器需要引入依赖:

加上jdk8的代码吧:后期好找:

<!--使用jdk8版本的-->
<properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

六、 处理日期参数

这个比较简单,就在实体类里面加一个属性就可以了,然后加上注解:

http://www.lryc.cn/news/505375.html

相关文章:

  • MATLAB2021B APP seriallist 串口通信
  • 【Python爬虫系列】_033.Scrapy_分布式爬虫
  • 2025erp系统开源免费进销存系统搭建教程/功能介绍/上线即可运营软件平台源码
  • Android实战经验篇-busybox小工具
  • 上海艾一公司-运维工程师知识点备战
  • 【网络安全】Web安全基础- 第一节:web前置基础知识
  • 数仓开发那些事_番外(2)
  • Linux常用指令-----下
  • MySQL通过binlog日志进行数据恢复
  • 【AIGC】与模型对话:理解与预防ChatGPT中的常见误解
  • 字符2
  • 25年宁德时代社招在职晋升Verify测评SHL题库:语言理解+数字推理考什么?
  • 数据转换:连接数据孤岛,释放信息价值
  • 提升PHP技能:18个实用高级特性
  • MySQL基础操作(2)
  • Windows环境 (Ubuntu 24.04.1 LTS ) 国内镜像,用apt-get命令安装RabbitMQ
  • web网页前后端交互方式
  • LN61C 高精度 低功耗 小封装 电压检测芯片
  • 自动驾驶控制与规划——Project 2: 车辆横向控制
  • Bootstrap-HTML(五)图像基础样式
  • bain.js(十二):RNN神经网络实战教程 - 音乐乐谱生成 -人人都是作曲家~
  • Endnote | 查看文献所在分组
  • DateRangePickerDialog组件的用法
  • 数据库合并操作:深入理解 MERGE INTO 语句
  • 联发科MTK8788_MT8788安卓核心板安兔兔跑分_安卓主板方案商
  • 计算机网络技术基础:6.数据传输方式
  • 免费开源了一个图床工具 github-spring-boot-starter
  • Mysql之YUM安装时GPG 密钥报错问题处理
  • Hw亮度省电
  • 【信息系统项目管理师-论文真题】2015下半年论文详解