Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我们可以序列化并发送一个表单,其中包含主对象(Ajax、SpringMVC)中的对象列表吗_Java_Jquery_Ajax_Json_Spring Mvc - Fatal编程技术网

Java 我们可以序列化并发送一个表单,其中包含主对象(Ajax、SpringMVC)中的对象列表吗

Java 我们可以序列化并发送一个表单,其中包含主对象(Ajax、SpringMVC)中的对象列表吗,java,jquery,ajax,json,spring-mvc,Java,Jquery,Ajax,Json,Spring Mvc,很抱歉,我无法共享我的代码。我将尝试为我的情况提供一个适当的例子 班长: public class Community { private int personCount; private List<Person> persons; //getters and setters for all } 现在,我想从JSP序列化并发送一个带有AJAX调用的表单。 在JSP中,我使用 <div th:each="paramet

很抱歉,我无法共享我的代码。我将尝试为我的情况提供一个适当的例子

班长:

    public class Community {

       private int personCount;
       private List<Person> persons;

       //getters and setters for all
   }
现在,我想从JSP序列化并发送一个带有AJAX调用的表单。 在JSP中,我使用

<div th:each="parameter,iterStat : ${persons}" class="personsContainer">
  <div th:text="'Name: ' + ${parameter.name}"></div>
   ...
我的控制器方法:

     @RequestMapping(value = "/getPeoplesJson", method = RequestMethod.POST)
public @ResponseBody String getCommunityPeopleValuesJson(HttpServletRequest request, HttpSession session,
        Locale locale, Model model, Device device, Principal principal, @Valid Community post,
        BindingResult result)
{
    int count = post.getPersonCount();
    if (post != null && post.getPersons() != null)
    {
        //calls to service and so on...
        return "true";
    }
    return false;
}

在这里,我能够正确地检索人员计数,但整个问题与人员列表有关。它总是空的…

您的人员列表总是
null
,因为jQuery的
serialize()
函数只考虑表单元素。从:

描述:将一组表单元素编码为字符串以供提交

.serialize()方法以标准URL编码表示法创建文本字符串。它可以作用于已选择单个表单控件的jQuery对象,例如
、和
$(“输入,文本区域,选择”).serialize()

但是,通常更容易选择
本身进行序列化

例如,如果您有一个
元素并将名称列为选项,serialize函数将正确序列化所有选定的
元素:

<form>
    <select multiple th:each="parameter,iterStat : ${persons}" class="personsContainer">
        <option selected th:text="'Name: ' + ${parameter.name}"></option>
    </select>
</form>
这意味着您的表单应该如下所示:

<form>
    <div th:each="parameter,iterStat : ${persons}" class="personsContainer">
        <input type="text" th:value="${parameter.name}" th:name="'people[' + ${iterStat.index} + '].name'">
        <input type="text" th:value="${parameter.age}" th:name="'age[' + ${iterStat.index} + '].age'">
    </div>
</form>


此外,如果要使用
serialize()

谢谢您的回复,那么ajax的
数据类型应设置为
应用程序/x-www-form-urlencoded
。你是说这不可能吗。我不确定我是否理解您的意思,但这仍然作为同一级别的参数(作为CommunityBean的直接属性)。我想知道是否有一种方法可以将这些值直接映射到社区Bean中的Person Bean。谢谢您的帮助。我以前确实尝试过这种方式,但可能服务器没有正确发布。我自信地再试了一次(因为你的回答),效果很好。
<form>
    <select multiple th:each="parameter,iterStat : ${persons}" class="personsContainer">
        <option selected th:text="'Name: ' + ${parameter.name}"></option>
    </select>
</form>
personCount=2&persons[0].name=joe&persons[0].age=20&persons[1].name=john&persons[1].age=25`
<form>
    <div th:each="parameter,iterStat : ${persons}" class="personsContainer">
        <input type="text" th:value="${parameter.name}" th:name="'people[' + ${iterStat.index} + '].name'">
        <input type="text" th:value="${parameter.age}" th:name="'age[' + ${iterStat.index} + '].age'">
    </div>
</form>