图像控制器有一个功能
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=""/>