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

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

问题描述

我有 2 个班级 - StudentCourse.两者都在它们内部的属性上定义了所有的 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 被调用,它设置一个学生对象并获取该学生的课程列表.请注意,我没有重复使用哈希图中的课程列表.在此页面中,学生分数与文本框内的分数一起显示以允许编辑.此页面显示正确的学生信息.

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 ?

推荐答案

如果您想将对象列表发送回 Action,您需要在 name 属性中指定一个 index:

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 表单标签编辑对象的 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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