@RequestMapping使用小结

@RequestMapping注解可以将HTTP请求映射给controller
来处理,包括返回视图页面的controller
Rest服务的controller

@RequestMapping可以添加到或者方法上。

@Controller
@RequestMapping(value = "/", method = RequestMethod.GET)
public class ContactController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String redirectToContactPage() {
        return  "redirect:contact";
    }

    @RequestMapping(value = "/admin", method = RequestMethod.GET)
    public String toAdminPage() {
        return "admin";
    }

    @RequestMapping(value = "/contact", method = RequestMethod.GET)
    public String toContactForOhersPage() {
        return "contact";
    }
}

在上面的例子中,@RequestMapping加在了类和方法上:

  • 访问/的请求会被ContactControllerredirectToContactPage()方法处理
  • 访问/admin的请求会被ContactControllertoAdminPage()方法处理

@RequestMapping映射多个URL

@Controller
@RequestMapping("/")
public class ContactController {
 
    @RequestMapping(value={"", "/page", "page*","view/*"})
    String multipleMapping(){
        return "Hello";
    }
  
}

访问下列地址都会被ContactControllermultipleMapping方法处理:

  • localhost:8080/
  • localhost:8080/page
  • localhost:8080/pagehello
  • localhost:8080/view/
  • localhost:8080/view/view1

@RequestParam请求参数

使用@RequestParam可以将HTTP请求参数绑定到controller中方法的参数上,例如

@Controller
@RequestMapping("/")
public class ContactController {
 
    @RequestMapping(value=/hello)
    String sayHelloToUser(@RequestParam("username") String username){
        return "Hello " + username;
    }
    
    @RequestMapping(value=/hi)
    String sayHiToUser(@RequestParam String username){
        return "Hello " + username;
    }
    
  
}

  • 当访问localhost:8080/hello?username=ted时,值ted会绑定到参数username上,结果是Hello ted
  • 当HTTP请求参数和controller方法的参数名称相同时,可以省略,如sayHiToUser方法
  • @RequestParam默认要求参数是必要的,通过设置@RequestParam(value = "username", required = false)设为可选,这样HTTP请求不带username参数也是可以访问到指定的方法。
  • 当HTTP请求不带username参数时,还可以设置它的默认值,如@RequestParam(value = "username", defaultValue = "mattie")

指定HTTP请求类型

HTTP请求有GET, POST, PUTDELETE等,@RequestMapping可以处理特定的请求类型。相当于如下注解:

  • GetMapping
  • PostMapping
  • PutMapping
  • DeleteMapping
@Controller
@RequestMapping("/")
public class ContactController {
 
    @RequestMapping(value=/hello, method = RequestMethod.GET)
    String sayHelloToUser(@RequestParam("username") String username){
        return "Hello " + username;
    }
    
    @PostMapping(value=/hello)
    String login(@RequestParam("password") String password){
        return "Login Success!";
    }
  
}

上面的例子中login方法处理POST类型的请求。

@RequestMapping处理动态URL

和注解@PathVariable联合使用,可以解析HTTP请求中的动态URL。

@Controller
@RequestMapping("/")
public class ContactController {

      @RequestMapping(value = "/contacts/{contactname}", method = RequestMethod.GET)
      String getContactName(@PathVariable("contactname") String name){
        return "Contact name is " + name; 
      }
      
}

上面的例子中,访问localhost:8080/contacts/tedlocalhost:8080/contacts/mattie都会被ContactControllergetContactName方法处理,结果分别是Contact name is tedContact name is mattie

动态URL也可以使用正则来匹配:

@Controller
@RequestMapping("/")
public class ShopController {

      @RequestMapping(value = "/{id:[a-z]+}/{productname}", method = RequestMethod.GET)
      String getProductName(@PathVariable("productname") String productName){
        return "Product name is " + productName; 
      }
      
}

  • 上面的例子中,访问localhost:8080/wears/shoeslocalhost:8080/foods/bread都会被ShopControllergetProductName方法处理
  • 但是访问localhost:8080/101/fun不会被处理。

注意@RequestParam@PathVariable的区别:@RequestParam解析URL中特定的请求参数的值;而@PathVariable用来匹配URL路径的规则和模式

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 对于java中的思考的方向,1必须要看前端的页面,对于前端的页面基本的逻辑,如果能理解最好,不理解也要知道几点。 ...
    神尤鲁道夫阅读 829评论 0 0
  • 1.SpringMvc是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱...
    wanggs阅读 561评论 0 0
  • 1. 接收请求参数 1.1. 【不推荐】通过HttpServletRequest 在处理请求的方法中,添加Http...
    fad2aa506f5e阅读 498评论 0 0
  • SpringMVC介绍 Spring web mvc 和Struts2都属于表现层的框架,它是Spring框架的一...
    day_Sunny阅读 764评论 0 0
  • P82-84 what:介绍了帮孩子认识错误观点的目的揭示法。 why:用客观友善的态度,平静的询问目的,可以帮助...
    Danny小奇阅读 474评论 0 0