已经为此响应调用了Grails getWriter()

时间:2014-01-22 12:04:25

标签: grails

每当我在template.gsp中调用<ut:profilePicture>时发生错误。这是我的customTagLib

import rms.User

class userTagLib {
    def springSecurityService
    static namespace = "ut"

   def fullName = {
        User currentUser = springSecurityService.currentUser as User
        if (currentUser) {
            out << currentUser.firstName + " " + currentUser.lastName
        }
    }

    def profilePicture ={
        User currentUser = springSecurityService.currentUser as User
        if (currentUser){
                response.outputStream << currentUser.picture
        }
    }
}

我是否需要发布gsp代码?

1 个答案:

答案 0 :(得分:2)

你正在混合HTML和图片。您不能只将二进制图像放在HTML中,HTML应仅包含指向图像的链接。

您可以为个人资料图片制作一个带动作的控制器,例如:

def picture() {
    User user = User.get(params.id)
    response.contentType = ... // image/jpeg i guess?
    response.outputStream << user.picture
    return null
}

并在标记内添加一个链接:

def profilePicture ={
    User currentUser = springSecurityService.currentUser as User
    if (currentUser){
        out << '<img src="'
        out << createLink(controller: 'user', action: 'profile', id: currentUser.id)
        out << '" alt="" />'
    }
}