1 SpringMVC

【狂神说Java】SpringMVC最新教程IDEA版通俗易懂:https://www.bilibili.com/video/BV1aE41167Tu/
https://mp.weixin.qq.com/mp/homepage?__biz=Mzg2NTAzMTExNg==&hid=3&sn=456dc4d66f0726730757e319ffdaa23e&scene=1&devicetype=android-33&version=28002259&lang=zh_CN&nettype=WIFI&ascene=7&session_us=gh_1dd456f1d44d
https://www.cnblogs.com/hupengfei/p/16096533.html

学习方法说明

ssm:mybatis+Spring+SpringMVC MVC三层框架

JavaSE:认真学习,老师带,入门块
JavaWeb:认真学习,老师带,入门块
框架:研究官方文档,段落自学能力,段落笔记能力,段落项目能力

SpringMVC+Vue+SpringBoot+SpringCloud+Linux
Spring=JavaWeb做项目
Spring:IOC和AOP

回顾MVC架构

MVC:模型Model(dao、service) 视图View(jsp) 控制器Controller(servlet)
Model1:客户端、JSP、业务逻辑
Model2:客户端、Servlet、业务逻辑、JSP

回顾Servlet

新建maven空项目SpringMVC,删除src文件夹
pom.xml导入依赖

<!--  导入依赖  -->
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.23</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
</dependencies>

新建module,springmvc-01-servlet选择maven空
java文件夹设置为SourcesBoot
resources文件夹设置为Resources Boot
main下新建webapp文件夹,新建index.jsp

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

webapp文件夹下新建WEB-INF文件夹,新建web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<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"
         metadata-complete="true">

</web-app>

HelloServlet.java

package com.liweixiao.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author:LiWeixiao
 * @date:2022/11/24
 * @description:
 */
public class HelloServlet extends HttpServlet {
    @Override
    protected 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);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

test.jsp

<html>
<head>
    <title>Title</title>
</head>
<body>

${msg}

</body>
</html>

web.xml注册

<servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.liweixiao.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mappimg>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
</servlet-mappimg>

form.jsp

<html>
<head>
    <title>Title</title>
</head>
<body>

<form action="/hello" method="post">
    <input type="text" name="method">
    <input type="submit">
</form>

</body>
</html>

配置tomcat

初始SpringMVC

Spring MVC的特定:

  1. 轻量级,简单易学
  2. 高效,基于请求响应的MVC框架
  3. 与Spring兼容性好,无缝结合
  4. 约定大于配置
  5. 功能强大:RESTful、数据验证、格式化、本地化、主题等
  6. 简洁灵活

最重要的一点还是用的人多,使用的公司多。

posted @   LiWeixiao  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示