简体   繁体   English

在ag中使用get进行Grails:每个

[英]Grails using a get within a g:each

I'll acknowledge right off that the title is probably not the right reference here so please let me know if I'm using the wrong phrase and I'll correct! 我马上就承认标题可能不是正确的参考,因此,如果我使用的短语错误,请告诉我,我会纠正!

Overview - I have an Form.gsp and corresponding _form.gsp, into which I feed a load of values from an SQL query, which includes the ID from another controller (called Object, multiple object being linked to a form). 概述-我有一个Form.gsp和相应的_form.gsp,我在其中向其中提供了来自SQL查询的值的负载,其中包括来自另一个控制器(称为Object,将多个对象链接到表单)的ID。

During the construction of the table which lists all these objects I need to take the object id and then get it to go off to the Object controller, use the ID to return the objects name and populate the text box with that value. 在构建列出所有这些对象的表的过程中,我需要获取对象ID,然后将其转到Object控制器,使用ID返回对象名称,并使用该值填充文本框。 But I can't work out how to do this, here's what I've tried (cutting out the rubbish of course). 但是我不知道该怎么做,这就是我尝试过的方法(当然是要清除掉垃圾)。

The Objects ID is called object_id and the g:each var is item. 对象ID称为object_id,而g:each var为item。 So I've tried to insert it my Object.get(x) as: - 因此,我尝试将其插入到我的Object.get(x)中:-

value = "${Object.get(item.object_id)}"

But this give the error No signature of method: static java.lang.Object.get() is applicable for argument types: (java.math.BigDecimal) values: [4] 但这会给错误No signature of method: static java.lang.Object.get() is applicable for argument types: (java.math.BigDecimal) values: [4]

Incidentally the ID value is 4 that I need to look for... 顺便说一句,我需要查找的ID值为4。

I've also tried 我也尝试过

value = "${Object.get(${item.object_id})}"

But I get an Unclosed GSP expression error. 但是我收到一个未Unclosed GSP expression错误。

This is within my _form.gsp 这是在我的_form.gsp中

<table id="eventList">
        <g:each in="${results}" status = "i" var="item">
            <tr>
                <td colspan="3">
                    <table id = "sub">
                        <tr>
                            <td><b>Object</b></td><td>
                            <g:select style="width:550px;"id="objectID[$i]" name="objectID[$i].id" from="${framework.Object.list(sort:"objDesc")}" optionKey="id" required="" value="${item.object_id}" class="many-to-one"/>

                            <richui:autoComplete name="objectID[$i].id" value = "${Object.get(item.object_id)}" action="${createLinkTo('dir': 'object/searchAJAX')}"    />

                            </td>

                        </tr>
                    </table>                                
                </td>       
            </tr>
        </g:each>
    </table>

Also worth noting that I do just want it to return and display the objDesc value not the whole entry and am not sure how I tell it to basically say "give me back the objDesc where the ID = item.object_ID. 同样值得注意的是,我只是希望它返回并显示objDesc值而不是整个条目,并且不确定如何告诉我基本上说“给我ID = item.object_ID的objDesc。

Thanks for any help! 谢谢你的帮助!

I have come up with a workaround for this, as I'm using SQL in my controller to send the info to the gsp I simple added in another JOIN to the statement to go and get the objects description value from the object table before I passed through the details, I can now reference it with an "item.object_description" 我想出了一个解决方法,因为我在控制器中使用SQL将信息发送到gsp,我简单地在另一个JOIN语句中添加了该信息,以便在传递之前从对象表中获取对象描述值通过详细信息,我现在可以使用“ item.object_description”进行引用

However, I'd still like to know if its possible to do the look-up as originally intended so I wont mark this as answered just yet... 但是,我仍然想知道是否有可能按照最初的意图进行查找,因此我暂时不会将其标记为已回答...

The first error you're getting: 您遇到的第一个错误:

No signature of method: static java.lang.Object.get() is applicable for argument types: (java.math.BigDecimal) values: [4] 没有方法签名:静态java.lang.Object.get()适用于参数类型:(java.math.BigDecimal)值:[4]

Indicates that the Object.get() call in your <richui> tag isn't actually accessing your domain object; 指示<richui>标记中的Object.get()调用实际上不是在访问您的域对象; it's just the generic java.lang.Object class. 它只是通用的java.lang.Object类。

There's a couple of ways you might work around this. 您可以通过以下两种方法解决此问题。

First, you could just import the domain class into your main Form.gsp, and then _form.gsp should inherit it when you it: 首先,您可以将域类导入到主Form.gsp中,然后_form.gsp在您继承时应继承它:

<%@ page import="com.mysite.DomainObject" %>

Make sure to change the "com.mysite." 确保更改“ com.mysite”。 portion to match the package name of your domain object. 部分以与您的域对象的包名称匹配。 The default package in a grails application is the same as the name of your application. grails应用程序中的默认软件包与您的应用程序名称相同。 So, your import might look like <%@ page import="myApp.Object" %> 因此,您的导入可能看起来像<%@ page import =“ myApp.Object”%>

Second (more elaborate) way: in your controller, you could pass the actual domain objects you want to access to the gsp. 第二种方法(更复杂):在控制器中,您可以将要访问的实际域对象传递给gsp。 So, at the end of your controller action, you might have something like: 因此,在控制器操作结束时,您可能会遇到类似以下内容的情况:

def sqlResults = someService.queryMethod(parameter1, parameter2)
def domainObjects = DomainObject.findAllBy( it.property == parameter3 )
[results: sqlResults, myObjects: domainObjects]

Given the way you're iterating through the results object using g:each, you may actually need to map the domain objects to the members of your sqlResults. 考虑到使用g:each遍历结果对象的方式,您实际上可能需要将域对象映射到sqlResults的成员。 This method is certainly more elaborate than just importing the domain object to your gsp, though it bears considering. 尽管需要考虑,但此方法肯定比将域对象导入到gsp更复杂。 Why run sql statements to get the IDs of your domain object when you can use the Grails frameworks findAll methods? 当您可以使用Grails框架的findAll方法时,为什么运行sql语句来获取域对象的ID?

Side note: If you're actually naming your domain class Object, you might consider refactoring with a more domain-specific name, just to avoid any ambiguity. 旁注:如果您实际上是在命名域类Object,则可以考虑使用更多特定于域的名称进行重构,以避免任何歧义。 I imagine you just did that for your post, so that it would be more generic, which is fine, but consider using a class name that isn't already in use at the lowest levels of the language. 我想您只是为您的帖子做的,所以它会更通用,这很好,但是请考虑使用在语言的最低级别上尚未使用的类名。

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

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