如何在每个请求一个会话中存储多个对象?

How to store multiple objects in a session one by one per request?

我正在为每个必须发送一个对象的应用程序工作 request.And 我必须将整个数据存储在一个会话中 variable.How 我可以这样做吗?我将数据发送到控制器并创建对象,然后尝试存储在会话中,但它不起作用,这是我的代码

MemberDependentBO memberdepinfo = new MemberDependentBO();

        memberdepinfo.setFirstName(getMemberFirstName().getValue().toString());
        memberdepinfo.setLastName(getMemberLastName().getValue().toString());
        memberdepinfo.setMiddleName(getMemberMiddleName().getValue().toString());
        memberdepinfo.setPassport(getMemberPassportNumber().getValue().toString());

    List<MemberDependentBO> listOfMemberAndDep = new ArrayList<MemberDependentBO>();

        listOfMemberAndDep.add(memberdepinfo);

        httpSession.setAttribute("insuredInfo", listOfMemberAndDep);

        System.out.println("List Size :"+listOfMemberAndDep.size());

Every time when i click the button,the object is coming from the controller .but each time i am getting the size of the list is 1.Any Help will be appreciated

首先尝试将 listOfMemberAndDep 拉出当前会话。如果不存在,则创建它,添加 memberdepinfo 并保存在会话中。如果它确实存在,只需添加 memberdepinfo 并重新保存在会话中。像这样....

List<MemberDependentBO> listOfMemberAndDep = (List<MemberDependentBO>)httpSession.getAttribute("insuredInfo");

if(listOfMemberAndDep == null){
    listOfMemberAndDep = new ArrayList<MemberDependentBO>();
}

MemberDependentBO memberdepinfo = new MemberDependentBO();
memberdepinfo.setFirstName(getMemberFirstName().getValue().toString());
memberdepinfo.setLastName(getMemberLastName().getValue().toString());
memberdepinfo.setMiddleName(getMemberMiddleName().getValue().toString());
memberdepinfo.setPassport(getMemberPassportNumber().getValue().toString();

listOfMemberAndDep.add(memberdepinfo);
httpSession.setAttribute("insuredInfo", listOfMemberAndDep);
System.out.println("List Size :"+listOfMemberAndDep.size());