【问题标题】:Run/Execute a query in gsp/view in Grails在 Grails 的 gsp/view 中运行/执行查询
【发布时间】:2013-05-28 03:46:39
【问题描述】:

图像控制器有一个功能

def scaled = {
    log.debug("SCALED IMAGE " + params)
    response.setContentType('image/jpeg')
    response.setHeader('Cache-control', 'no-cache')
    def userId = session.selectedUser.id
    if(params.userId){
      userId = params.userId
    }
    if(params?.id != null && !(params?.id.empty)){
      params.maxWidth?: 20.00
      params.maxHeight?: 20.00
      response.outputStream << imageProxyService.loadImage(params.id, securityService.passwordEncoder(userId.toString()),params.maxWidth, params.maxHeight)
    }
  }

和头像的文件名存储在用户表中。 用户有很多状态,我想根据显示的状态加载用户的个人资料照片。我的状态 gsp 如下所示:

<g:each in="${statusList}" status="i" var="status" status="i">
          <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
            <tr><td><img id="profile_photo" src="${createLink(controller:'image', action:'profilePhoto', id:status.photo)}" alt="" title="" />
              </td></tr>
          </tr>
        </g:each>

我在状态控制器中使用的查询:

def alllist = {
        log.debug("BEGIN IWI PROFILE")
        def statusList = []


            def sql = new Sql(dataSource);

            def statusQuery = """SELECT c.id, c.status_message, c.status_type as profile, se.id, se.photo FROM user_account se, status c
                    WHERE
                    c.user_account_id = se.id
                    GROUP BY se.id, c.id, c.status_message, c.status_type, se.photo"""


            def result = sql.rows(statusQuery);

            def userQuery = """not sure """
            if (result){
                log.debug("GOT SOME RESULTS IN PERSONAL user" + result)
                result.each() { status ->
                    def userResult = sql.firstRow(userQuery, [status.id])
                    if (userResult){
                        status['userId'] = userResult.id
                    } else {
                        status['userId'] = ""
                    }
                    statusList += status
                }
            }


        render(template: "alllist", model: [statusList: statusList])

请注意:这样我可以得到所有照片,但照片仅显示给会话用户。

<img id="profile_photo" src="/myApp/image/profilePhoto/9edd692580d148c791c6c2aa3510605a95ba6de6.jpg" alt="" title=""/>

【问题讨论】:

  • 您可以发布您的域对象吗?
  • 请查看更新后的问题

标签: grails


【解决方案1】:

我会创建一个 TagLib。您实际呈现照片的方式取决于 UserAccount.photo 字段中存储的内容。我假设它是图像的绝对 URL。

class UserTagLib {

    def photo = {attrs ->
        UserAccount u = attrs.user
        out << "<img src=${u.photo}/>"
    }

}

您可以在 GSP 中使用它,例如:

<user:photo user="${status.userAccount}"/>

..假设您在一个名为 status 的变量中循环访问状态对象列表

【讨论】:

  • 非常有帮助...如果我只是将 filename.jpg 存储到照片中,那我该怎么做呢?还有什么方法可以动态使用图像标签。你的是: 我可以使用 它会接受吗?
  • 这是我找到的一个例子:gamerzfever.wordpress.com/2012/10/02/…
【解决方案2】:

你可以用这个:

<img src="http://applicationUrl/images/${u.photo}" />

<img src="${createLink(dir:'images',file: u.photo}" />

它们可以使用,但现在不推荐使用创建链接,我建议使用:

相同的资源

<img src="${resource(dir: 'images', file: 'file.jpg')}"/>

更多信息请访问:

http://grails.org/doc/2.2.1/ref/Tags/resource.html

关于资源

【讨论】:

  • 谢谢,这是我一直在寻找的 syntex... 更多学习... 如何在局部变量中设置图像 var 并在每个标签中以这种方式使用它?
  • devdevdev.wordpress.com/2009/01/13/… 勾选这个我认为它应该可以帮助您从数据库中在 GSP 上渲染图像
  • 这很有帮助...我要在一个操作中运行 2 个查询,因此我可以通过第一个查询和用户信息以及第二个查询的照片来获取状态信息,具体取决于状态...我需要吗?在每个循环中使用第二个查询...请帮助
  • 您可以创建地图并存储来自查询的信息,最后渲染地图,或者如果您可以在您的操作中发布一些代码,那么我可以提供更多帮助。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-28
  • 2015-07-05
  • 2015-07-05
  • 1970-01-01
  • 2018-07-10
  • 2021-03-17
  • 2021-03-09
相关资源
最近更新 更多