Spring MVC一些常見註解的使用(一)關於url的一些註解

                                                  Spring MVC一些常見註解的使用(一)java

                                                  關於url的一些註解正則表達式

@PathVariablespring

直接獲取url上的指定值,用法示例:mvc

@RequestMapping(value = "/comIncomeType/{userId}/test", method = RequestMethod.GET)
	@ResponseBody
	public JsonAndView incomeType(@PathVariable("userId")String userId ) {
		JsonAndView jv = new JsonAndView();
		jv.addData("key",userId);
		return jv;
	}

 @RequestMappingapp

這個註解是你們都比較經常使用的的一個註解,可是一個比較有用的是這個註解提供url模版的正則表達式(應該是3.0之後的特性)用法以下:url

@RequestMapping(value = "/comIncomeType/{userId:[0-9]}/test", method = RequestMethod.GET)
@ResponseBody
public JsonAndView incomeType(@PathVariable("userId")String userId ) {
	JsonAndView jv = new JsonAndView();
	jv.addData("key",userId);
	return jv;
}

這個時候userId就只支持就0到9的數字,其餘的所有不匹配。spa

@MatrixVariablecode

這個註解是專門在,作參照的時候使用,這種url:com/comIncomeType/1;p=2下使用,不過我在實現用例的時候法相,spring mvc 4.0下面這個註解還可能有一些bug,當模版url使用了正則表達式的時候,後面的那些值是拿不到的,後面有機會去看看實現上是否是真的bug。xml

@RequestMapping(value = "/comIncomeType/{userId}", method = RequestMethod.GET)
	@ResponseBody
	public JsonAndView incomeType(@MatrixVariable(value="p" ,pathVar="userId")String p) {
		
		System.out.println(p);
		JsonAndView jv = new JsonAndView();
		jv.addData("key","nihao");
		return jv;
	}

當有不少url上不止一個矩陣參數的時候,能夠使用Map<String,String>作參數就不用指定value值了。注意的是要使用的時候,配置文件上必定要配置io

<mvc:annotation-driven enable-matrix-variables="true"/>
相關文章
相關標籤/搜索