How to iterate the session in JSP page?

后端 未结 2 1117
悲哀的现实
悲哀的现实 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:01

    There is no need for iterator.
    In your action set the values to session attributes.
    In Jsp retrieve them using there name e.g.:

    Name:<s:property value="#session.a" /><br>
    Age:<s:property value="#session.b" /> <br>
    Marks:<s:property value="#session.c" /><br>
    Country:<s:property value="#session.d" /> 
    

    This should do.

    Update 1:

    For displaying all the set of values entered :
    1) Create a POJO to store all these values (call Person).
    2) In your action maintain a arraylist of Person objects.
    3) At every submit create a new person object and add it to person list.
    4) In jsp iterate over the person list and display all the values.
    NO Need for session. Avoid using session as much as possible.

    0 讨论(0)
  • 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<Person> getPersonList() {
      List<Person> personList = (List<Person>) session.get("personList");
      if (personList == null) {
        personList = new ArrayList<Person>();
        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

    <s:form action="verify">
      <s:textfield name="person.name" label="Enter Username" /><br>
      <s:textfield name="person.age" label="Enter Age" /><br>
      <s:textfield name="person.marks" label="Enter Marks" /><br>
      <s:textfield name="person.country" label="Enter Country" /><br> 
      <s:submit value="Click" id="submit"  /> 
    </s: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

    <s:iterator value="#session.personList">
      Name:<s:property value="name" /><br>
      Age:<s:property value="age" /> <br>
      Marks:<s:property value="marks" /><br>
      Country:<s:property value="country" /><br>
    </s:iterator>
    
    0 讨论(0)
提交回复
热议问题