1. 程式人生 > >springmvc的controller方法中接受到的引數值為NULL

springmvc的controller方法中接受到的引數值為NULL

最近遇到一個問題controller方法中接收到的引數為Null,使用的是spring-webmvc-4.3.1,tomcat使用的是tomcat6,前後臺程式碼大概如下:

function test7(){
		var user={"username":"zhangsan","password":"0000fe87eeb84a009e68859573a81069"};
		$.ajax({
			type:'POST',
			url:"http://localhost:8080/springmvc/test7.json",
			data:user,
			success:function(data){ 
				console.log(data);
			},
			error:function(data){
				console.log(data);
			}
		});
	}
@RequestMapping(value="/test7")
	public String test7(String username,HttpServletRequest request){
		System.out.println("引數:"+username);//這裡接收到的username總是為null
..........................
		return "demo1";
	}
	

在後臺test7方法中接收到的username值始終為null,但是直接從request中取是可以取到值的,後來發本地服務換成tomcat7後就可以正常接收了。這裡不去糾結為什麼了,肯定是4.3程式碼裡需要至少tomcat7及以上的執行環境,查下
maven的庫
發現其實人家早就標明使用spring-webmvc4.3執行環境必須使用servlet3.0,而tomcat7以上才支援servlet3.0,具體見下圖。之前一直只是關注 Compile Dependencies,Provided Dependencies同樣很重要,搭建服務的時候一定要關心,否則出現詭異的問題往往會浪費掉很多時間。