blogSite

我是谁?我在哪儿?我在干什么?

View project on GitHub

spring boot中Controller获取前台参数方式

@RequestParam

  • @RequestParam(value = “name”, required = false, defaultValue = “”) String name
    • required:是否必须,当为false的时候,允许不传这个参数
    • defaultValue:设置默认值
  • 在controller中直接拿name使用
  • 例:http://localhost:8089/testParam?name=zhang
          /**
          * @RequestParam获取参数值
          **/
          @RequestMapping("/testParam")
          public String paramData(@RequestParam(value = "name", required = false, defaultValue = "") String name, 
                                  Model model) {
              // name直接判断使用
              ……
          }
    
  • 作用:获取前台传递的参数值

@PathVariable

  • 例: http://localhost:8089/test/2/20
          /**     
          * 测试动态接收URL中的数据     
          * */    
          @RequestMapping(value = "/test/{pageNo}/{pageSize}", method = RequestMethod.POST)    
          public String urlData(@PathVariable int pageNo , @PathVariable int pageSize){       
              String result = "receive pageNo = "+pageNo+" pageSize = "+pageSize;        
              System.out.println(result);        
              return result;    
          }
    
  • 作用:获取url中的uri值

HttpServletRequest

  • 例:http://localhost:8089/testParam?name=zhang
          /**
          * 在request中获取参数值
          **/
          @RequestMapping("/testParam")
          public String paramData(HttpServletRequest request, Model model) {
              String name = request.getParameter("name");
          }
    
  • 作用:获取前台传递的参数值

参考文档

上一篇:应用服务器工具 下一篇:spring boot使用Redis

首页 > 学习总览 > 开发语言 > Java