I am trying to create a user profile page to show some information about my users.
The page URL is similar to profile.xhtml?username=randomString.
So, I need to load all user data randomString.
Everything is going fine, as this is the moment to show the user's image.
I use PrimeFaces with the graphicImage component, but the problem is that it calls a NEW request to get the image, so the request parameter is actually lost, and the method getAvatar()gets a null parameter.
One solution might be to create a SessionScoped bean, but it will receive data from the first requested user and it will display it even if randomString changes, so I ask for help:
How can I show a dynamic image from a database that depends on a query parameter?
Thank:)
EDIT: new code following BalusC answer
JSF Page:
<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>
(I use these vars because I want the registered user profile to be displayed if the request for the il page is simply profile.xhtmlwithout parameters)
Managed bean:
@ManagedBean
@ApplicationScoped
public class UserDataBean {
@EJB
private UserManagerLocal userManager;
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 about him? username is always null!
EDIT 2: Added answer to BalusC
Yes, because the method getAvatar()calls findUser(), because I need to find a user object with the user name passed as a parameter ( <f:param>it will not allow me to pass the object!).
So it findUser()throws an exception, because I use entityManager.find()with the nullmain key!
Btw, , #{utente} #{utente.username} , , , , #{utente ne null} username !
HTML!
, #{utente} , getAvatar(), HTTP-