“没有这样的财产”,但Grails看到它并给出了“可能的解决方案”?

时间:2014-08-21 00:51:18

标签: grails

我在一些令人困惑的Grails语法中再次被抓住了。我得到的错误是:

  

没有这样的属性:productDescr for class:grailsTest.ProductType   可能的解决方案:productDescr

具有讽刺意味的是,Grails建议的productDescr是我想要使用的属性。不知怎的,但它知道它应该是productDescr。

这是我使用...

的示例代码

Model(ProductType.groovy):

package grailsTest

class ProductType {

    String productCode
    String productName
    String productDescr

    static constraints = {
        productCode (size: 3..20, unique: true, nullable: false)
        productName (maxSize: 45, blank: false)
        productDescr (maxSize: 500, blank: true)        
    }
}

Controller(ProductTypeController.groovy):

package grailsTest

class ProductTypeController {
    static scaffold = true

    def showSingleType() {
        def productType = ProductType.findByProductCode("prod1");
        render view: "showSingleType", model: [productType: ProductType]
    }    
}

和View(showSingleType.gsp):

<html>
<head>
    <title>Product Types</title>
</head>
<body>
  <div class="body">
        Selected product type: ${productType.productDescr }
  </div>
</body>
</html>

我首先通过脚手架添加我的测试数据(参见屏幕图片)。 enter image description here

然后我将网址更改为:

http://localhost:8080/GrailsTest/productType/showSingleType

响应是这个错误页面: enter image description here

我错过了一些愚蠢的环境,但我看不到森林中的树木:(

1 个答案:

答案 0 :(得分:3)

你的模型中有错误:

render view: "showSingleType", model: [productType: ProductType]

应该是:

render view: "showSingleType", model: [productType: productType]

编写它的方式,模型中productType变量的值是ProductType类,而不是在控制器操作中初始化的productType变量。由于您的GSP引用productType.productDescr且值productType存在ProductType类,因此系统正在尝试引用{{1}上名为productDescr的静态属性}},并且正如错误消息所示,该属性不存在。