当前位置: 代码迷 >> java >> 玩Excel模块怪异行为
  详细解决方案

玩Excel模块怪异行为

热度:124   发布时间:2023-07-26 14:35:04.0

我正在使用Play with Excel模块1.2.3。 在控制器中,我通过调用模型中定义的方法-学生来获得学生列表:

List<Student> students= Student.findStudents();

findStudents()定义为:

public static List<Student> findStudents() {
    List<Student> list =  Student.find("colA != colB").fetch();

    return list;
}

然后我通过以下方式呈现excel文件:

renderExcel("student_report");

在excel模板内部,我使用了JXLS。 例如:

<jx:forEach items="${students}" var="stu">
    ${stu.address.name}                    ${stu.name}
</jx:forEach>

现在,奇怪的事情发生了。 stu.name总是显示正常。 但是,仅当我在代码中完成了类似System.out.println(student.address.name)操作时,才会显示stu.address.name 否则,Excel报表中的单元格为空白。

谁能解释一下?

NB学生懒洋洋地引用地址

Jxls使用Apache Jexl处理诸如stu.address.name类的属性表达式。 Jexl使用反射来计算对象属性值。 但是反射和延迟加载不会进行,因为您不是使用真实对象而是使用代理对象。

当您执行System.out.println(student.address.name)将实例化实际对象,并且反射工作正常。

该问题的可能解决方案在此答案中描述。 。 或者,只要您需要将对象传递给Jxls时,就应该急于获取。

  相关解决方案