fetch records dynamically without getter

2019-09-25 06:20发布

问题:

I am fetching records dynamically using struts2 framework. eg. ExamId 1 has 6 subject. ExamId 2 has 8 subject. ExamId 3 has 2 subject. etc. but the problem is i dont have getSubject1() setSubject1().....getSubjectN() setSubjectN()

[EDITED] Now issue is how should i retreive it wihtout setters and getters.I am using JQuery Jtable reference : http://www.simplecodestuffs.com/crud-operations-in-struts-2-using-jtable-jquery-plugin-via-ajax/

My Bean class

 @Entity
public class SubjectBean
{
 @Id
 @GeneratedValue
 private int SubjectId;
 private String paperCode;
 int totalNumber;

 @OneToMany(cascade=CascadeType.ALL)
 private Collection<Subject> subjectList;

 private int examId;
     //setters and getters of all
}

Subject class

   @Entity
public class Subject
{
 @Id
 @GeneratedValue
 private int Id;
 private String subjectId;
 private int capacity;
  //setters and getters
}

action class.

class  CrudAction
{
 public String insert() throws IOException
 {

    String examid =(String) httpSession.getAttribute("examId");
    Integer examId =Integer.parseInt(examid);

    Map<String, String[]> requestParams = request.getParameterMap();

    //inserting data using hibernate

     return "success";
 }
 punlic String getAll()
 {
       // how can i access?
  }
}

回答1:

You are ignoring fundamentals of both Struts2 and OGNL. And this is a perfect case of the XY problem.

The only things you need are the getter and the setter for the Collection/List, that obviously must be declared at class level, not method level, or it'll be destroyed once the method execution is over. Since in your case the Collection/List is inside another object, then that object must be declared at class level, and you will need getter/setter for it too:

class CrudAction {
    private SubjectBean sb;
    // getter and setter

Also use a List to have order and use the [] notation and the dot notation to specify it in page, with the help of IteratorStatus object

<s:iterator value="sb.subjectList" status="ctr">
    <s:property value="sb.subjectList[%{#ctr.index}].id" />
    <s:hidden    name="sb.subjectList[%{#ctr.index}].id" />
    <s:textfield name="sb.subjectList[%{#ctr.index}].subjectId" />
    <s:textfield name="sb.subjectList[%{#ctr.index}].capacity" type="number" />
</s:iterator>