Display of a dynamic image, which depends on the request parameter

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;

    /**
     * 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 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-

+2
1

<f:param>. .

<p:graphicImage value="#{images.image}">
    <f:param name="id" value="#{someBean.imageId}" />
</p:graphicImage>

#{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()));
        }
    }

}

bean , , .

+7

All Articles