入门注解@MatrixVariable

siye@r480:~/svlution/workspace/springmvc4322$ tree src/
src/
├── main
│   ├── java
│   │   ├── log4j.properties
│   │   └── ocn
│   │       └── site
│   │           └── springmvc
│   │               └── controller
│   │                   └── Manicontroller.java
│   ├── resources
│   └── webapp
│       └── WEB-INF
│           └── web.xml
└── test
    ├── java
    │   └── ocn
    │       └── site
    │           └── springmvc
    │               └── controller
    │                   └── Runtest.java
    └── resources
        └── config
            └── application.xml

17 directories, 5 files
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<!-- 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-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.22.RELEASE</version>
    <scope>test</scope>
</dependency>
package ocn.site.springmvc.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;

@Controller
public class Manicontroller {

	private final Logger logger = Logger.getLogger(this.getClass());

	// 矩阵变量的历史
	// 根据RFC3986规定name-value可随着路径片段(path segments)一起出现的规范。
	// spring3.2新增了@MatrixVariable开始支持矩阵变量的部分功能。
	// 从spring4.0开始全面支持该功能。

	// 矩阵变量的使用规范
	// 根据RFC3986的规范,矩阵变量应当绑定在路径变量中!
	// 若是有多个矩阵变量,应当使用英文符号;进行分隔。
	// 若是一个矩阵变量有多个值,应当使用英文符号,进行分隔,或之命名多个重复的key即可。
	// 矩阵变量的值可以存储到 org.springframework.util.MultiValueMap<K, V> 对象中。

	// 矩阵变量默认的配置说明在哪里查看?
	// 查看mvc的语法约束的XSD文档
	// 查看 <mvc:annotation-driven enable-matrix-variables 该属性的doc说明。
	// 在当前测试使用的版本中,矩阵变量的功能默认是关闭的。

	// 如何源码跟踪矩阵变量的默认设置?
	// 1)有此组件进行配置设置矩阵变量的功能是否开启的!
	// org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.requestMappingHandlerMapping()
	// 2)查看该方法,有两个类对象的配置设置,但是查看该对象定义的布尔类型,无法确定,故跟踪调用该属性的地方。ctrl-alt-h
	// org.springframework.web.servlet.handler.AbstractHandlerMapping.setRemoveSemicolonContent(boolean)
	// 3)阅读该方法的源码,查看是否可确定矩阵变量的默认配置。
	// org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder.StandaloneConfiguration.getHandlerMapping()
	// 4)查看此变量的定义,默认是没有初始化的,即默认值是false的。(追踪到此处未得到结果,回到分岔口重新查看。)
	// org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder.removeSemicolonContent
	// 5)此类的设置方法,在默认情况下是禁用矩阵变量的功能的。
	// org.springframework.web.util.UrlPathHelper.setRemoveSemicolonContent(boolean)
	// org.springframework.web.util.UrlPathHelper.removeSemicolonContent
	// 6)以下指定的方法调用的是UrlPathHelper中的同名方法。
	// org.springframework.web.cors.UrlBasedCorsConfigurationSource.setRemoveSemicolonContent(boolean)
	// 总结:跟随以上的配置流程,就可确定默认的配置情况下,是禁用矩阵变量的功能。

	// 注意!在springmvc中,以及当前版本的默认配置,矩阵变量的功能默认是关闭的。
	// 若是使用矩阵变量的功能,需要手动的开启。
	@GetMapping("/test/{var}")
	public void handler(@PathVariable String var, @MatrixVariable("id") int id,
			@MatrixVariable("list") List<String> list) {
		logger.info(var);
		logger.info(id);
		logger.info(list);
	}

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

	<context:component-scan base-package="ocn.site.springmvc.controller"></context:component-scan>
	<!--
		基于java-config风格的配置,有两种配置方案。

