How to iterate the session in JSP page?

后端 未结 2 1115
悲哀的现实
悲哀的现实 2021-01-27 18:50

How to iterate the session and keep the every submit value in same page till the session end?




    <         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-27 19:20

    In the JSP and in action you are dealing with objects or an object that is array of objects. It doesn't matter what a structure is used, what does matter that the object has properties such as Name, Age, Marks, Country and they are belong to the one entity name it Person.

     @Entity
     public class Person {
       @Id
       @GeneratedValue
       private int id;
       private String name;
       public int getId() {
         return id;
       }
       public void setId(int id) {
         this.id = id;
       }
       public String getName() {
         return name;
       }
       public void setName(String name) {
         this.name = name;
       }
       ...
       //other fields
    } 
    

    Now, the session should contain many of such objects, therefore you should create a list and put it into session.

    public List getPersonList() {
      List personList = (List) session.get("personList");
      if (personList == null) {
        personList = new ArrayList();
        session.put("personList", personList);
      }
      return personList;
    }
    

    Then, in the action you should have an object that maps to the form fields, which when submitted saved into session. It is the same Person object.

    private Person person = new Person();
    //public getters and setters
    

    Now the map the form

    
      



    this form will not display values (because the values are mapped to the action property Person and it's empty)

    How to fill the personList I have already explained in the previous example. Now to iterate the values (persons) from it, that is in the session use

    
      Name:
    Age:
    Marks:
    Country:

提交回复
热议问题