SpringMvc的requestMapping的几种常用用法

一:value 举例说明如下

public class indexController{

@Controller

@RequestMapping("/update")

 public void update(){

System.out.println("this is value");

  }

}

通过使用requestMapping可以使得一个controller类可以满足来自不同路径的多个方法调用,比如现在可以通过访问http://localhost:xxxx/xxx/update来调用update().


二:@PathVariable 举例说明如下(同样的在一个类indexController里)

 @RequestMapping(value="index1/{username}")
    public ModelAndView update(@PathVariable("username") String username){
    ModelAndView mav=new ModelAndView("index");
    System.out.println("this is update绑定赋值"+username);
    mav.addObject("username",username);
    return mav;
    }

这样可以通过访问http://localhost:xxxx/xxx/index/hhh形式的就能访问delete(); 

@PathVariable接收参数并且把它赋值给name,即name=username;


三:PathVariable接收参数 并不指定赋值

 @RequestMapping(value="index2")
    public void sys(@PathVariable String password){
     System.out.println("不指定赋值"+password);
    }

同样的,http://localhost:xxxx/xxx/update/hhh形式


四:PathVariable同时绑定多个参数

 @RequestMapping(value="index4/{username}/{password}")
    public void sys1(@PathVariable("username") String username,
    @PathVariable("password") String password){
    System.out.println("绑定赋值"+username+":"+password);
    }

访问http://localhost:xxxx/xxx/update/hhh/123形式


五:@RequestParam 进行一般的参数绑定

@RequestMapping(value="index3")
    public void sys1(@RequestParam String password){
     System.out.println("RequestParam接收"+password);
    }
只有访问http://localhost:xxxx/xxx/index3?password=xx即必须带有参数password才能访问sys();




只不过是在有多个参数的时候,中间用逗号隔开





猜你喜欢

转载自blog.csdn.net/qq_34800258/article/details/78574378