1

I'm using ajax call to submit a form. But I'm not able to access the parameter as a modelattribute.

Ext.Ajax.request({
                url: 'url',
                headers: { 'Content-Type': 'application/json' },
                params : Ext.JSON.encode(form.getValues()), //this is the parameter in jason pattern

                success: function(response, options) {
                    var result = Ext.JSON.decode(response.responseText, true);

                    if (result.success) {

                        Ext.Msg.show({
                            title:'DataBase',
                            msg: result.message,
                            buttons: Ext.Msg.OK,
                            icon: Ext.Msg.INFO
                        });

My doubt is that can i access this parameter as a modelattribute as in the given code below. ?

@RequestMapping(value="/employee/new", method = RequestMethod.POST )
@ResponseBody public String doAddEmp(@RequestBody String Str){
    try{
        empServ.setError();
        if(empServ.addEmp(Str)){
            return "{success:true, message:'Insertion Successfull'}";
        }else
            return "{success:false, message:\""+empServ.returnError()+"\"}";

    }
    catch(Exception e){
        e.printStackTrace();
        return "{success:false, message:'SQL ERROR'}";

    }

}

I would be very thankful to hear a good suggestion for my doubt. If this is a wrong pattern(for the parameter) please suggest me the right pattern.

1 Answer 1

1

You are confusing @ModelAttribute with @RequestBody: you are saying you want to access the modelattribute but you are instead using the requestbody (both are binding data mechanisms, one from the params the other from the body).

With Ajax requests it is better to use @RequestBody as you did in your code.

Make sure:

  • Ext.Ajax.request has "method: 'POST'"
  • Your controller is annotated with @RestController
  • Change "@RequestBody String Str" to "@RequestBody Employee employee"
  • Ensure your form elements names match the employee object fields
Sign up to request clarification or add additional context in comments.

6 Comments

Technically @RequestBody String Str should also work, but as you asked for the right pattern, for a REST api it is common to let Spring do the mapping between the json object posted and the model (Employee in your case). So that you can call the empServ.addEmp(employee). Also make sure "url: '/employee/new'".
Sir i have one more doubt @RequestMapping(value="/employee/new", method = RequestMethod.POST ) @ResponseBody public String doAddEmp(@ModelAttribute Employee employee){ empServ.addEmp(Employee); } How to send the param from ajax request for this function.?
For that (@ModelAttribute) you should simply submit the form. If you want ot use Ajax better to use @RequestBody as you did before. Here some links to look at alvinalexander.com/javascript/… and techzoo.org/javascript-tutorial/extjs/…. In this latter example, the data is posted with jsonData: book.
Also, I would add a break point in java on the spring controller to see if it reaches it. It may be an issue with your mapping and not your code.
Thank you sir.! I could do it with the @RequestBody annotation. It was a problem with the Date formatting. String can't be mapped to date format.
|

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.