【问题标题】:GRAILS: findALL() vs FindBy---(params.id)GRAILS:findALL() vs FindBy---(params.id)
【发布时间】:2015-09-10 13:33:05
【问题描述】:

大家好,

我正在尝试将参数从 URL 传递给 findAll() 方法。

LINE3我使用 findAll() 来定义鼠标。

LINE2 def house 在我进入页面时会带入参数DELAWARE:http://localhost:8080/TestApp/home/county/DELAWARE

House 将只显示一个实例而不是列表。无论如何传递 url 而不是 ["DELAWARE"]? (请参阅第 3 行)谢谢 :)

 def county() {
    def house = Home.findByCounty(params.id)  //sends only user related address to view
    def mouse = Home.findAll("from Home h where h.county= ?", ["DELAWARE"]);
    if (!house) {
    response.sendError(404)
    } else {

    [house:house, mouse:mouse ]
    }
}

工作代码 +1 @Danilo

 def county() {
            def house = Home.findAllByCounty  (params.id)  //sends only county specified thru URL e.g. http://localhost:8080/TestAPP/home/county/DELAWARE
    if (!house) {
        response.sendError(404)
    } else {
            [house:house ]
        }
    }

【问题讨论】:

  • 我不明白你想要什么。老实说

标签: grails


【解决方案1】:

findBy* 最多返回一行,如果要获取所有行,请使用 findAllBy*

要了解 Grails 将如何使用 URL,您必须查看 conf/UrlMappings.groovy。你可能会发现这样的东西:

static mappings = {
    "/$controller/$action?/$id?(.$format)?"{
    }
}

这意味着,当您调用 TestApp/home/county/DELAWARE 时,Grails 试图做的是使用主控制器 (HomeController),调用县方法 (def county(){...}) 并将 DELAWARE 传递为 id

如果在HomeController 的县方法内你有,这应该可以正常工作:

def filteredInstances = Home.findAllByCounty(params.id)

【讨论】:

  • findAllByCounty(params.id) 做到了,谢谢! +1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多