0

I have a view form that contains a table with textfield inside it. I need to save the information of the table inside a database.

* javascript function *

function sendTableToServer()
{
    var table; 
    table = document.getElementById('myTable');
    var rowLength = table.rows.length;
    var tableArray = [rowLength -1];

    var JSONObject;

    for (var tblRow = 1; tblRow < rowLength; tblRow++){
            console.log("***** ROW CHANGE *****");

            JSONObject = {
                "rowIndex": "myTableRow" + tblRow,
                "partNo": table.rows[tblRow].cells[0].firstChild.value,
                "partName":table.rows[tblRow].cells[1].firstChild.value,                     
            };
        f24Array[tblRow] = JSONObject;
    }
    console.log(f24Array[rowLength-1]["rowIndex"]);
    console.log(f24Array.length -1);

        $.getJSON("f24BatcCreateEAF.do", {
            type: "F24", 
            pRefForm: "DVL",  
            pF24Values: tableArray 
            } 
        , function(ans)  {
            console.log("The row is" +ans.strVar);
        }
    );
};

* controller *

public @ResponseBody
AjaxReturnMessage createEaf( Model model, HttpServletRequest pRequest, @RequestParam String type,  @RequestBody String[] pF24Values ) throws Exception {

    long eafId=0;
    AjaxReturnMessage pARM = new AjaxReturnMessage();
    log.info( "entering creatEaf );

        try {

            System.out.println("calling gF24BatchService");

            eafId = gF24BatchService.createEAFRow( model, pp, type, pRefForm, pDescription );


            pARM.setId( eafId );
            pARM.setStrVar( "f24TableRow1" );
            pARM.setCode( AjaxReturnMessage.STATUS_ERROR );
        } catch ( Exception e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // return the jsp page only
        return pARM;

}

When i trigger the sendTableToServer function i get an error "404". But if i remove the pF24Values from the JSON call and he service then there is no error.

How would it be possible for me to "catch" pF24Values without having to create a new object type. is this possible?

Thanks

Yan
  • 5
  • 2

1 Answers1

0

You are performing a GET to your controller, so that controller can only capture data via request parameters. In addition, your getJSON function is sending all the data as request parameters as well.

Therefore your controller has to capture pF24Values as a @RequestParam, not as a @RequestBody.

If you want to send complex data structures to your controller you need to do a POST with the JSON as the request body (for instance) - although you can use GET for array data but not the way you are doing it here - each row needs to have a separate request param(e.g. pF24Values:one and pF24Values:two, etc - there are a few ways to do this).

nickdos
  • 8,348
  • 5
  • 30
  • 47
  • Thanks for you help mckdos So using a form and capturing the information as POST with the @RequestBody would fix the problem? could you give me a simple example with a simple json, i've seen alot of example that use objects of type "employee" or "anmal" as example but id like to stick to an array of string if possible. – Yan Jan 31 '14 at 13:47
  • I'm on my phone so I can't check code sorry and won't be near a PC for another day. But the jist is to encode your table data in JSON and then POST the data in the request body. Your controller shouldn't need much changing except to unmarshall the JSON (Spring May do this automatically ?). – nickdos Jan 31 '14 at 23:32
  • Have you tried using POST yet? This post may be helpful http://stackoverflow.com/questions/5908466/jquery-spring-mvc-requestbody-and-json-making-it-work-together – nickdos Feb 03 '14 at 23:43
  • Hello nickdos, i had to "estinguish some fire" thus i didn't had time to check the post. I'm planning of spending most of the rest of the week working on my spring project thus will have time to try the post solution. – Yan Feb 04 '14 at 22:03
  • nickdos, the post works if I remove the json Array but if i leave it i get the error POST http://localhost:8080/eaf/f24BatcCreateEAF.do 415 (Unsupported Media Type). As i said i'm pretty new to all this, is there some configuration i need to setup in my web.xml to use JsonArray. – Yan Feb 05 '14 at 14:55
  • So if i Stringify my JSON Array then in my controller i can get it as "@RequestParam String pJSONArray" but then i would need to convert it back to a list to be able to use it, my parameter becomes: [{"rowIndex":"f24tableRow1","partNo":"FOH0123","partName":"13-1234","receivingQty":"RAW PCB 2-W STRP-U AC/DC WH 10"]] ... :/ – Yan Feb 05 '14 at 15:45
  • Can't find an easy way to convert the String back to a list that i could use in my controller ... So when i try to post my json i get the error "POST http://localhost:8080/eaf/test.do 415 (Unsupported Media Type)" I tried adding the jackson mapping file to my class didn't work, i copied the code for the jacksonMessaconConverter bean in the app-servlet.xml – Yan Feb 05 '14 at 20:25
  • Alright i made it work with @RequestBody. he problem was with the JSONArray. thanks for you help nickdos – Yan Feb 06 '14 at 14:48