如何从grails中的params列表中存储日期?

时间:2012-02-22 04:22:46

标签: grails date groovy

我尝试创建服务(下面的代码)。它获取从控制器传递的参数列表,并迭代参数并保存它。现在我一直收到这个错误:

    Error 500: Executing action [save] of controller [controller.EmploymentHistoryController] caused exception: Runtime error executing action
Servlet: grails
URI: /hrapp/grails/employmentHistory/save.dispatch
Exception Message: No signature of method: static service.EmploymentHistoryService.saveEmploymentHistory() is applicable for argument types: (org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap) values: [[position:ksfdj, startDate:Wed Feb 22 00:00:00 PHT 2012, ...]] Possible solutions: saveEmploymentHistory(java.lang.Object) 
Caused by: No signature of method: static service.EmploymentHistoryService.saveEmploymentHistory() is applicable for argument types: (org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap) values: [[position:ksfdj, startDate:Wed Feb 22 00:00:00 PHT 2012, ...]] Possible solutions: saveEmploymentHistory(java.lang.Object) 

这是我的代码:

def saveEmploymentHistory(params){

    params.company = [params.company].flatten()
    params.position = [params.position].flatten()
    params.responsibilities = [params.responsibilities].flatten()
    params.startDate = [params.startDate].flatten()
    params.endDate = [params.endDate].flatten()
    params.employee = [params.employee].flatten()

    params.company.eachWithIndex{ company, i ->
        def employmentHistory = new EmploymentHistory()

        employmentHistory.employee = params.employee[i]
        employmentHistory.company = company
        employmentHistory.position = params.position[i]
        employmentHistory.responsibilities = params.responsibility[i] 

        def tempStartDate = "${params.startDate_year[i]}/${params.startDate_month[i]}/${params.startDate_day[i]}"
        employmentHistory.startDate = new Date(tempStartDate)

        def tempEndDate = "${params.endDate_year[i]}/${params.endDate_month[i]}/${params.endDate_day[i]}"
        employmentHistory.endDate = new Date(tempEndDate)

        employmentHistory.save(flush:true)
    }



}

我打赌这个错误来自日期。我尝试了几个找到here的解决方案,但无济于事。还尝试了下面的代码,但仍然得到相同的错误...

employmentHistory.endDate = new Date(params.endDate[i])
employmentHistory.startDate = new Date(params.startDate[i])

我试过的另一个,但仍然是失败的:

    employmentHistory.endDate = params.endDate[i]
    employmentHistory.startDate = params.startDate[i]

有关如何正确执行此操作的任何建议?我对groovy不太熟悉。 :( 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

似乎你试图将此服务方法称为静态方法(如EmploymentHistoryService.saveEmploymentHistory()),但实际上需要一个实例方法。见Class Methods

要实现这一点,您需要在控制器的头部定义服务字段(它将由grails自动连接):

class EmploymentHistoryController {

   def employmentHistoryService

}

然后将其命名为:

def save() {

    employmentHistoryService.saveEmploymentHistory(params)

}

或者您也可以将服务方法设为static,但在这种情况下,您并不真正需要服务,它可以是任何类。