jeudi 9 avril 2015

Binding request parameters with view model Spring MVC

I am using drag and drop to create a document with simple questions, check boxes and radio buttons.enter image description here On save button clicked i create a form and append all the dropped elements to that form to serialize it and make an ajax call to get data on server. I can't figure out how to do this correctly because the way i do it i don't get any data on server. This is what i've tried so far:



public class DocumentDefinitionViewModel {
private String name;
private DocumentStatus status;
private Set<DocDefQuestionViewModel> questions;
private Set<DocDefMultipleSelectViewModel> checkBoxes;
private Set<DocDefMultipleSelectViewModel> radioButtons;
//getters and setters


} Controller



@RequestMapping(value="/saveDocument", method = RequestMethod.GET)
public @ResponseBody String saveDocument(@ModelAttribute(value="doc")DocumentDefinitionViewModel doc){
//save the document on database
}


evaluations.js



$('#saveDocDef').click(function(){

//get the document definition name
var docName = $('#docName').val();
if(docName == ""){
alert("You need to choose a name for the document");
return;
}
//get the selected status
var docStatus = $('#selectStatus').val();
if(docStatus == ''){
alert("You need to select a valid status for the document");
return;
}

//create a form to serialize the defined elements
//make sure that the form doesn't exist
$('#frmSubmit').remove();
//append it to body
$('body').append('<form id="frmSubmit" action=""></form>');
//all elements are hidden
//append the name and the status of the document definition
$('#frmSubmit').append('<input type="hidden" id="name" name="name" value="'+ docName +'">');
$('#frmSubmit').append('<input type="hidden" id="status" name="status" value="'+ docStatus +'">');

/*************************QUESTION****************************/
//get all questions content
var questionText = $('.doc-def-quest-text');
$(questionText).each(function(index){
$('#frmSubmit').append('<input type="hidden" id="'+ questionText['index']+'" name="questionContent" value="'+ questionText.eq(index).val() +'">');

});

/**************************CHECK BOX*************************/

//get all checkboxes elements content
var ckbElements = $('.ckbElem');
//create new checkbox definition for each element
$(ckbElements).each(function(index){
var ckbQuest = $(ckbElements[index]).find('.ckbQuestionText').val();
$('#frmSubmit').append('<input type="hidden" id="'+ ckbElements['index']+'" name="question" value="'+ ckbQuest +'">');
var predefinedAnswers = $(ckbElements[index]).find('.ckbOptions')[0];
$(predefinedAnswers).each(function(i){
$('#frmSubmit').append('<input type="hidden" id="'+ predefinedAnswers['i']+'" name="options" value="'+predefinedAnswers.options[i].text +'">');
});
});

/**************************RADIO***************************/
//get all radio elements content
var radioElements = $('.radioElem');
//create new radio definition for each element
$(radioElements).each(function(index){
var radioQuest = $(radioElements[index]).find('.radioQuestionText').val();
$('#frmSubmit').append('<input type="hidden" id="'+ radioElements['index']+'" name="question" value="'+ radioQuest +'">');
var predefinedAnswers = $(radioElements[index]).find('.radioOptions')[0];
$(predefinedAnswers).each(function(i){
$('#frmSubmit').append('<input type="hidden" id="'+ predefinedAnswers['i']+'" name="options" value="'+predefinedAnswers.options[i].text +'">');
});
});

var result = $('form').serialize();
$( "#results" ).text( result );

$.ajax({
url :"/eMuse/admin/saveDocument",
dataType: 'json',
accepts :{
xml : 'text/xml',
text: 'text/plain'
},
data : {request : result},
success : function(response){
alert("Success");
}
});
});


Query string parametersenter image description here


Aucun commentaire:

Enregistrer un commentaire