简单(?)grails如果声明

时间:2014-09-30 14:02:05

标签: grails

所以尝试做某事(我觉得应该很简单) 在代码中进一步设置一个值,然后执行if语句来检查我设置它的内容。 设置变量的代码段。复制在下面

if ( params.headers == null) {
    def headline="1"
    render ("Starting to read on line 1<BR>")
} else {
    def headline="0"
    render ("Skipping line 1<BR>")
}

然后在控制器中向下进一步向下执行以下操作

if ( headline == "0") {
    render "Skipped line for ${fields[0]}"
    headline = "1"
} else {

每当我运行此操作时,我都会收到以下错误消息&#34;

No such property: headline for class: regmap.CountryuploadController. Stacktrace follows:
Message: No such property: headline for class: regmap.CountryuploadController
    Line | Method
->>   25 | doCall    in regmap.CountryuploadController$_loadcsv_closure1$$EOrLxdfM
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     34 | eachLine  in org.grails.plugins.csv.CSVReaderUtils
|     47 | eachLine  in     ''
|    140 | doCall    in CsvGrailsPlugin$_closure4_closure12
|     22 | loadcsv . in regmap.CountryuploadController$$EOrLxdfM
|    198 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1142 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    617 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread

有什么想法吗? 我显然遗漏了一些东西,因为我甚至试图在彼此之后运行它。

1 个答案:

答案 0 :(得分:3)

由于您在headlineif块内声明else变量,因此其范围仅限于这些块,这就是您在尝试引用时遇到错误的原因它以后。你想要这样的东西:

def headline
if ( params.headers == null) {
    headline="1"
    render ("Starting to read on line 1<BR>")
} else {
    headline="0"
    render ("Skipping line 1<BR>")
}