使用视图模型Spring MVC绑定请求参数

时间:2015-04-08 20:06:15

标签: jquery spring

我正在使用拖放来创建一个包含简单问题,复选框和单选按钮的文档。enter image description here点击保存按钮后,我创建一个表单并将所有删除的元素附加到该表单以序列化它并制作ajax调用以获取服务器上的数据。我无法弄清楚如何正确地做到这一点,因为我这样做的方式我不会在服务器上获得任何数据。这是我到目前为止所尝试的:

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

} 控制器

@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");
            }
        });
    });

查询字符串参数enter image description here

0 个答案:

没有答案