Grails:无法从控制器获取模型数据以进行查看

发布于 2024-09-16 23:58:21 字数 1121 浏览 10 评论 0原文

过去 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞笑我太脆弱 2024-09-23 23:58:21

[product:product]render(product.toString() + ":" + Product.id) 不兼容。当您看到一个控制器操作的最后一行是像 [product:product] 这样的映射时,这是隐式返回值,因为它是操作闭包的最后一个语句 - 它相当于 return [产品:产品]。但是,如果方法中间有一个映射,它就会被创建并丢弃。它几乎相当于

def ignoreThisMap = [product: product]
// other code

既然您正在进行 Ajax 调用,那么将产品放入模型中就没有意义,因为您不会重新渲染 GSP。您将呈现文本、JSON、XML 或客户端 JavaScript 将用来更新 html 的某些子集的其他内容。因此,您可能想要更接近的东西

if (product) {
   render product as JSON
}
else {
   render "none"
}

,然后您可以使用 jQuery 或 Prototype 来评估 JSON 并提取 JavaScript 中的数据。

[product: product] and render(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 of return [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 to

def ignoreThisMap = [product: product]
// other code

Since 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

if (product) {
   render product as JSON
}
else {
   render "none"
}

and then you can use jQuery or Prototype to evaluate the JSON and extract the data in your JavaScript.

御守 2024-09-23 23:58:21

您可能想要使用为此类工作创建的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文