【问题标题】:Cannot get value from an object within a list in GSP无法从 GSP 列表中的对象获取值
【发布时间】:2012-05-05 10:30:47
【问题描述】:

我是 Groovy 和 grails 的新手。我想在员工列表中显示员工的当前项目。我只能通过去ProjectMember类来获取员工当前的项目。我的想法是获取每个员工的当前项目,然后将其放入列表中,然后在 .gsp 中进行迭代。

class EmployeeController {

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)

        def currentProject = [ProjectMember];
        List<Employee> employeeList = Employee.list(params)
        System.out.print("PREV SIZE" + currentProject.size())
        for(Employee emp: employeeList) {
            def current = ProjectMember.findAllByEmployeeAndEndDateIsNull(emp, [sort: "project.name", order: "asc"])
            System.out.print(current.project.name);
            // This is working, I can get the current projects of the employee
            if(!current.empty) {                     
                currentProject.add(current);
                // Here is the code I didn't understand I really don't know 
                // if the project is added in the list.
                // Everytime I try to display the contents of the list using foreach, 
                // I always get an error. MissingProperty
            }
        }

        [currentProject: currentProject,
         employeeInstanceList: Employee.list(params),
         employeeInstanceTotal: Employee.count()]
    }
}

class ProjectMember {
    Employee employee
    EmployeeRole role
    Date startDate
    Date endDate
    String notes

    static belongsTo = [project: Project]
}

class Project {
    String name
    String alternateName
    boolean useAlternateNameInResume = false

    String summary
    String duration
    String skills
    String technologies

    Date startDate
    Date endDate

    static hasMany = [members: ProjectMember]
}

最后是视图,list.gsp

<g:each in="${employeeInstanceList}" status="i" var="employeeInstance">
    <tr class="${(i % 2) == 0 ? 'odd' : 'even'} clickable" onclick="window.location='<g:createLink action='show' id='${employeeInstance.id}' />'">
        <td>${employeeInstance.idNo?.encodeAsHTML()}</td>
        <td>${fieldValue(bean: employeeInstance, field: "fullNameWithMiddleName")}</td>
        <td>${employeeInstance.position}</td>
        <td>
            <g:each in="${currentProject}" var="currentProject" status ="j">                                            
                ${fieldValue(bean: currentProject, field: "project.name")}
            </g:each>
            <g:if test="${currentProject.empty}">
                No projects yet
            </g:if> 
        </td>
    </tr>
</g:each>

除了不显示任何内容的当前项目之外,一切正常。

【问题讨论】:

  • 是的。什么都没有显示。我的代码有什么问题吗?
  • currentProject 被初始化为一个列表,其中 ProjectMember.class 作为其单个成员。这看起来很奇怪,并且可能导致 gsp 在尝试获取类的 project.name 属性时出现意外行为。也许“def currentProject = []”是正确的初始化?
  • 顺便说一句,在 gsp 中重载名称是不好的做法。这些变量的作用域并不像你想象的那么明显。
  • 我明白了,当前项目列表在 gsp 中不可读?我已将 def currentProject = [ProjectMember] 更改为 def currentProject = []。什么都没有再次显示。如何从控制器初始化 currentProject 在 gsp 中可读?
  • 使用调试器:)。在那个if(!current.empty)中放一个断点,看看程序运行时会发生什么,变量的值是多少等等。

标签: grails groovy gsp


【解决方案1】:

试试

<g:each in="${currentProject}" var="project">                                            
     ${fieldValue(bean: project, field: "name")}
</g:each>

【讨论】:

  • 静止。没发生什么事。我认为它不会进入循环。
  • 如果这行得通,我会感到非常惊讶,因为代码中从未引用定义的 var (currentProject)。
猜你喜欢
  • 2016-04-02
  • 1970-01-01
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
相关资源
最近更新 更多