org.springframework.web.bind.annotation.MatrixVariable注释拓展了URL请求地址的功能使用@MatrixVariable注释时多个变量可以使用;分隔,该注释允许开发者进行多条件组合查询。
属性 |
类型 |
是否必要 |
说明 |
name |
String
|
否 |
指定请求参数绑定的名称,如果省略则绑定同名参数 |
value |
String |
否 |
name属性的别名 |
pathVar |
String |
否 |
matrix variable所在路径的url path变量的名称 |
required |
String |
否 |
指示参数是否必须绑定 |
defaultValue |
String |
否 |
如果没有传递参数而使用默认值 |
需要注意的是,@MatrixVariable注释功能在SpringMvc中默认是不启用的,我们首先需要启用这个注释功能:
(1)修改springmvc-config.xml,添加:
<mvc:annotation-driven enable-matrix-variables="true”/>
- <mvc:annotation-driven enable-matrix-variables="true”/>
<mvc:annotation-driven enable-matrix-variables="true”/>
(2)将RemoveSemicolonContent设置为false
package org.fkit.configuration;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@Configuration
public class CustomMvcConfiguration implements InitializingBean {
@Autowired
private RequestMappingHandlerMapping requestMappingHandlerMapping;
@Override
public void afterPropertiesSet() throws Exception {
requestMappingHandlerMapping.setRemoveSemicolonContent(false);
}
}
- package org.fkit.configuration;
- import org.springframework.beans.factory.InitializingBean;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
- @Configuration
- public class CustomMvcConfiguration implements InitializingBean {
- @Autowired
- private RequestMappingHandlerMapping requestMappingHandlerMapping;
- @Override
- public void afterPropertiesSet() throws Exception {
- requestMappingHandlerMapping.setRemoveSemicolonContent(false);
- }
- }
package org.fkit.configuration;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@Configuration
public class CustomMvcConfiguration implements InitializingBean {
@Autowired
private RequestMappingHandlerMapping requestMappingHandlerMapping;
@Override
public void afterPropertiesSet() throws Exception {
requestMappingHandlerMapping.setRemoveSemicolonContent(false);
}
}
然后我们就可以使用这个注释了
package org.fkit.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
@Controller
public class MarixVariableController
{
@GetMapping(value="/matrixVariable/{userId}")
public String marixVariable(
@PathVariable Integer userId,
@MatrixVariable(value="name",pathVar="userId") String name,
@MatrixVariable(value="age",pathVar="userId") String age,
Model model
)
{
model.addAttribute("userId",userId);
model.addAttribute("name", name);
model.addAttribute("age",age);
return "marixVariable";
}
}
- package org.fkit.controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.MatrixVariable;
- import org.springframework.web.bind.annotation.PathVariable;
- @Controller
- public class MarixVariableController
- {
- @GetMapping(value="/matrixVariable/{userId}")
- public String marixVariable(
- @PathVariable Integer userId,
- @MatrixVariable(value="name",pathVar="userId") String name,
- @MatrixVariable(value="age",pathVar="userId") String age,
- Model model
- )
- {
- model.addAttribute("userId",userId);
- model.addAttribute("name", name);
- model.addAttribute("age",age);
- return "marixVariable";
- }
- }
package org.fkit.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
@Controller
public class MarixVariableController
{
@GetMapping(value="/matrixVariable/{userId}")
public String marixVariable(
@PathVariable Integer userId,
@MatrixVariable(value="name",pathVar="userId") String name,
@MatrixVariable(value="age",pathVar="userId") String age,
Model model
)
{
model.addAttribute("userId",userId);
model.addAttribute("name", name);
model.addAttribute("age",age);
return "marixVariable";
}
}
