• springboot 如何获取URL请求参数呢?


    转自:

    springboot 如何获取URL请求参数呢?

    下文笔者讲述SpringBoot请求URL参数的方法分享,如下所示

    URL请求参数:
         是web客户端同服务器交流数据的一种方式
    	 那么SpringBoot中如何从url中获取指定的参数值呢?
    	 下文将一一道来,如下所示:
    

    方式1:直接将参数写在Controller的形参中

    例:

        @RequestMapping("/addUser1")
        public String addUser1(String username,String password) {
            System.out.println("用户名:"+username);
            System.out.println("密码:"+password);
            return "index";
        }
    
    http://localhost:8080/addUser1?username=chengcheng&password=87654321
    

    方式2:使用HttpServletRequest的方式接收

        @RequestMapping("/addUser2")
        public String addUser2(HttpServletRequest request) {
            String username=request.getParameter("username");
            String password=request.getParameter("password");
            System.out.println("用户名:"+username);
            System.out.println("密码:"+password);
            return "index";
        }
    

    方式3:使用bean对象接收参数

    package demo.model;
    
    public class UserModel {
        
        private String username;
        private String password;
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        
    }
    
        @RequestMapping("/addUser3")
        public String addUser3(UserModel user) {
            System.out.println("用户名:"+user.getUsername());
            System.out.println("密码:"+user.getPassword());
            return "index";
        }
    

    方式4:使用@PathVariable获取路径中的参数

    @RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)
    public String addUser4(@PathVariable String username,@PathVariable String password) {
            System.out.println("用户名:"+username);
            System.out.println("密码:"+password);
            return "index";
        }
    http://localhost:8080/addUser4/usernametest/pwd88888
    

    方式5:使用@ModelAttribute注解获取POST请求的FORM表单数据

    Jsp表单: 
    
    
    用户名: 
    密  码: 
    @RequestMapping(value="/addUser5",method=RequestMethod.POST) public String addUser5(@ModelAttribute("user") UserModel user) { System.out.println("username is:"+user.getUsername()); System.out.println("password is:"+user.getPassword()); return "demo/index"; }

    方式6:使用注解@RequestParam绑定请求参数到方法入参

    当请求参数username不存在时会有异常发生
     可以通过设置属性required=false解决
      例
       @RequestParam(value="username", required=false)
      @RequestMapping(value="/addUser6",method=RequestMethod.GET)
        public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {
            System.out.println("用户名:"+username);
            System.out.println("密码:"+password);
            return "demo/index";
        }
  • 相关阅读:
    React xlsx(工具库) 处理表头合并
    java毕业设计大学生心愿墙系统Mybatis+系统+数据库+调试部署
    webpack性能优化——从产出代码角度优化(提升产品性能)
    近似熵的计算
    【数据结构】顺序栈
    谷歌JAX快速入门笔记详解和案例
    数据存储——存储图像
    asp.net+sqlserver团购网站c#
    大数据技术之Hadoop:提交MapReduce任务到YARN执行(八)
    C++----智能指针
  • 原文地址:https://blog.csdn.net/qq_25073223/article/details/127932195