JSP将JTO modelAttribute与DTP文件一起发送到Jquery中

时间:2017-11-15 04:44:07

标签: jquery spring jsp

这里我试图将我的表单modelAttribute和DTO文件一起发送到spring控制器,但DTO在控制器中为空。 其实我的模特属于我的形象'包含字段值,并且我需要向控制器发送更多数据,因此不是单独发送,而是在DTO文件中设置值并发送它。你能帮我解决这个问题吗?提前致谢。以下是我的代码详细信息,

<form:form name="myForm" modelAttribute="item">

var values = $("#item").serialize();
var dto = $("customDto").serialize();
    dto = {
        type : "New Item",
        userId : userId,
        status : "New",
    };

    $.ajax({
        type : "post",
        data : values + "&dto=" + dto,
        url : "/myApp/submit.do",
        success : function(msg) {
            jQuery("#msg").html("Form submitted");
        }
    });

Spring Controller:

@RequestMapping(value = "/submit")
public ModelAndView submitForm(
    @ModelAttribute("item") Item item,@ModelAttribute("dto") CustomDto customDto) {
    //Logic
  return model;
}

1 个答案:

答案 0 :(得分:0)

使用一个ModelAttribute,为此创建一个mapper类 使用2个对象(item和DTO),然后为此映射器输入modelattribute

     @RequestMapping(value = "/submit")
                    public ModelAndView submitForm(
                            @ModelAttribute MapperClass mapper) {
        ...               

                    }



      Class MapperClass {
        //Put all fields here which is sending from UI to controller

    private Item item;
private CustomDto customDto;
//create getters and setters

        }