Ann
Ann

Reputation: 473

struts 2 list iteration

I am new struts I have a frontend using struts and a backend using spring dao with entity types pojo class, I want to return a list of students in system.code I have tried is attached below I am getting the values only if I set the list to form class.

public class Studentform {
    private StudentEntity student;
    public StudentEntity getStudent() {
        return student;
    }
    public void setStudent(StudentEntity student) {
        this.student = student;
    }
    public void setStudent(ArrayList<StudentEntity> studentList) {
        // TODO Auto-generated method stub
        this.studentList=studentList;
    }
}

action class code to acessslist is as studform.setStudent(studentList);

public class StudentAction extends ActionSupport implements
    ModelDriven<Studentform> {
    ArrayList<StudentEntity> studentList=new ArrayList<StudentEntity>();
    //geters and setter for studentList
        public String stdus() {
        HttpSession session = ServletActionContext.getRequest().getSession();
        String id = (String) session.getAttribute("userid");
        studentList=controller.getStudentProfile();
        studform.setStudentList(studentList);
        System.out.println(studentList.size());
        return "SUCCESS";
    }
}

Upvotes: 0

Views: 92

Answers (1)

Boldbayar
Boldbayar

Reputation: 875

If you want to get Studentlist without using second method (setStudent(ArrayList<StudentEntity> studentList)) you have to add your student to your arraylist in your first method like studentList.add(student); something like this;

public class Studentform {

private StudentEntity student;
private List<StudentEntity> studentList = new ArrayList();

public StudentEntity getStudent() {
    return student;
}
public void setStudent(StudentEntity student) {
    this.student = student;
    studentList.add(student);
}
//add your list getter method here

Upvotes: 1

Related Questions