【发布时间】:2020-04-23 19:18:55
【问题描述】:
在我的顶部栏中,我有一个 <o:graphicImage> 来显示来自我的用户的图片。
<o:graphicImage dataURI="true" height="32" width="32" styleClass="img-circle"
value="#{employeeProfileMenuPictureRequestController.getPicture_32_32(loginBean.currentEmployee)}"
lastModified="#{employeeProfileMenuPictureRequestController.lastUpdate}" />
我的后端 bean 如下:
@GraphicImageBean
public class EmployeeProfileMenuPictureRequestController implements Serializable {
private Date lastUpdate = new Date();
public byte[] getPicture_32_32(Employee employee) throws StorageAttachmentNotFoundException, IOException {
try {
String path = employeeProfilePictureService.findProfileImageByEmployee(employee, FileSizeType.SIZE_32_32.toString());
if (employee == null || path == null || path.isEmpty()) {
return Utils.toByteArray(Faces.getResourceAsStream("/resources/images/no-photo-icon.png"));
}
Path fileLocation = Paths.get(path);
byte[] data = Files.readAllBytes(fileLocation);
LOGGER.info("END getPicture_32_32");
return data;
catch (Exception e) {
LOGGER.error(ExceptionUtils.getFullStackTrace(e));
}
return Utils.toByteArray(Faces.getResourceAsStream("/resources/images/no-photo-icon.png"));
}
public Date getLastUpdate() {
return lastUpdate;
}
}
不幸的是,每个页面请求/页面导航都会调用getPicture_32_32(Employee)。这意味着它也是每次对数据库的请求,这需要时间。
我已经尝试将lastModified 添加到<o:graphicImage>,但每次页面请求时也会调用该函数。
谁能帮我解决这个问题?
【问题讨论】:
-
如果 lastUpdate 始终是
new Date(l则说明它没有被缓存的资源 -
但我使用的是@GraphicImageBean?这类似于 SessionScoped?那我该如何解决呢?
-
为什么要使用dataURI?你能用纯html缓存那些吗?不要以为你可以。它们嵌入在页面中...... Snafu......
-
试试 PrimeFaces
p:cache -
如果我将 dataURI 切换为“false”,那么我得到:请求 /javax.faces.resource/EmployeeProfileMenuPictureRequestController_getPicture_32_32.jsf: javax.servlet.ServletException: 参数类型不匹配
标签: jsf caching omnifaces graphicimage