Can @RequestParam be used on non GET requests?

后端 未结 3 1781
一向
一向 2021-01-14 00:29

Spring documentation says:

Use the @RequestParam annotation to bind request parameters to a method parameter in your controller.

AFA

相关标签:
3条回答
  • 2021-01-14 00:53

    It works with posts too. Can you post your method body and you html?

    0 讨论(0)
  • 2021-01-14 00:54

    Instead of @RequestParam which binds to a single form value, you can use @ModelAttribute annotation and bind to the whole object. But it should be used in conjunction with form or bind Spring's JSTL.

    Example: - controller that calls JSP-page, it should add objects to a Model:

    @RequestMapping(value="/uploadForm", method=RequestMethod.GET)
    

    public String showUploadForm(Model model) {

    Artist artist = new Artist();
    Track track = new Track();
    
    model.addAttribute("artist", artist);
    model.addAttribute("track", track);
    
    
    return "uploadForm";
    

    }

    • JSP might look something like that:

    Track Title *:

    • Controller that processes form submission;

      @RequestMapping(value="/uploadToServer", method=RequestMethod.POST)

      public String uploadToServer(@ModelAttribute("artist") Artist artist, @ModelAttribute("track") Track track) { .... }

    Here I found a good explanation of using @ModelAttribute annotation - krams915.blogspot.ca

    0 讨论(0)
  • 2021-01-14 01:07

    Yes it works perfectly with post method too. you can mention the method attribute of @RequestParam as RequestMethod=POST. Here is the code snippet

    @RequestMapping(value="/register",method = RequestMethod.POST)
    
    public void doRegister
    (
    
        @RequestParam("fname") String firstName,
        @RequestParam("lname")String lastName,
        @RequestParam("email")String email,
        @RequestParam("password")String password 
    )
    
    0 讨论(0)
提交回复
热议问题