简体   繁体   English

Grails - 如何将 personInstance ID 从 gsp 发送到域类?

[英]Grails - How to send a personInstance ID from gsp to domain class?

I'm a complete newbie in grails and I need you guys' help.我是 grails 的完全新手,我需要你们的帮助。 I have my sql query in the domain class.我在域类中有我的 sql 查询。 I put [1] to see the result but ultimately I'd like to send an argument in that place to display the result according to the person's id number.我放 [1] 来查看结果,但最终我想在那个地方发送一个参数来根据这个人的 ID 号显示结果。

def dataSource

def someMethod() {

    def sql = new Sql(dataSource)

    def resultRows = sql.rows('select * from result where id = ?', [1])
}

And this is what I have in my gsp.这就是我的 gsp 中的内容。

<g:each in="${personInstance.someMethod()}" status="i" var="results">
    <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
        <td>${results.column_1}</td>
        <td>${results.column_2}</td>
        <td>${results.column_3}</td>
    </tr>
</g:each>

How do I send a parameter from view to domain class?如何将参数从视图发送到域类? Please help.请帮忙。

Thank you in advance.先感谢您。

If you want to see how Grails 'would like' your controller and view code to look, try letting Grails generate the code for you.如果您想了解 Grails“希望”您的控制器和视图代码的外观,请尝试让 Grails 为您生成代码。 Even if you don't keep that code in your final project, it is still useful as an instructional tool.即使您没有在最终项目中保留该代码,它作为一种教学工具仍然很有用。

arc$ grails create-app Tester1
arc$ cd Tester1
arc$ grails
grails> create-domain-class Person
-- add some attributes to your Person domain class, save the file
grails> generate-all tester1.Person

Now go look at the PersonController.groovy, and the various views.现在去看看 PersonController.groovy 和各种视图。 Basically, it's marshal your data in the controller, pass it to the views, views operate on what they're given.基本上,它是在控制器中编组您的数据,将其传递给视图,视图根据给定的内容进行操作。

Very basic example of passing arbitrary data to the gsp:将任意数据传递给 gsp 的非常基本的示例:

// show method for an Adventure
def show(Adventure adventure) {
    // a String to pass to the gsp
    def attribute = 'Bilbo'
    // an Array to pass to the gsp
    def attributeList = ['Dwalin','Balin','etc']
    // create a map of values that are 'automagically' passed
    // to the show.gsp
    [adventure: adventure, hobbit: attribute, dwarves: attributeList]
}

The adventure, hobbit, and dwarves variables are all available in the gsp.冒险、霍比特人和矮人变量都在 gsp 中可用。 The template code likes to use verbose naming, like adventureInstance, but as long as your gsp code uses the keynames in the map you define, you're good to go.模板代码喜欢使用冗长的命名,如 AdventureInstance,但只要您的 gsp 代码使用您定义的地图中的键名,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM