天天看點

SpringMVC-RequestMapping映射屬性

在一個方法的前面添加注解@RequestMapping(),攔截使用者的請求,并處理

也可以在類的前面添加注解@RequestMapping(),隻是請求的位址為 類/方法

@RequestMapping()中的屬性

value使用者請求的位址

method使用者請求的方法,請求的方法有兩種post,get

method = RequestMethod.GET

method = RequestMethod.POST

params使用者請求的時候的參數

params={}中可以系欸多個參數

package org.sun.handler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "Project")
public class SpringMVCHandler {
	@RequestMapping(value = "springmvc",method = RequestMethod.POST,params = {"name=sun"})//請求的參數必須有name=name,value=sun
     public String welcome() {
    	 return "success";
     }
}
           

通過@PathVariable("name") String name來擷取送出的資料

前台的交的資料,可以直接寫在位址的後面

方法的@RequestMapping()中加上接受資料的名字,加上{}

方法中添加參數@PathVariable("name") String name

package org.sun.handler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "Project")
public class SpringMVCHandler {
	@RequestMapping(value = "springmvc/{name}")//攔截請求
     public String welcome(@PathVariable("name") String name) {
		System.out.println(name);
    	 return "success";
     }
}
           
<a href="Project/springmvc/sun" target="_blank" rel="external nofollow" >first springmvc</a>