• SpringMVC三种风格的路径映射


    • 默认

    在这里插入图片描述

    • 1.标准URL映射

    在这里插入图片描述
    在这里插入图片描述

    • 2.Ant风格的映射(通配符)

    在这里插入图片描述

    • 3.占位符的映射(restful风格)

    在这里插入图片描述

    项目结构

    在这里插入图片描述
    TestController3.java

    package com.deng.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class TestController3 {
    
        /**
         * 1.标准URL映射
         * @RequestMapping(value=”xxx”)
        * */
        @RequestMapping(value = "hello")
        public ModelAndView test() {
            ModelAndView mv = new ModelAndView();
            mv.addObject("msg", "原来是小别扇1");
            mv.setViewName("/hello");
            return mv;
        }
    
        @RequestMapping("/hello1")
        public ModelAndView test1() {
            ModelAndView mv = new ModelAndView();
            mv.addObject("msg1", "你很会打吗?");
            mv.setViewName("/hello1");
            return mv;
        }
    
        /**
         * 2.Ant风格的映射(通配符)
         *     ?:通配一个字符
         *     *:配合其他一个字符以上的时候,通配0个或者多个字符,单独使用的时候只能配置多余0个的字符
         *     **:通配0个或者多个路径, 配合其他一个字符以上的时候通配0或多个字符,而单独使用的时候,可以配置多个路径
         * */
        @RequestMapping("a?/show2")
        public ModelAndView test2() {
            ModelAndView mv = new ModelAndView();
            mv.addObject("msg2", "springmvc ant style");
            mv.setViewName("/hello2");
            return mv;
        }
    
        /**
         * 3.占位符的映射(restful风格)
         * @RequestMapping(value=“/user/{userId}/{name} ")
         * */
        @RequestMapping(value = "/ok/{name}/{id}")
        public ModelAndView test3(@PathVariable("name") String name, @PathVariable("id") Long id) {
            ModelAndView mv = new ModelAndView();
            //接收名字
            mv.addObject("msg3", "占位符name: " + name + ",id = " + id);
            //jsp名字
            mv.setViewName("/hello3");
            return mv;
        }
    }
    
    
    • 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

    mv.setViewName(“/hello2”);
    hello2是jsp的名字

    hello1.jsp

    <%--
      Created by IntelliJ IDEA.
      User: U100926
      Date: 2022/08/27
      Time: 18:04
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    <%--少了这一行,jsp页面接收不到值显示${msg}--%>
    <%@ page isELIgnored="false" %>
    
    <html>
    <head>
        <title>springmvc2</title>
    </head>
    <body>
    <span style="font-size:30px; color:darkgreen;">springmvc的第一个程序:${msg1}</span>
    <p>${msg1}</p>
    </body>
    </html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    hello2.jsp

    <%--
      Created by IntelliJ IDEA.
      User: U100926
      Date: 2022/08/27
      Time: 18:04
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    <%--少了这一行,jsp页面接收不到值显示${msg}--%>
    <%@ page isELIgnored="false" %>
    
    <html>
    <head>
        <title>springmvc2</title>
    </head>
    <body>
    <span style="font-size:30px; color:darkgreen;">springmvc的第一个程序:${msg2}</span>
    <p>${msg3}</p>
    </body>
    </html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    springmvc.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
    
        
        <context:component-scan base-package="com.deng.controller">context:component-scan>
        
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/view/">property>
            <property name="suffix" value=".jsp">property>
        bean>
    
    beans>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    web.xml

    DOCTYPE web-app PUBLIC
            "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
            "http://java.sun.com/dtd/web-app_2_3.dtd" >
    <web-app>
      
      <servlet>
        <servlet-name>SpringMVCservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
          <param-name>contextConfigLocationparam-name>
          <param-value>classpath:springmvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
      servlet>
      <servlet-mapping>
        <servlet-name>SpringMVCservlet-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

    pom.xml

    
    
    <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/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0modelVersion>
    
      <groupId>org.examplegroupId>
      <artifactId>springmvc2artifactId>
      <version>1.0-SNAPSHOTversion>
      <packaging>warpackaging>
    
      <name>springmvc2 Maven Webappname>
      
      <url>http://www.example.comurl>
    
      <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.7maven.compiler.source>
        <maven.compiler.target>1.7maven.compiler.target>
      properties>
    
      <dependencies>
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>4.11version>
          <scope>testscope>
        dependency>
        
        <dependency>
          <groupId>javax.servletgroupId>
          <artifactId>javax.servlet-apiartifactId>
          <version>4.0.1version>
        dependency>
        
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-webmvcartifactId>
          <version>5.2.8.RELEASEversion>
        dependency>
        
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>4.11version>
          <scope>testscope>
        dependency>
      dependencies>
    
      <build>
        <finalName>springmvc2finalName>
        <pluginManagement>
          <plugins>
            <plugin>
              <artifactId>maven-clean-pluginartifactId>
              <version>3.1.0version>
            plugin>
            
            <plugin>
              <artifactId>maven-resources-pluginartifactId>
              <version>3.0.2version>
            plugin>
            <plugin>
              <artifactId>maven-compiler-pluginartifactId>
              <version>3.8.0version>
            plugin>
            <plugin>
              <artifactId>maven-surefire-pluginartifactId>
              <version>2.22.1version>
            plugin>
            <plugin>
              <artifactId>maven-war-pluginartifactId>
              <version>3.2.2version>
            plugin>
            <plugin>
              <artifactId>maven-install-pluginartifactId>
              <version>2.5.2version>
            plugin>
            <plugin>
              <artifactId>maven-deploy-pluginartifactId>
              <version>2.8.2version>
            plugin>
          plugins>
        pluginManagement>
      build>
    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
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
  • 相关阅读:
    【LeetCode】图文+实例弄清KMP算法
    机器学习基础之《分类算法(6)—决策树》
    【高并发基础】Spring 事务传播级别及造成死锁的隐患分析
    React源码分析4-深度理解diff算法
    新电脑的前端开发环境的安装配置
    03Java基础语法:注释/关键字/字面量/变量
    vscode中git的使用,以及与webstorm中git的使用对比
    莞中集训游记
    基于SSH(Struts+Spring+Hibernate)实现汽修管理系统《建议收藏:附完整源码+数据库》
    [附源码]计算机毕业设计学生社团信息管理系统Springboot程序
  • 原文地址:https://blog.csdn.net/djydjy3333/article/details/126621717