Groovy脚本保存域类

时间:2012-04-23 19:57:43

标签: grails groovy gorm

我使用netbeans在脚本文件夹中创建了以下脚本。我无法保存域类。另外,如果我将整个项目部署为war文件,我可以使用Windows scheduler运行该脚本吗?

脚本

def json = ""
def txt = new URL("http://free.worldweatheronline.com/feed/weather.ashx?q=Singapore,Singapore&format=xml&num_of_days=1&key=b674fb7e94131612112609").text
def records = new XmlSlurper().parseText(txt)
def weather = records.weather
def dates = weather.date
def min = weather.tempMinC
def max = weather.tempMaxC
def img = weather.weatherIconUrl
def desc = weather.weatherDesc
def descLink = desc.toString().replaceAll(" ","%20")
println max
Weathers w  = new Weathers()
w.cityName="singapore"
w.day = dates
w.description =desc
w.max = max
w.img = img
w.min = min
w.url = "jk"

域类

package org.mPest

class Weathers {
    int id
    String day
    String min
    String max
    String img
    String description
    String cityName
    String url

static constraints = {
    id(blank:false, unique:true)
    cityName(blank:false)
    url(blank:false)

}

}

3 个答案:

答案 0 :(得分:0)

您无法直接使用域类。

请参阅this FAQ,了解如何使用src/groovy中的域类:

import org.codehaus.groovy.grails.commons.ApplicationHolder
//…
def book = ApplicationHolder.application.getClassForName("library.Book").findByTitle("Groovy in Action")

我不知道是否可以从Windows运行war pack脚本,但你可以使用grails Quartz plugin来安排你的任务......

答案 1 :(得分:0)

看看grails run-script command。您应该可以使用它来执行使用诸如Windows scheduler或cron之类的脚本,但是您必须拥有可供脚本执行的完整源代码(而不是war文件)。

答案 2 :(得分:0)

在Grails 2.x中,您应该使用Holders而不是ApplicationHolder。例如:

import grails.util.Holders
def validKeys = Holders.grailsApplication.getClassForName("com.vcd.Metadata").findAll { it.metadataKey }*.metadataKey