0%

1.SpringMVC

ssm:mybatis+spring+springMVC mvc三层架构

框架:研究官方文档,锻炼自学能力,锻炼笔记能力,锻炼项目能力

SSM=JavaWeb做项目;

Spring: IOC 和 AOP

SpringMVC: SpringMVC的执行流程

SpringMVC:ssm框架整合

官方文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html

MVC:模型(dao,service) 视图(jsp) 控制器(servlet)

1.1什么是MVC

  • MVC是一种设计规范
  • 是将业务逻辑、数据、显示分离的方法来组织代码
  • MVC主要作用于是降低了视图与业务逻辑间的双向耦合
  • MVC不是一种设计模式,MVC是一种架构模式

Model(模型):数据模型,提供要展示的数据,因此包含数据和行为,可以认为是领域模型或JavaBean组件(包含数据和行为),不过现在一般都分离开来:Value Object(数据Dao) 和 服务层(行为Service)。也就是模型提供了模型数据查询和模型数据的状态更新等功能,包括数据和业务。

View(视图):负责进行模型的展示,一般就是我们见到的用户界面,客户想看到的东西。

Controller(控制器):接收用户请求,委托给模型进行处理(状态改变),处理完毕后把返回的模型数据返回给视图,由视图负责展示。 也就是说控制器做了个调度员的工作。

JSP:本质就是一个Servlet

假设:你的项目的架构,是设计好的,还是演进的?

  • Alibaba PHP(个人网站)
  • 随着用户量大, Java
  • 王坚 –去IOE Mysql
  • Mysql:MySQL—–>AliSQL,AliRedis
  • All in one —>微服务

1.2回顾servlet

  1. 新建Maven工程当父工程 添加依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    <!-- 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/org.springframework/spring-webmvc -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.5.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
    <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.2</version>
    <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    </dependency>
  2. 建立一个moudle,添加web app支持

  3. 新建servlet 类继承HTTPServlet

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //获取前端参数
    String method = req.getParameter("method");
    if ("add".equals(method)){
    req.getSession().setAttribute("msg","执行了add方法");
    }
    if ("delete".equals(method)){
    req.getSession().setAttribute("msg","执行了delete方法");
    }
    //调用业务层
    //视图转发或者重定向
    req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,resp);
    }

    重写get和post方法

  4. xml中配置servlet

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.lwj.servlet.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
    </servlet-mapping>

    <session-config>
    <session-timeout>15</session-timeout>
    </session-config>

MVC要做哪些事情:

  • 将url映射到Java类或者Java类的方法
  • 封装用户提交的数据
  • 处理请求–调用相关的业务处理–封装响应数据
  • 将相应的数据进行渲染,jsp/html等表示层数据