• Spring MVC快速入门。


    前言

    上次分享了关联关系。本次来分享一下Spring MVC的入门。


    一、什么是springMVC?

    Spring Web MVC是一种基于Java的实现了MVC设计模式的、请求驱动类型的、轻量级Web框架。

    二、SpringMVC的工作原理

      1、DispatcherServlet在web.xml中的部署描述,从而拦截请求到springMVC
      2、HandlerMapping的配置,从而将请求映射到处理器
      3、HandlerAdapter的配置,从而支持多种类型的处理器
      4、处理器(页面控制器)的配置,从而刊行功能处理
      5、ViewResolver的配置,从而将逻辑视图名解析为具体的视图技术

    三、Spring MVC 搭建环境

    1、框架配置文件   springmvc.xml

            1.1、扫描base-package

                    定义base-package

                    开启注解扫描

                    开启动态代理

            1.2、配置视图解析器

            1.3、静态资源映射

    2、web.xml配置

            Spring与web容器集成

           前面分享过了,这里就不分享了。

            SpringMVC 与web容器集成

    1. <dependency>
    2. <groupId>org.springframeworkgroupId>
    3. <artifactId>spring-webmvcartifactId>
    4. <version>${spring.version}version>
    5. dependency>

    修改web.xml

    1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    4. version="3.1">
    5. <display-name>Archetype Created Web Applicationdisplay-name>
    6. <context-param>
    7. <param-name>contextConfigLocationparam-name>
    8. <param-value>classpath:applicationContext.xmlparam-value>
    9. context-param>
    10. <listener>
    11. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    12. listener>
    13. <servlet>
    14. <servlet-name>SpringMVCservlet-name>
    15. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    16. <init-param>
    17. <param-name>contextConfigLocationparam-name>
    18. <param-value>/WEB-INF/springmvc-servlet.xmlparam-value>
    19. init-param>
    20. <load-on-startup>1load-on-startup>
    21. <async-supported>trueasync-supported>
    22. servlet>
    23. <servlet-mapping>
    24. <servlet-name>SpringMVCservlet-name>
    25. <url-pattern>/url-pattern>
    26. servlet-mapping>
    27. web-app>

    四、SpringMVC工作原理

    1.首先用户发送请求–>DispatherServlet
    2 DispatcherServlet–>HandlerMapping
    3 DispatcherServlet–>HandlerAdapter
    4 HandlerAdapter–>处理器功能处理方法的调用
    5 ModelAndView的逻辑视图名–>ViewRecolver
    6 View–>渲染
    7 返回控制权给DispatcherServlet,由DispatcherServlet返回呼应给用户
     

    hellocontroller

    1. package com.zhw.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. /**
    5. * @author louis
    6. * @create  2022-08-16 21:51
    7. */
    8. @Controller
    9. public class HelloController {
    10. @RequestMapping("/hello1")
    11. public String hello(){
    12. return "hello";
    13. }
    14. }

     hello.jsp

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: zhw
    4. Date: 2022/8/16
    5. Time: 22:38
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>你好MVCtitle>
    12. head>
    13. <body>
    14. 你好MVC
    15. body>
    16. html>

     

     

    五、SpringMVC   crud

    bookcontroller

    1. package com.zhw.controller;
    2. import com.zhw.biz.BookBiz;
    3. import com.zhw.model.Book;
    4. import com.zhw.util.PageBean;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Controller;
    7. import org.springframework.web.bind.annotation.PathVariable;
    8. import org.springframework.web.bind.annotation.RequestMapping;
    9. import javax.servlet.http.HttpServletRequest;
    10. import java.util.HashMap;
    11. import java.util.List;
    12. import java.util.Map;
    13. /**
    14. * @author louis
    15. * @create  2022-08-16 21:53
    16. */
    17. @Controller
    18. @RequestMapping("/book")
    19. public class BookController {
    20. @Autowired
    21. private BookBiz bookBiz;
    22. @RequestMapping("/add")
    23. public String add(Book book){
    24. this.bookBiz.insertSelective(book);
    25. return "redirect:/book/list";
    26. }
    27. @RequestMapping("/del/{bid}")
    28. public String del(@PathVariable(value = "bid") Integer bid){
    29. this.bookBiz.deleteByPrimaryKey(bid);
    30. return "redirect:/book/list";
    31. }
    32. @RequestMapping("/list")
    33. public String list(HttpServletRequest request, Book book){
    34. PageBean pageBean = new PageBean();
    35. pageBean.setRequest(request);
    36. Map map = new HashMap();
    37. map.put("bname","%圣墟%");
    38. List list = this.bookBiz.listPager(map, pageBean);
    39. request.setAttribute("bookList",list);
    40. request.setAttribute("pageBean",pageBean);
    41. return "bookList";
    42. }
    43. @RequestMapping("/edit")
    44. public String edit(Book book){
    45. this.bookBiz.updateByPrimaryKeySelective(book);
    46. return "redirect:/book/list";
    47. }
    48. }

    index.jsp

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: zhw
    4. Date: 2022/8/17
    5. Time: 9:25
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>Titletitle>
    12. head>
    13. <body>
    14. <a href="${pageContext.request.contextPath}/book/list">查询所有a>
    15. <a href="${pageContext.request.contextPath}/book/add">增加a>
    16. <a href="${pageContext.request.contextPath}/book/edit">修改a>
    17. <a href="${pageContext.request.contextPath}/book/del/22">删除a>
    18. body>
    19. html>

     

  • 相关阅读:
    Linux操作系统概述3——进程相关操作讲解(进程概念、xinetd守护进程、进程管理命令)
    C语言sizeof函数解析
    LeetCode75——Day2
    C语言处理参数的 getopt() 函数
    http://localhost:8080打不开/shutup.bat命令行闪退
    【使用malloc函数动态模拟开辟二维数组的三种方法】
    探花交友03-MongoDB基础
    如何阅读 Spring Cloud OpenFein 源码
    SpringBoot整合JavaMailSender(发送QQ邮件)
    iOS越狱检测总结
  • 原文地址:https://blog.csdn.net/qq_62331938/article/details/126383456