grails - 在Grails的gsp/view中运行/执行查询

标签 grails

图像 Controller 具有功能

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>

我在状态 Controller 中使用的查询:
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])

请注意:这样我就可以正确处理所有照片,但照片仅为 session 用户显示。
<img id="profile_photo" src="/myApp/image/profilePhoto/9edd692580d148c791c6c2aa3510605a95ba6de6.jpg" alt="" title=""/>

最佳答案

我将创建一个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的变量中遍历Status对象列表

关于grails - 在Grails的gsp/view中运行/执行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16783555/

相关文章:

debugging - GGTS 在调试期间不会进入常规代码行

forms - Grails-重复的表单提交处理-withForm {}如何工作?

grails - r:img导致资源未定位

validation - Grails 3:以编程方式调用约束验证方法

maven - 如何将第三方 jar 添加到 Maven 中心

grails - Grails:简单的hasMany关系创建了不必要的表

session - 集成测试期间未设置Grails session 对象属性

grails - Grails中验证和刷新的顺序是否保存方法问题?

database - grails的数据库中存在用户名的逻辑

javascript - Grails 中具有一个域类的依赖下拉菜单