Editing ArrayList of objects with struts 2 form tag

99封情书 提交于 2020-01-27 03:09:46

问题


I have 2 classes - Student and Course. Both have all getters and setters defined on the attributes inside them. Using these two classes I am trying to build a web application with general functions like editing and adding students in different jsp pages. I student could have enrolled in multiple courses and marks for each course of each student should be editable inside the app.

Now i have a studentMarks.jsp which loads the data from an action class - StudentAction

public class StudentAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    private List<Student> studentList;
    private List<Course> courseList;
    private HashMap<Student,List<Course>> 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<Course>>();
        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";
    }
}

First a page is loaded that executes studentCourseList action which populates the page with all records(this page works fine) and after every record there is an edit button. Upon clicking edit, editCoursePage is called which set one student object and gets a list of courses for this student. Please note that i am not reusing the course list from the hashmap. In this page the student marks are displayed with the marks inside the textboxes to allow for editing. This page is displaying proper student information.

editCourse.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                      "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Edit courses</title>
    </head>
    <body>

        Student: <s:label value="%{student.name}" />

        <s:form action="editCourseAction">
            <s:hidden name="rollNo" value="%{student.rollNo}" />

            Courses: 

            <s:iterator value="courseList" var="courses">
                <s:label ><s:property value="name" /></s:label>
                <s:textfield name="marks" theme="simple"/>
                <br />            
            </s:iterator>

            <s:submit action="editCourseAction" value="submit" />

        </s:form>
    </body>
</html>

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
}

Now this form is displaying correct data but the problem is that upon submitting, it does not pass the values of courses back to editCourseAction. I have already tried all sort of OGNL expressions that can populate a list through form but cannot get it done. It always through NPE if i try to get the value of courseList in action class. Although it doesn send the rollNo correctly. Please tell me what am i doing wrong. Is it the syntax problem or is the approach that is wrong ?


回答1:


If you want to send back to Action a list of objects you need to specify an index in the name attribute:

instead of

<s:iterator value="courseList">
   <s:textfield name="marks" />
</s:iterator>

use

<s:iterator value="courseList" status="ctr">
   <s:textfield name="courseList[%{#ctr.index}].marks" />
</s:iterator>


来源:https://stackoverflow.com/questions/15338286/editing-arraylist-of-objects-with-struts-2-form-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!