• SpringMVC


    SpringMVC

    1.SpringMVC项目搭建

    1.1pom文件

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0modelVersion>
        <groupId>com.aaa.springmvcgroupId>
        <artifactId>SpringMVCDemoartifactId>
        <packaging>warpackaging>
        <version>1.0-SNAPSHOTversion>
        <name>SpringMVCDemo Maven Webappname>
        <url>http://maven.apache.orgurl>
    
        
        <properties>
            <spring.version>5.3.10spring.version>
        properties>
    
        <dependencies>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>${spring.version}version>
            dependency>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webartifactId>
                <version>${spring.version}version>
            dependency>
            
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-databindartifactId>
                <version>2.14.2version>
            dependency>
    
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>8.0.33version>
            dependency>
    
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>1.1.23version>
            dependency>
    
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-aspectsartifactId>
                <version>${spring.version}version>
            dependency>
    
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jdbcartifactId>
                <version>${spring.version}version>
            dependency>
    
            
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>javax.servlet-apiartifactId>
                <version>4.0.1version>
            dependency>
    
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>1.18.28version>
            dependency>
        dependencies>
    project>
    
    
    • 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    1.2 Spring配置文件Spring.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                               http://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/context
                               http://www.springframework.org/schema/context/spring-context.xsd
                               http://www.springframework.org/schema/mvc
                               http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
        <context:annotation-config>context:annotation-config>
        <context:component-scan base-package="com.aaa">context:component-scan>
        
        <mvc:annotation-driven>mvc:annotation-driven>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    1.3web.xml

    
    <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">
        <display-name>Archetype Created Web Applicationdisplay-name>
        <servlet>
            <servlet-name>DispatcherServletservlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:spring.xmlparam-value>
            init-param>
        servlet>
    
        <servlet-mapping>
            <servlet-name>DispatcherServletservlet-name>
            
            <url-pattern>/url-pattern>
        servlet-mapping>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    1.4测试StudentController

    package com.aaa.springmvc.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * @Author Grg
     * @Date 2023/9/11 21:23
     * @PackageName:com.aaa.springmvc.controller
     * @ClassName: StudentController
     * @Description: 又是码代码的一天
     * @Version plus max 宇宙无敌终极版本
     */
    @Controller
    @RequestMapping("stu")
    public class StudentController {
        @RequestMapping("add")
        public void addStu(){
            System.out.println("添加学生");
        }
    
        @RequestMapping("delete")
        public void delStu(){
            System.out.println("删除学生");
        }
    }
    
    
    • 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

    在路径输入对应路径,可以看到控制台输出

    2.配置启动优先级

    <servlet>
            <servlet-name>DispatcherServletservlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:spring.xmlparam-value>
            init-param>
        
        
        	加上这一句时,在启动猫咪的时候 就会直接加载类 就让第一次访问变快,但是启动猫咪的时间会变长
            <load-on-startup>1load-on-startup>
        servlet>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.访问静态资源

    在配置了<load-on-startup>1</load-on-startup> 访问不到静态资源.html文件 
                                                是因为和Tomcat的配置文件冲突了
        
    老版本修改web.xml  <!--        <url-pattern>*.do</url-pattern>-->
    新版本修改spring配置文件    <mvc:default-servlet-handler></mvc:default-servlet-handler>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4.接收请求参数

    4.1传统方式

    @RequestMapping("add")
        public void addStu(HttpServletRequest req, HttpServletResponse resp){
            String id = req.getParameter("id");
        }
    
    • 1
    • 2
    • 3
    • 4

    4.2直接注入

    @RequestMapping("delete")
    	//传过来的参数需要名字一致 不然就赋值为空  如果参数不能赋值为空就会报500
        public void delStu(int id,String name){
            System.out.println("删除学生");
        }
    
    @RequestMapping("update")   //不匹配可以加这个@RequestParam("xxx")
        public void updateStu(@RequestParam("sid") int id,@RequestParam("sname") String name)     {
            System.out.println("删除学生");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    4.3对象注入

    @RequestMapping("list")
    	//当传过来的参数 对应着StudentVO中的成员变量 就会自动给对象赋值
        public void list(StudentVO s) {
            System.out.println(s);
            System.out.println("对象注入");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4.4地址栏传参

    @RequestMapping("xixi/{id}/{name}")
        public void getStu(@PathVariable("id") int id,@PathVariable("name") String name) {
            System.out.println(id);
            System.out.println(name);
            System.out.println("地址栏传参");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    5.跳转结果视图

    5.1传统方式

    @RequestMapping("haha")
        public void haha(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        }
    
    • 1
    • 2
    • 3
    • 4

    5.2ModelAndView

    @RequestMapping("sdfd")
        public ModelAndView sdfd() {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("/index.jsp");
            return modelAndView;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    5.3forward

    @RequestMapping("xixi")
        public String xixi() {
            //请求转发
            return "forward:/index.jsp";
        }
    
    @RequestMapping("xixi")
        public String xixi() {
            //重定向
            return "redirect:/index.jsp";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    6.共享数据

    6.1传统方式

    @RequestMapping("haha")
        public void haha(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            req.setAttribute("msg","共享数据");
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    6.2ModelAndView

    @RequestMapping("sdfd")
        public ModelAndView sdfd() {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("msg","共享数据")
            modelAndView.setViewName("/index.jsp");
            return modelAndView;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    6.3forward

    @RequestMapping("xixi")
        public String xixi(Model m) {
            m.addAttribute("msg","共享数据");
            return "forward:/index.jsp";
        }
    
    
    
        @Autowired
        private HttpSession session;
    
        @RequestMapping("hei")
        public String hei(Model m) {
            session.setAttribute("msg", "共享数据");
            return "forward:/index.jsp";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    7.响应Ajax

        @RequestMapping("haha")
        @ResponseBody
        public AjaxResult haha(){
            return AjaxResult.success();
        }
    
    
    @RestController //复合注解
    @RequestMapping("ajax")
    public class AjaxController {
    
        @RequestMapping("haha")
        public AjaxResult haha(){
            return AjaxResult.success();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    书生·浦语大模型全链路开源体系-第6课
    JavaScript 任务池
    【LeetCode54螺旋矩阵、LeetCode59螺旋矩阵II:简单模拟(Java实现,一种思想解决两道题)】
    程 控 电 源1761程控模块电源
    等级保护定级之备案!
    vs生成dll且被java通过jna调用
    iOS iGameGuardian修改器检测方案
    Linux文件与目录的增删改查
    怎样选择合适的CRM客户管理系统?
    芯片洁净间的等级是如何划分的
  • 原文地址:https://blog.csdn.net/g877835148/article/details/132822716