使用struts 2 form对象编辑对象的ArrayList [英] Editing ArrayList of objects with struts 2 form tag

查看:113
本文介绍了使用struts 2 form对象编辑对象的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

现在我有一个 studentMarks。 jsp 从动作类加载数据 - StudentAction

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";
    }
}

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

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
}

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

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 ?

推荐答案

如果你想要发回一个对象列表,您需要在名称属性中指定索引

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

而不是

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

使用

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

这篇关于使用struts 2 form对象编辑对象的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