将列表列表传递给mvc控制器

时间:2016-06-13 04:14:24

标签: javascript java json ajax spring-mvc

我正在使用spring MVC,我想将数据传递给后端服务器中的控制器。

@RequestMapping(value="/project/update")
public @ResponseBody projectWebForm update(HttpServletRequest request,
        HttpServletResponse response,
        @RequestBody projectWebForm input){

        ..............

        }

我使用@RequestBody标记将我的字段映射到名为projectWebForm

的类

在projectWebForm类中,我有3个字段

private String name;
private String age;
private List<List<Data>> list;

以下是我的数据类

 public class Data{
  private String data1;
  private String data2;

getters setters..... 
}

现在在jsp文件中我正在编写ajax来将数据传递给控制器​​

function update(name,age){
var option={
    headers:{'Content-type':'application/json;charset=utf-8'},
    contentType:'application/json;charset=utf-8',
    dataType:'json',
    async:true,
    type:'post',
    data:JSON.stringify({
        name:name,
        age:age,
        list:____________ <---- ????????
    }),
    url:'<%=request.getContextPath()%>/project/update.do',
    success:function(response){
        ......
        }
    },
};
$.ajax(option);
};

我现在可以轻松地将名称和年龄传递给控制器​​,因为它只有一个字符串 我需要一个列表列表,那么我应该如何构建列表以便将其映射到RequestBody?为了正确地将它传递给RequestBody,json应该是什么样的?一个例子可能吗?

2 个答案:

答案 0 :(得分:0)

function Form( form ){
var formData = $(form).serialize();
att=$(form).attr("action") ;
$.post(att, formData).done(function(response){

     var errorInfo="";

      if(response.status == "SUCCESS"){
          window.location.href="/";
      }
});
return true;}

序列化表单数据并在spring mvc

中发送到服务器
    <form name="myForm" th:object="${demo}" th:action="@{/demo}"  method="post"
    class="stdform" id="triggerform">

            <div class="form-group">
         <label for="usr">Name:</label>
     <input type="text" th:field="*{name}" class="form-control" />
     </div>
    <div class="form-group">
         <label for="usr">hobbies:</label>
            <input  th:field="*{hobbies[0].hobbiename}" type="text" class="form-control"   />
             <input th:field="*{hobbies[1].hobbiename}" type="text" class="form-control"   />
             <input th:field="*{hobbies[2].hobbiename}" type="text" class="form-control"  />
             <input th:field="*{hobbies[3].hobbiename}" type="text" class="form-control"  />
             <input th:field="*{hobbies[4].hobbiename}" type="text" class="form-control"   />
             <input th:field="*{hobbies[5].hobbiename}" type="text" class="form-control"   />


     </div>



            <button type="button"  onclick="return Form(this.form)" class="btn btn-primary">Get Started</button>

</form>

控制器部分

    @RequestMapping(value = "/demo", method = RequestMethod.GET)
public ModelAndView demo(HttpServletRequest request) {
        logger.info("Welcome to getInboxMessage Method inside the MessageController ");
        Profile profile=(Profile)request.getSession().getAttribute("profile");
        ModelAndView model=new ModelAndView("test");
        model.addObject("demo", new Demo());
        return model;

    }

@RequestMapping(value = "/demo", method = RequestMethod.POST)
public ModelAndView demopostdata(@ModelAttribute Demo demo,HttpServletRequest request) {
        logger.info("Welcome to getInboxMessage Method inside the MessageController ");
        Profile profile=(Profile)request.getSession().getAttribute("profile");
        ModelAndView model=new ModelAndView("test");
        model.addObject("demo", new Demo());
        return model;

    }

这是最简单的方法,因此您可以避免大量的jquery编码

答案 1 :(得分:0)

的js

var list = new Array();

var data = {
    data1 : getData1,
    data2 : getData2
};

list.push(data);

data:JSON.stringify({
        name:name,
        age:age,
        list: list
    }),

public class test {

   pirvate String name;
   private String age;
   pirvate List<data> list;

    ... s,g

}

控制器

@RequestMapping(value="/project/update", method = RequestMethod.POST)
public @ResponseBody String update(HttpServletRequest request, @RequestBody test test){

      .............. // break point
      return "";

}

你检查模型(测试)。