Spring MVC学习@MatrixVariable

学前须知

这是关于String MVC框架的扫描注释操作部分,感觉有点难以理解,可以先学习此框架的入门知识。

迈入正题

@MatrixVariable的使用简单来说就是获取-路径中的变量和值,也就是通过路径传递参数。

如下:

//在某路径名中有如下这些内容:

/cars;color=red;year=2020		--两个不同的变量用“;”分隔开
/cars;color=red,green,blue		--同一个变量的多个数值用“,”分隔开
/cars;color=red;color=green;color=blue	--也可以重复使用变量名

获取路径中参数的方式:

使用@MatrixVariable注释类进行获取。

需要配合使用的注释类:
1、@RequestMapping:定义路径
2、@PathVariable: 获取路径中“{ }”内变量的内容。
链接: @PathVariable学习.

注意

使用@MatrixVariable前需要添加mvc配置,不然无法使用。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven enable-matrix-variables="true"/>

</beans>

主要就是添加上 <mvc:annotation-driven enable-matrix-variables=“true”/>语句

mvc命名空间的属性知识查看我的String MVC mvc命名空间属性总结

开始获取

简单使用方式:

@RequestMapping(path = "/Demo2/{id}",method = RequestMethod.GET)
    public String test1(@PathVariable String id,@MatrixVariable String color, @MatrixVariable int year){
        System.out.println("id="+id+",color="+ color+",year="+year);
        return "success";
    }
<form action="/Demo2/66;color=red;year=2020" method="get">
     <input type="submit" value="提交">
</form>

在页面上点击提交之后结果:

浏览器显示地址样式:
/Demo2/66;color=red;year=2020?
后台控制器显示结果:
id=66,color=red,year=2020

多种使用方式:

/*获取单个参数
    * 请求路径内容:/Demo2/66;color=red;year=2020 */
    @RequestMapping(path = "/Demo2/{id}",method = RequestMethod.GET)
    public String test1(@PathVariable String id,@MatrixVariable String color, @MatrixVariable int year){
        System.out.println("id="+id+",color="+ color+",year="+year);
        return "success";
    }

    /*获取不同路径段中的变量
    * 请求路径内容:/Demo2/66;q=45/pets/77;q=34*/
    @RequestMapping(path = "/Demo2/{id1}/pets/{id2}" ,method = RequestMethod.GET)
    public String test2(@MatrixVariable(name = "q",pathVar = "id1") int q1,
                        @MatrixVariable(name = "q",pathVar = "id2") int q2){
        System.out.println("q1="+q1+",q2="+q2);
        return "success";
    }

    /*设置默认值,请求段可以不需要设置参数
    * 请求路径内容:/Demo2/pets/66*/
    @RequestMapping(path = "/Demo2/pets/{id}",method = RequestMethod.GET)
    public String test3(@MatrixVariable(required = false,defaultValue = "1")int q){
        System.out.println("q="+q);
        return "success";
    }

    /*路径中的所有变量都可以被Map获得
    * 请求路径内容:/Demo2/66;q=45;s=54/pet/77;q=23;t=95*/
    @RequestMapping(path = "/Demo2/{id1}/pet/{id2}",method = RequestMethod.GET)
    public String test4(@MatrixVariable Map<String,String> map,
                        @MatrixVariable(pathVar = "id2") Map<String,String> mapid2){
        System.out.print("map="+map.toString()+",mapid2="+mapid2.toString());
        return "success";
    }

对应表单:

<form action="/Demo2/66;color=red;year=2020" method="get">
        <input type="submit" value="提交">
    </form>
    <form action="/Demo2/66;q=45/pets/77;q=34" method="get">
        <input type="submit" value="提交">
    </form>
    <form action="/Demo2/pets/66" method="get">
        <input type="submit" value="提交">
    </form>
    <form action="/Demo2/66;q=45;s=54/pet/77;q=23;t=95" method="get">
        <input type="submit" value="提交">
    </form>

运行结果

后台控制器显示结果:
id=66,color=red,year=2020
q1=45,q2=34
q=1
map={q=45, s=54, t=95},mapid2={q=23, t=95}

关于@MatrixVariable的参数

属性属性值含义
name设置要获取路径中某参数的参数名
pathVar设置要获取参数的路径段
value指定pathVar中的某参数名
requiredfalse路径中可以没有这个变量
true路径中必须有这个变量默认
defaultValue设置默认值

云中君后话:

URI规范RFC 3986定义了在路径段中包括名称/值对的可能性,可以使用一般的“ URI路径参数”,尽管也经常使用并且相当知名的,更独特的“矩阵URI”。在Spring MVC中,这些被称为矩阵变量。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值