2010-09-22 46 views
-5

以下是我的代碼。 這裏我得到一個錯誤,'沒有這樣的屬性:它爲類:emp.EmployeeController'。 我想我在這裏做錯了事。 有什麼建議?Grails錯誤:沒有這樣的屬性:它的類:

def list ={ 

    def id=params.id 
    def results 

    String employee="SELECT empName, empDate, empNo from employee where empId='id'"  
    String referrer="SELECT empName, empDate, empNo from referer where empId='id'" 


    def employeeInstanceList = new ArrayList<Employee>() 
    Sql sql = new Sql(dataSource) 
    def joining=null 
    joining = sql.rows("select joining from employee_dates") 


    if (joining!=null)  
    results = sql.eachRow(employee) 
    employeeInstanceList=getCalculatedEmployeeData(results) 
    /*{ 

     def employee = new Employee() 
     employee.setempName it.empName 
     employee.setEmpNo it.empNo 
     employee.setEmpDate it.EmpDate 
     employeeInstanceList.add employee 
    }*/ 

    else 

    results = sql.rows (currentDaySql) 
    employeeInstanceList=getCalculatedEmployeeData(results) 
    /*{ 
     def employee = new Employee() 
     employee.setempName it.empName 
     employee.setEmpNo it.empNo 
     employee.setEmpDate it.EmpDate 
     employeeInstanceList.add employee  }*/ 

    } 


    [employeeInstanceList: [employeeInstanceList: employeeInstanceTotal: Employee.count()] 
} 


def getCalculatedImpactData(def results){ 
     def employee = new Employee() 
     employee.setempName it.empName 
     employee.setEmpNo it.empNo 
     employee.setEmpDate it.EmpDate 
     employeeInstanceList.add employee  }*/ 
    return [employeeInstanceList: employeeInstanceList] 
} 

感謝, Meghana

回答

2

有這麼多錯代碼,我不知道從哪裏開始...

但爲了避免越來越向下票我曾嘗試: )

我試圖將你的代碼複製到一個IDE中,並試着找出你正在嘗試實現但是不能實現的目標。

這是接近我能得到它:

def list = { 
     def id = parmas.id 
     def results 

     String employee = "SELECT empName, empDate, empNo from employe" 

     def employeeInstanceList 
     Sql sql = new Sql(dataSource) 
     def joining = sql.rows("select joining from employee_dates") 

     if (joining != null) { 
      results = sql.eachRow(employee) 
      employeeInstanceList = getCalculatedEmployeeData(results) 
     } 
     else { 
      results = sql.rows(currentDaySql) 
      employeeInstanceList = getCalculatedEmployeeData(results) 
     } 

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


    def getCalculatedImpactData(def results) { 
     def employeeInstanceList = new ArrayList<Employee>() 
     results.each { it -> 
      def employee = new Employee() 
      employee.empName = it.empName 
      employee.empNo = it.empNo 
      employee.empDate = it.EmpDate 
      employeeInstanceList.add(employee) 
     } 
     return employeeInstanceList 
    } 

,但它仍然是指不存在的變量currentDaySql,我不知道你想用做什麼'加入'的結果。

你真的需要閱讀Groovy的基礎知識。

2

我將第二leebutts答案......但只是一個指針,它關鍵字的使用通常侷限於封閉...所以不是在Java中這樣做:

List l = []; 
for (Iterator i = l.iterator(); i.hasNext();) { 
    ...do something adressing List l at position i... 
} 

你可以爲此在Groovy/Grails的:

list.each { it -> 
    ...do something with each object in the list (it)... 
} 

,但你應該在http://groovy.codehaus.org/Closures

0

閱讀了關於Groovy閉中發生錯誤的代碼塊大概是:

def getCalculatedImpactData(def results){ 
    def employee = new Employee() 
    employee.setempName it.empName 
    employee.setEmpNo it.empNo 
    employee.setEmpDate it.EmpDate 
    employeeInstanceList.add employee 
    return [employeeInstanceList: employeeInstanceList] 
} 

it沒有定義任何地方(提示:編譯錯誤告訴你這一點)。像塞巴斯蒂安所說的,it通常用於封閉;你在這裏定義的是一個函數。推測你想在這裏使用results(或其他),而不是it

我假設代碼中的某些內容(例如註釋打開/關閉)不在其中,並且在您看到錯誤和發佈代碼時添加了。否則,你會得到其他錯誤。

相關問題