		++++ 1种是实现WebMvcConfigurerAdapter配置类,重写其内的configurePathMatch方法
		创建 UrlPathHelper 的实例并设置该方法 setRemoveSemicolonContent(boolean)
		注意,参数要设置为false。
		具体设置方法如下(注意实现WebMvcConfigurerAdapter配置类)
		@Override
		public void configurePathMatch(PathMatchConfigurer configurer) {
		UrlPathHelper urlPathHelper = new UrlPathHelper();
		urlPathHelper.setRemoveSemicolonContent(false);
		configurer.setUrlPathHelper(urlPathHelper);
		}

		++++ 1种方式是实现DelegatingWebMvcConfiguration配置类,重写实现请求处理适配器的方法
		记住重写该方法requestMappingHandlerMapping()
		注意,需要执行父类种的配置方法,这样才能让保留有父类的配置。
		然后调用setRemoveSemicolonContent(boolean),参数设置为false即可。
		具体设置方法如下(注意实现DelegatingWebMvcConfiguration配置类)
		@Bean
		@Override
		public RequestMappingHandlerMapping requestMappingHandlerMapping() {
		RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping();
		handlerMapping.setRemoveSemicolonContent(false);
		return handlerMapping;
		}
	-->
	<mvc:annotation-driven enable-matrix-variables="true"></mvc:annotation-driven>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>
package ocn.site.springmvc.controller;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:config/application.xml")
public class Runtest {

	private @Autowired WebApplicationContext wac;

	@Test
	public void run() throws Exception {
		String path = "/test/path;id=34;list=rose,jack,hack";
		MockMvc build = MockMvcBuilders.webAppContextSetup(this.wac).build();
		MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get(path);
		ResultActions ra = build.perform(requestBuilder);
		ra.andExpect(MockMvcResultMatchers.status().isOk());
	}

}
19-09-05 14:09:09 org.springframework.test.context.web.WebTestContextBootstrapper  =====>>> Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
19-09-05 14:09:09 org.springframework.test.context.web.WebTestContextBootstrapper  =====>>> Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@6d78f375, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@50c87b21, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5f375618, org.springframework.test.context.support.DirtiesContextTestExecutionListener@1810399e]
19-09-05 14:09:09 org.springframework.beans.factory.xml.XmlBeanDefinitionReader  =====>>> Loading XML bean definitions from class path resource [config/application.xml]
19-09-05 14:09:10 org.springframework.web.context.support.GenericWebApplicationContext  =====>>> Refreshing org.springframework.web.context.support.GenericWebApplicationContext@6eebc39e: startup date [Thu Sep 05 14:09:10 CST 2019]; root of context hierarchy
19-09-05 14:09:10 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  =====>>> Mapped "{[/test/{var}],methods=[GET]}" onto public void ocn.site.springmvc.controller.Manicontroller.handler(java.lang.String,int,java.util.List<java.lang.String>)
19-09-05 14:09:10 org.hibernate.validator.internal.util.Version  =====>>> HV000001: Hibernate Validator 5.2.0.Final
19-09-05 14:09:10 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter  =====>>> Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@6eebc39e: startup date [Thu Sep 05 14:09:10 CST 2019]; root of context hierarchy
19-09-05 14:09:10 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter  =====>>> Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@6eebc39e: startup date [Thu Sep 05 14:09:10 CST 2019]; root of context hierarchy
19-09-05 14:09:10 org.springframework.mock.web.MockServletContext  =====>>> Initializing Spring FrameworkServlet ''
19-09-05 14:09:10 org.springframework.test.web.servlet.TestDispatcherServlet  =====>>> FrameworkServlet '': initialization started
19-09-05 14:09:10 org.springframework.test.web.servlet.TestDispatcherServlet  =====>>> FrameworkServlet '': initialization completed in 17 ms
19-09-05 14:09:11 ocn.site.springmvc.controller.Manicontroller  =====>>> path
19-09-05 14:09:11 ocn.site.springmvc.controller.Manicontroller  =====>>> 34
19-09-05 14:09:11 ocn.site.springmvc.controller.Manicontroller  =====>>> [rose, jack, hack]
19-09-05 14:09:11 org.springframework.web.context.support.GenericWebApplicationContext  =====>>> Closing org.springframework.web.context.support.GenericWebApplicationContext@6eebc39e: startup date [Thu Sep 05 14:09:10 CST 2019]; root of context hierarchy
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值