显示依赖于请求参数的动态图像 [英] Showing a dynamic image that depends on a Request Parameter
问题描述
我正在尝试建立一个用户个人资料页面,以显示有关我的用户的一些详细信息.
I'm trying to build a user profile page to show some details about my users.
页面的网址类似于profile.xhtml?username=randomString
.
因此,我要做的是加载randomString用户的所有数据.
So, what I've to do is loading all the data of randomString's user.
一切正常,因为是时候显示用户的图像了.
It goes everything fine since it's the moment to show user's image.
我正在将PrimeFaces与graphicImage组件一起使用,但是问题是,这会导致NEW请求获取图像,因此请求参数实际上丢失了,并且getAvatar()
方法接收到空参数.
I'm using PrimeFaces with graphicImage component, but the problem is that it causes a NEW request to get the image, so the request parameter is actually lost and the getAvatar()
method receives a null parameter.
一个解决方案可能是将bean设为SessionScoped,但是它将从第一个请求的用户那里获取数据,即使randomString会更改,它也会向他们显示数据,所以我正在寻求帮助:
One solution may be making the bean SessionScoped, but it will take data from the first requested user and it will show them even if randomString will change, so I'm asking for help :
如何显示依赖请求参数的数据库中的动态图像?
How can I show a dynamic image from database that depends on a request parameter?
谢谢:)
BalusC回复后的新代码
EDIT : New code following BalusC's reply
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>
(我正在使用此var,因为如果页面请求仅profile.xhtml
不带参数,我希望显示登录用户的个人资料)
(I'm using this vars because I want the logged user's profile to be shown if page request il just profile.xhtml
without parameters)
受管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()));
}
}
这有什么问题? 用户名始终为空!
What's wrong with it? username is always null!
已添加对BalusC的回复
EDIT 2 : Added reply to BalusC
是的,因为getAvatar()
方法调用findUser()
,因为我需要找到以用户名作为参数传递的用户实体(<f:param>
不允许我传递对象!).
Yeah, because the getAvatar()
method calls findUser()
as I need to find the user's entity with the username passed as parameter (<f:param>
won't allow me to pass an object!).
所以findUser()
会引发异常,因为我正在将entityManager.find()
与null
主键一起使用!
So findUser()
throws an exception because I'm using entityManager.find()
with a null
primary key!
顺便说一句,我绝对确定#{utente}
和#{utente.username}
都不为空,因为仅当#{utente ne null}
和username
是其主键时,才会渲染包含图像的面板!
Btw, I'm absolutely sure that both #{utente}
and #{utente.username}
are not null because the panel that contains the image is rendered only if #{utente ne null}
and username
is its primary key!
所以我不能真正检查HTML输出!
So I can't really check the HTML output!
恐怕当我呼叫getAvatar()
时#{utente}
丢失了,因为获取图像需要新的http请求
I'm afraid that #{utente}
is lost when I call getAvatar()
as getting an Image requires a new http request
推荐答案
将其作为<f:param>
传递.它将在渲染响应期间添加.
Pass it as <f:param>
. It will be added during render response.
<p:graphicImage value="#{images.image}">
<f:param name="id" value="#{someBean.imageId}" />
</p:graphicImage>
#{images}
帮助器bean看起来像这样:
The #{images}
helper bean can just look like this:
@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()));
}
}
}
由于上述辅助bean没有基于请求的状态,因此可以安全地在应用程序范围内使用.
As the above helper bean has no request based state, it can safely be application scoped.
这篇关于显示依赖于请求参数的动态图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!