Ste*_*TNT 2 jsf bytearray primefaces
我正在尝试构建用户个人资料页面以显示有关我的用户的一些详细信息.
页面的网址是这样的profile.xhtml?username=randomString
.
所以,我要做的是加载randomString用户的所有数据.
这一切都很好,因为它是显示用户图像的时刻.
我使用PrimeFaces与graphicImage的组成部分,但问题是,它会导致一个新的请求,以获得图像,所以请求参数实际损失和getAvatar()
方法接收一个空参数.
一个解决方案可能是使用SessionScoped bean,但它将从第一个请求的用户获取数据,即使randomString将改变它也会显示它们,所以我要求帮助:
如何显示依赖于请求参数的数据库中的动态图像?
谢谢 :)
编辑:BalusC回复后的新代码
JSF页面:
<c:set value="#{request.getParameter('user')}" var="requestedUser"/>
<c:set value="#{(requestedUser==null) ? loginBean.utente : userDataBean.findUtente(request.getParameter('user'))}" var="utente"/>
<c:set value="#{utente.equals(loginBean.utente)}" var="isMyProfile"/>
<pou:graphicImage value="#{userDataBean.avatar}">
<f:param name="username" value="#{utente.username}"/>
</pou:graphicImage>
Run Code Online (Sandbox Code Playgroud)
(我正在使用此变量,因为如果页面请求只是profile.xhtml
没有参数,我希望显示已记录用户的配置文件)
托管Bean:
@ManagedBean
@ApplicationScoped
public class UserDataBean {
@EJB
private UserManagerLocal userManager;
/**
* Creates a new instance of UserDataBean
*/
public UserDataBean() {
}
public Utente findUtente(String username) {
return userManager.getUtente(username);
}
public StreamedContent getAvatar(){
String username = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("username");
System.out.println(username==null);
Utente u = findUtente(username);
return new DefaultStreamedContent(new ByteArrayInputStream(u.getFoto()));
}
}
Run Code Online (Sandbox Code Playgroud)
它出什么问题了?用户名始终为空!
编辑2:添加了对BalusC的回复
是的,因为getAvatar()
方法调用,因为findUser()
我需要找到用户的实体,其用户名作为参数传递(<f:param>
不允许我传递对象!).
所以,findUser()
因为我使用抛出异常entityManager.find()
与null
主键!
顺便说一句,我绝对相信,无论#{utente}
和#{utente.username}
因为包含图像的面板只有当呈现不为空#{utente ne null}
并且username
是其首要关键!
所以我无法真正检查HTML输出!
我担心因为获取图像需要新的http请求#{utente}
而丢失getAvatar()
把它传递给<f:param>
.它将在渲染响应期间添加.
<p:graphicImage value="#{images.image}">
<f:param name="id" value="#{someBean.imageId}" />
</p:graphicImage>
Run Code Online (Sandbox Code Playgroud)
该#{images}
助手bean可以仅仅是这样的:
@ManagedBean
@ApplicationScoped
public class Images {
@EJB
private ImageService service;
public StreamedContent getImage() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getRenderResponse()) {
// So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
return new DefaultStreamedContent();
}
else {
// So, browser is requesting the image. Get ID value from actual request param.
String id = context.getExternalContext().getRequestParameterMap().get("id");
Image image = service.find(Long.valueOf(id));
return new DefaultStreamedContent(new ByteArrayInputStream(image.getBytes()));
}
}
}
Run Code Online (Sandbox Code Playgroud)
由于上面的帮助bean没有基于请求的状态,因此可以安全地应用作用域.
归档时间: |
|
查看次数: |
9381 次 |
最近记录: |