Grails:无法从控制器获取模型数据以进行查看
过去 3 周我一直在使用 Grails(学习和工作)。我一直致力于将 JSP/Servlet 应用程序移植到 Grails,移植该应用程序绝对是“有趣”的。
我遇到了一个问题,一直在搜索、阅读,但还无法解决。
在 GSP 页面中,我有一个文本字段和搜索按钮,用户可以在其中输入 ProductID。我有一个控制器,当单击搜索按钮时从 jQuery Ajax 方法调用该控制器。
// ----- Part of jQuery Ajax call --- $.ajax({ type : "post", url : '/${grailsApplication.metadata['app.name']}/product/checkProductAjax', data : "pid="+proID, // ----- Contoller code----- class ProductController { def scaffold = true def checkProductAjax = { def product= Product.findByProductId(params.pid) if(product) { [product: product] // model to get data in GSP page. render(product.toString() + ":" + product.id) } else { render("none") } } }
Ajax 调用一切正常。我面临的问题是如何获取模型(即产品数据返回到 GSP 页面,即 [product:product] 并在 GSP 中显示,例如产品名称:${product}
我怎样才能让它工作?我有阅读示例,其中提到仅设置模型 [产品:产品] 将有助于获取 GSP 中的数据
产品名称:${product} 在 GSP 页面中始终显示空白 产品名称:
请告诉我我做错了什么。干杯
! 杰·钱德兰
I have been using Grails for last 3 weeks (learning and working). I have been working on porting a JSP/Servlet application to Grails and it has been absolute "fun" porting the application.
I have facing an issue and have been searching, reading but could not solve it yet.
In the GSP page I have a textfield and search button where user enters ProductID. I have a controller which is called from jQuery Ajax method when a search button is clicked.
// ----- Part of jQuery Ajax call --- $.ajax({ type : "post", url : '/${grailsApplication.metadata['app.name']}/product/checkProductAjax', data : "pid="+proID, // ----- Contoller code----- class ProductController { def scaffold = true def checkProductAjax = { def product= Product.findByProductId(params.pid) if(product) { [product: product] // model to get data in GSP page. render(product.toString() + ":" + product.id) } else { render("none") } } }
Ajax call and everything works fine. The problem I am facing is how to get the model (i.e. the Product data back to the GSP page i.e. [product: product] and display in GSP as for e.g. Product Name: ${product}
How can I get it working? I have read examples where it is mentioned that just setting the model [product: product] will help to get the data in GSP.
Product Name: ${product} always shows blank in the GSP page Product Name:
Please tell me what I am doing wrong.
Cheers!
Jay Chandran
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
[product:product]
和render(product.toString() + ":" + Product.id)
不兼容。当您看到一个控制器操作的最后一行是像[product:product]
这样的映射时,这是隐式返回值,因为它是操作闭包的最后一个语句 - 它相当于return [产品:产品]
。但是,如果方法中间有一个映射,它就会被创建并丢弃。它几乎相当于既然您正在进行 Ajax 调用,那么将产品放入模型中就没有意义,因为您不会重新渲染 GSP。您将呈现文本、JSON、XML 或客户端 JavaScript 将用来更新 html 的某些子集的其他内容。因此,您可能想要更接近的东西
,然后您可以使用 jQuery 或 Prototype 来评估 JSON 并提取 JavaScript 中的数据。
[product: product]
andrender(product.toString() + ":" + product.id)
are incompatible. When you see a controller action whose last line is a map like[product: product]
this is the implicit return value since it's the last statement of the action closure - it's the equivalent ofreturn [product: product]
. But if you have a map in the middle of a method it's just created and discarded. It's pretty much equivalent toSince you're making an Ajax call though, putting the product in the model doesn't make sense since you aren't going to re-render the GSP. You're going to render text, JSON, XML, or some other content that the client-side JavaScript will use to update some subset of the html. So you probably want something closer to
and then you can use jQuery or Prototype to evaluate the JSON and extract the data in your JavaScript.
您可能想要使用为此类工作创建的 grails 标签;它为您封装了所有 AJAX 代码
http://www.grails.org/Ajax
you probably want to use the grails tags that are created for this type of work; it wraps the AJAX code all up for you
http://www.grails.org/Ajax