使用struts 2 form对象编辑对象的ArrayList

我有2个class – StudentCourse 。 两者都在其内部的属性上定义了所有getter和setter。 使用这两个类,我正在尝试构建一个Web应用程序,其中包含编辑和在不同jsp页面中添加学生等常规function。 我的学生可以注册多个课程,每个学生的每门课程的分数都应该可以在应用内编辑。

现在我有一个studentMarks.jsp ,它从一个动作类加载数据 – StudentAction

 public class StudentAction extends ActionSupport { private static final long serialVersionUID = 1L; private List studentList; private List courseList; private HashMap<Student,List> studentCourseList; private int rollNo; private String name; private String DOB; StudentService studentService; CourseService courseService; Student student; Course course; /**** Setters and getters for all the attributes here **/ public String studentCourseList() { this.studentList = studentService.getStudentRecords(); studentCourseList = new HashMap<Student, List>(); for(Student s : studentList) { this.studentCourseList.put(s,courseService.getStudentCourses(s.getRollNo())); } return "SUCCESS"; } public String editCoursePage() { this.student = studentService.getStudent(rollNo); this.courseList = courseService.getStudentCourses(rollNo); return "SUCCESS"; //loads the editCourse.jsp } public String editCourseAction() { System.out.print("This line displays null pointer exception" + courseList.size()); //courseService.editCourse(rollNo,this.courseList); return "SUCCESS"; } } 

首先加载一个页面,该页面执行studentCourseList动作,该动作用所有记录填充页面(此页面工作正常),并且在每个记录之后都有一个编辑按钮。 点击编辑后, 调用editCoursePage ,设置一个学生对象并获取该学生的课程列表。 请注意,我没有重复使用hashmap中的课程列表。 在此页面中,学生标记与文本框内的标记一起显示以允许编辑。 此页面显示正确的学生信息。

editCourse.jsp

       Edit courses   Student:    Courses:    

Course.java

 @Entity @Table(name="courses") public class Course { @Id @GeneratedValue @Column(name="id") int id; @Column(name="marks") int marks; @Column(name="rollNo") int rollNo; @Column(name="course") String course; //all getters and setters } 

现在这个表单正在显示正确的数据,但问题是在提交时,它没有将课程的值传递回editCourseAction 。 我已经尝试过所有类型的OGNL表达式,它们可以通过表单填充列表,但无法完成。 如果我试图在动作类中获取courseList的值,它总是通过NPE。 虽然它没有正确发送rollNo 。 请告诉我我做错了什么。 是语法问题还是错误的方法?

如果要将对象列表发送回Action,则需要在name属性中指定索引

代替

    

使用