-1

I am using @RequestParam to catch the front-end user input and pass it to the back end through controller and save it to the database.

So far controller handles the request like this:

@RequestMapping(value = "/myURL", method = RequestMethod.POST)
    public String saveCustomer(
            @RequestParam("customerFirstName") String customerFirstName,
            @RequestParam("customerLastName") String customerLastName,) {

        Customer customer = customerService.saveCustomer(
                customerFirstName, customerLastName);
        return null;
    }

Well I guess this is fine when I only have two @RequestParam for two arguements, but I am facing some table that has more than 10 params, I think by using @RequestParam is apparently not realistic, is there another around this?

OPK
  • 4,120
  • 6
  • 36
  • 66
  • Why do you say it's not realistic? – cadams Jul 23 '15 at 18:28
  • @cadams it is time consuming and confusing to write method that takes 10 parameters dont u think? – OPK Jul 23 '15 at 18:30
  • Perhaps just perform the user input outside of the argument list, and store the information in variables, and then use those variables as parameters? – cadams Jul 23 '15 at 18:32

3 Answers3

1

You can save the customer directly.

@RequestMapping(value = "/myURL", method = RequestMethod.POST)
public String saveCustomer(Customer customer) {

    customerService.saveCustomer(customer);
    return null;
}

Spring can databind POJOs as long as you have a no-args constructor and setters for your properties.

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
  • so currently at the front-end i have forms that the user will type in and I use the `@RequestParam` to catch these data, how do you make sure I can pass in the user input data to `Customer` if do what you suggested? – OPK Jul 23 '15 at 18:38
  • I don't understand your question. There is no need to use @RequestParam in your case – Neil McGuigan Jul 23 '15 at 18:45
0

You should create a provider to use JSON, so you can send complex Java\JS objects and not just primitives.

Yogev Caspi
  • 151
  • 12
0

If you are going to deal with multiple @RequestParam you can always opt for a bean class approach.

Controller:

@RequestMapping(value = "/myURL", method = RequestMethod.POST)
public String saveCustomer(
        @RequestBody Customer customer) {
    return null;
}

Bean:

public class Customer{
    private String customerFirstName;
    private String customerLastName;
    //constructors, getters and setters
}

Check this link out for more info on @RequestBody

Community
  • 1
  • 1
Anand
  • 305
  • 1
  • 2
  • 9