Several ways for spring to receive ajax parameters

Reference URL: Several methods for spring to receive ajax parameters

@ModelAttribute annotation

Using @ModelAttribute this method can directly map parameters to pojo objects, I do not add @ModelAttribute annotation, directly receive pojo objects, and can also receive parameters

Front-end ajax request

        <script type="text/javascript">
            $(function(){
                $.ajax({
                    type:"post",
                    url:"http://localhost:8080/test",
                    data:{
                        "name":"张三",
                        "phone":"10086",
                        "password":"123456"
                    },
                    async:true,
                    success:function(data){
                        console.log(data);
                    }
                });
            })
        </script>

java receives parameters

    @RequestMapping("/test")
    @ResponseBody
    public String testUser(@ModelAttribute("TUser") TUser user){
        System.out.println(user.getName());
        System.out.println(user.getPassword());
        System.out.println(user.getPhone());
        return "ok";
    }

Mapping result

@PathVariabl annotation

@PathVariable is to map the specified segment point on the requested path to the specified parameter name. Multiple @PathVariable can be specified. If none of the parameters are placed in the url path, access is directly requested. http://localhost:8080/test . can also receive parameters

Front-end ajax request

        <script type="text/javascript">
            $(function(){
                $.ajax({
                    type:"post",
                    url:"http://localhost:8080/test/10086",
                    data:{
                        "name":"张三",
                        "password":"123456"
                    },
                    async:true,
                    success:function(data){
                        console.log(data);
                    }
                });
            })
        </script>

java receives parameters

    @RequestMapping("/test/{phone}")
    @ResponseBody
    public String testUser(String name,@PathVariable String phone,String password){
        System.out.println(name);
        System.out.println(phone);
        System.out.println(password);
        return "ok";
    }

Mapping result

Get directly with HttpServletRequest

Front-end code is equivalent to case 1

java receives parameters

    @RequestMapping("/test")
    @ResponseBody
    public String testUser(HttpServletRequest request, HttpServletResponse response){
        System.out.println(request.getParameter("name"));
        System.out.println(request.getParameter("phone"));
        System.out.println(request.getParameter("password"));
        return "ok";
    }

The mapping result is exactly the same as Case 1

@RequestParam binds request parameters

The front-end request code is equivalent to Case 1

java receives parameters

    @RequestMapping("/test")
    @ResponseBody
    public String testUser(@RequestParam("name") String a,@RequestParam("phone") String b, String password){
        /**
         * The value in @RequestParam() must be the same as the parameter name passed by the front end
         */
        System.out.println(a);
        System.out.println(b);
        System.out.println(password);
        return "ok";
    }

The mapping result is exactly the same

Summarize:

1. If the parameters passed from the front end are the same as the parameter names defined by the background interface , no annotations are required. If all parameters are in a pojo object, they can be directly mapped to pojo objects , or HttpServletRequest can be used to receive parameters

2. If you want to use the resfull style request method, you can use @PathVariable to annotate

3. If the parameters passed by the front end are different from the parameter names defined by the backend interface, use the RequestParam annotation

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325141845&siteId=291194637