MVC

一、MVC
MVC是什么

​ MVC 是一种软件设计规范

​ 一定程度上完成了前后端分离

​ MVC是一种框架模式

MVC的分层
Mode(模型)

​ 提供要展示的数据(Dao)和行为(Service) 提供数据 提供 对数据操作的能力

​ 提供业务实现

​ 操作保存数据

View(视图)

​ 把数据展示给用户 和用户交互 发起请求 (负责进行模型的展示)

​ 展示模型

​ 发起请求

Control(控制器)

​ 接受用户和视图交互产生的请求 转换为模型的数据操作 返回操作后的数据 交给View展示

​ 取得View 请求中的数据

​ 调用模型 进行 数据处理

​ 得到处理后的数据转向指定页面

MVC的执行顺序

用户在View 发送请求(点击某个按钮或者图片) 控制层 调用Dao层 完成业务逻辑 然后返回数据重新渲染页面

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wRgECI5v-1614396470489)(https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsishuok.cn%2Fforum%2Fupload%2F2012%2F2%2F22%2F09f0d66cbd85fcc3f49c0f807e4a6929__3.jpg&refer=http%3A%2F%2Fsishuok.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1616933061&t=9a938791a004eb63ddeca55e736111fe)]

二、构建一个MVC Web项目
①导入依赖
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.12.RELEASE</version>
</dependency>


<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>
②Servlet
//@WebServlet(name = "UserServlet",value = "/UserServlet")
//实现了Servlet 接口的程序就是一个Servlet
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("asd");
        doGet(req, resp);
        //复用
        //Post 用于客户端传送数据到服务器
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("asd");
        String act = req.getParameter("act");
        System.out.println(act);
        //获取前端参数
        if (act.equals("add")) {
            add(req, resp);
        } else if (act.equals("delete")) {
            delete(req, resp);
        }
        //post 方法的请求在导航栏会隐藏请求的数据
        //GET 调用用于获取服务器信息,并将其做为响应返回给客户端。当经由Web浏览器或通过HTML、
        // JSP直接访问Servlet的URL时,一般用GET调用。 GET调用在URL里显示正传送给SERVLET的数据,
        // 这在系统的安全方面可能带来一些问题,比如用户登录,表单里的用户名和密码需要发送到服务器端,
        // 若使用Get调用,就会在浏览器的URL中显示用户名和密码
        //Get 拿到请求
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("value", "删除了一个用户");
        //调用业务层(Dao)处理数据
        //...
        req.getRequestDispatcher("/after.jsp").forward(req, resp);
        //转发 或者 重定向视图
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("value", "增加了一个用户");
        req.getRequestDispatcher("/after.jsp").forward(req, resp);
    }


}

可以使用注解(WebServlet) 快速注册Servlet

③注册Servlet
<?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>UserServlet</servlet-name>
        <servlet-class>com.lxc.servlet.UserServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/UserServlet</url-pattern>
    </servlet-mapping>
    <!--    请求映射 当form 请求这个url 的时候 会映射到 servlet-class 中指定的类 然后servlet 开始处理请求-->
    <!--    <session-config>-->
    <!--        <session-timeout>12</session-timeout>-->
    <!--        设置session 超时时间 默认为30分钟在Tomcat-->
    <!--    </session-config>-->

    <!--    <welcome-file-list>-->
    <!--        <welcome-file>index.jsp</welcome-file>-->
    <!--    </welcome-file-list>-->
    <!--    设置欢迎页 默认为 index.jsp-->
</web-app>
④准备请求Jsp页面
  <body>
<%--  action 中要写Servlet 的Uri--%>
<%--为什么 /UserServlet 会404--%>
<%--不加斜杠请求的是当前页面路径的资源? 那Servlet 也没在同一个路径啊?--%>
<%--这是不加的路径 http://localhost:8080/springmvc_01_war_exploded/UserServlet--%>
<%--加斜杠请求的是服务器根目录下的资源--%>
<%--这是加了的路径 http://localhost:8080/UserServlet--%>
  <form action="UserServlet" method="post">
    <input type="text" name="act">
    <input type="submit">
  </form>
  </body>
⑤响应请求后
<body>
${value}
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值