简体   繁体   English

如何在JSP页面中迭代会话?

[英]How to iterate the session in JSP page?

How to iterate the session and keep the every submit value in same page till the session end? 如何迭代会话并将每个提交值保留在同一页面中,直到会话结束?

<html>
<body>
<s:form action="verify">
    <s:textfield name="stuname" label="Enter Username" /><br>
    <s:textfield name="stuage" label="Enter Age" /><br>
    <s:textfield name="stumarks" label="Enter Marks" /><br>
    <s:textfield name="country" label="Enter Country" /><br> 
    <s:submit value="Click" id="submit"  /> 
</s:form>
<s:iterator>
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" />
</s:iterator>
</body>
</html>

There is no need for iterator. 不需要迭代器。
In your action set the values to session attributes. 在您的操作中,将值设置为会话属性。
In Jsp retrieve them using there name eg: 在Jsp中,使用那里的名称检索它们,例如:

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: 更新1:

For displaying all the set of values entered : 为了显示所有输入的值集:
1) Create a POJO to store all these values (call Person ). 1)创建一个POJO来存储所有这些值(调用Person )。
2) In your action maintain a arraylist of Person objects. 2)在您的操作中,维护一个arraylist of Person对象的arraylist of Person
3) At every submit create a new person object and add it to person list. 3)在每次提交时,创建一个新的人员对象并将其添加到人员列表。
4) In jsp iterate over the person list and display all the values. 4)在jsp中,遍历人员列表并显示所有值。
NO Need for session. 无需会话。 Avoid using session as much as possible. 尽可能避免使用会话。

In the JSP and in action you are dealing with objects or an object that is array of objects. 在JSP中,实际上,您正在处理对象或作为对象数组的对象。 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 . 不管使用什么结构,对象具有诸如NameAgeMarksCountry等属性,并且它们属于一个名为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. 它是同一Person对象。

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) 此表单将不显示值(因为这些值已映射到动作属性Person且为空)

How to fill the personList I have already explained in the previous example . 如何填充personList我已经在前面的示例中说明了。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM