0

I would like to pass many @RequestParam annotated parameters to a Model object in an easy fashion.

Currently, I see two alternatives:

  1. You can do it by hand (not so easy, example below)
  2. You can create command objects for related parameters, and pass the object to your model.

I'm looking for a third way, which will do this for me automatically.

@RequestMapping
public void handle(@RequestParam String p1, @RequestParam String p2,
       @RequestParam String p3, @RequestParam String p4, Model model) {
     model.addAttribute("p1",p1);
     model.addAttribute("p2",p2);
     model.addAttribute("p3",p3);
     model.addAttribute("p4",p4);
}

The main reason I need this is to pass the parameters in a POST handler to flash attributes easily in case of an error.

Update: @ModelAttribute method (or specifying a bean without it, I think they're basically the same when you don't give @ModelAttribute a value) is actually the the second method I mentioned in the question. I'm looking for a way other than those I mentioned.

1

2 Answers 2

0

I prefer to use

@RequestMapping
public void handle(BeanDTO p1, Model model ){
    model.addAttribute("Bean",p1);
} 

It takes all the parameters that match with the property beans and place them

classs BeanDTO {    
    private String p1;
    private String p2;
    private String p3;
} 

Sorry withouth @RequestBody

Check this. Spring MVC bean mapping to HTTP GET request parameters similar to @BeanParam

Sign up to request clarification or add additional context in comments.

Comments

0

I would do this way. Where as P is a java bean with the request parameters

@RequestMapping
    public ModelAndView handle(@ModelAttribute("p") P p) 
    {
      if (validationerror){
          return new ModelAndView("index","p",p);
      }
      return new ModelAndView("success");
    }   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.