Grails Domain Object ArrayList突然空了

时间:2012-01-25 21:30:10

标签: grails

我目前正在开发一个grails 1.3.7应用程序,它将用于组织讲座,研讨会和大学相关的东西。在这种情况下,导师应该能够分配学生之前选择的主题。为此,我使用一个简单的表'矩阵',右边的学生和表头的可用主题。现在导师希望我实现,表行是可拖动的。我使用jQuery插件实现了这一点,一切正常,但我还必须在某处保存行的新顺序,所以我决定将它保存为域类中的ArrayList。我实施了一个简单的逻辑来擦除列表,并在每次导师保存主题分配时重建它。

这些步骤的代码:
域类

class Seminar extends  AbstractLecture {
    ...
    ArrayList<Integer> studentOrder = new ArrayList<Integer>()
    ...

gsp: hiddenField保存新行顺序的序列化表示,由javascript函数设置(工作正常)。

<g:form action="saveDistribution">
    <g:hiddenField name="order" id="order" value=""/>
    ...

控制器
插件序列化函数返回一个我在这里分解的字符串,这也没问题,并且在这个块之后,id正确地包含在列表中。

def saveDistribution = {
....
    if (!params.order.toString().equals("")) {
        seminar.studentOrder.clear() //seminar is the domain object

        def temp = params.order.toString().replace('[','').replace(']','').replace("table-1=", "").replaceFirst('&', '')
        def split = temp.split("&")

        for (id in split) {
            seminar.studentOrder.push(Integer.parseInt(id)) //Works fine
        }
    }

    //If I check seminar.studentOrder here, the parsed values are present

    //Now I redirect to another function (forward would also work, doesn't matter here)
    redirect(action: "distributeTopics", id: params.id)

但是现在最大的问题是:当我在重定向后立即检查seminar.studentOrder时,列表完全是空的......我真的,真的不知道为什么。在最后几个小时里,我差点把我逼疯了:( 我尝试了一切我甚至无法想象的解决这个问题,但没有什么愿意工作。 现在我被敦促认为我在某个地方错过了一个重点。

我感谢你为解决这个问题所做的一切。 如果您需要更多代码,甚至是此上下文的完整代码,我将提供它。

非常感谢, 星

更新(为两个控制器功能添加完整代码):
以下是来自distributeTopics的代码: TopicTableDataWrapper类是一个简单的Java类,用于收集必要的数据,以便我在gsp中更容易。 正如您所看到的,我只是使用ArrayList studentOrder中提供的ID从数据库中提取学生对象,但由于列表始终为空,因此保存也不起作用......好吧,我被困在这里。

def distributeTopics = {
    def seminar = Seminar.findById(params.id)
    if (!seminar) {
        flash.message = "Error: Seminar not found. (Database error?)"
        redirect(controller: "lecture", action: "list")
    }

    /*
     * Gather all students and build an ArrayList<TopicTableDataWrapper>, which will be passed to the gsp.
     */
    def wrapperList = new ArrayList<TopicTableDataWrapper>()

    if (seminar.studentOrder.isEmpty()) {
        for (student in seminar.students) {
            //Selected topics
            def topics = seminar.getPrioritizedTopics(student)
            if (topics[0] == null) {
                topics = null
            }

            //Already assigned topic
            def assigned = seminar.getAssignedTopic(student)

            //Comment
            def comment = null
            if (seminar.comments.containsKey(student.id.toString())) {
                comment = seminar.comments.get(student.id.toString())
            }

            wrapperList.push(new TopicTableDataWrapper(student, assigned, comment, topics))
        }
    }
    else {
        for (studentId in seminar.studentOrder) {
            def student = User.findById(studentId)
            if (student == null) continue

            //Selected topics
            def topics = seminar.getPrioritizedTopics(student)
            if (topics[0] == null) {
                topics = null
            }

            //Possibly assigned topic
            def assigned = seminar.getAssignedTopic(student)

            //Comment
            def comment = null
            if (seminar.comments.containsKey(student.id.toString())) {
                comment = seminar.comments.get(student.id.toString())
            }

            wrapperList.push(new TopicTableDataWrapper(student, assigned, comment, topics))
        }
    }

    /*
     * Now we also need a list of all topics.
     * It is important that the list is sorted, because we must map the topic titles to the shortcuts in the gsp!
     * We do this by building a hashmap which just maps 'T1', 'T2', ... as keys to the topics
     */
    def topicList = seminar.topics.sort()
    def map = new HashMap<String, Topic>()

    def i = 1
    for (topic in topicList) {
        map.put("T" + i.toString(), topic)
        i++
    }

    [seminar: seminar, wrapperList: wrapperList, topicMap: map, id: seminar.id]
}

def saveDistribution = {
    def seminar = Seminar.findById(params.id)
    if (!seminar) {
        flash.message = "There was a problem while retrieving the seminar (database error?)."
        redirect(controller: "lecture", action: "list")
    }

    /*
     * Parse the corresponding radiogroup for every student in the seminar
     */
    for (student in seminar.students) {
        //Look if a topic has been assigned by checking the params -> "group <student.id>"
        if (params.containsKey("group " + student.id)) {

            //if that's the case, assign him this topic, but keep his selection for later corrections!
            def topic = Topic.findById(Integer.parseInt(params.get("group " + student.id).toString()))

            //Remove previously assigned topic for this seminar
            def prevTopic = seminar.getAssignedTopic(student)
            if (prevTopic != null) {
                student.removeFromAssignedTopics(prevTopic)
            }
            student.addToAssignedTopics(topic)
        }
    }

    if (!params.order.toString().equals("")) {
        seminar.studentOrder.clear()

        def temp = params.order.toString().replace('[','').replace(']','').replace("table-1=", "").replaceFirst('&', '')
        def splitParam = temp.split("&")

        for (id in splitParam) {
            seminar.studentOrder.push(Integer.parseInt(id))
        }
    }

    flash.message = "Distribution saved."
    seminar.save(failOnError: true) //Still empty list after redirect
    redirect(action: "distributeTopics", id: params.id)
}

1 个答案:

答案 0 :(得分:0)

虽然我们遗漏了您编写的代码的某些部分,但上面的评论可能是正确的。

我粘贴了Grails对象的save方法的链接: http://grails.org/doc/2.0.x/ref/Domain%20Classes/save.html

如果保存调用不起作用,请在 distributeTopics 方法中添加检索对象的方式。