0

I have the controller method for processing form submitting:

@RequestMapping(method = {RequestMethod.POST})
    public String submitForm(...){...}

But, I have got a new test case:

If Form has parameter ProductData call controller method submitFormWithProductData. and I am facing difficulties with this, because ProductData is a Map. On Site ProductData field in form tag looks like:

<input type="text" name="productData['param1']">
<input type="text" name="productData['param2']">

And I don't know, how to create right @RequestMapping annotation for submitFormWithProductData method.

I have tried:

@RequestMapping(method = {RequestMethod.POST}, params="productData")

and

@RequestMapping(method = {RequestMethod.POST}, params="productData[]")

but I didn't get success.

2
  • could you please explain why you want to do this (handling the request differently) ? maybe there is another approach that meets your requirements Commented Apr 1, 2015 at 10:09
  • Sure, I can explain: at the moment, I have out of box controller method submitForm; then, I added new feature (productData Map) in existing html form and I need to process this data to. I am not able to modify oob code, so I just created new method submitFormWithProductData, but how to call it... Commented Apr 1, 2015 at 10:15

2 Answers 2

2

productData has to be a property of a model object.

public class FormModel {
    private Map<String,String> productData = ...;
    ...
}

according to this you have to create a handler method like that:

@RequestMapping(....)
public String submitFormWithProductData(FormModel formModel) {
  ....
}

Spring will automatically bind the productData parameters to the according property in the FormModel object.

But I don't know why you want to handle it differently. You could add a hidden input field productDataSubmitted and add the following handler:

@RequestMapping(method = {RequestMethod.POST}, params="productDataSubmitted")
Sign up to request clarification or add additional context in comments.

1 Comment

You have: FormModel class and submitForm(FormModel) handler - they are out of box, it means, that you can not modify them, ok? You can add FormWithProductDataModel extends FormModel, agree? and add in FormWithProductDataModel property Map<String, String> productData, agree? then you need to handle this form on server side, you can not to modify default handler, so, you will add new method submitFormWithProductData(FormWithProductDataModel form), yes? What @RequestMapping parameters you will use for new handler?
1

Use @RequestBody Map<String,String> productData as argument int the controller method.

Here is a Blog and Read more...

For example:

@RequestMapping(value = "/add", method = RequestMethod.POST, consumes="application/json")
public void submitForm(@RequestBody Map<String,String> productData, Model model) {
    // implementation omitted
}

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.