0

i had some problem showing images retrieved by my db.

View caller:

<p:graphicImage value="#{appController.image}" height="200 px" >
      <f:param name="oid" value="#{item.oid}" />
</p:graphicImage>

Controller:

@Named("appController")
@ApplicationScoped
public class AppController {

    @Inject
    private MultimediaFacade multimediaFacade;

    public StreamedContent getImage() throws IOException {
        System.out.println("getting image")
        FacesContext context = FacesContext.getCurrentInstance();
        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            // So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL.
            return new DefaultStreamedContent();
        } else {
            // So, browser is requesting the image. Return a real StreamedContent with the image bytes.
            String imageId = context.getExternalContext().getRequestParameterMap().get("oid");
            int oid=Integer.parseInt(imageId);
            System.out.println(oid);
            Multimedia image = multimediaFacade.find(oid);
            System.out.println(Arrays.toString(image.getFileBlob()));
            return new DefaultStreamedContent(new ByteArrayInputStream(image.getFileBlob()));
        }
    }
}

this code shows nothing and it looks like the method is never called (never print in console)!

after days of trial changing the scope, i tried to use @ManagedBean instead of @Named, and it works!!!

can someone explain me why this work only with @ManagedBean and not with @Named?

Marco
  • 169
  • 6
  • 19
  • 1
    `javax.enterprise.context.ApplicationScoped` for CDI, `javax.faces.bean.ApplicationScoped` for JSF. – Geinmachi May 19 '16 at 20:09
  • it isn't a scope issue – Marco May 20 '16 at 09:55
  • You mean to say, CDI beans work otherwise fine in "regular" pages? – BalusC May 20 '16 at 16:05
  • @BalusC what do u mean with "regular"? I have a ViewScoped CDI bean that works fine. Of course i can't use graphicimage with a ViewScoped bean, so i had to create an ApplicationScoped (or SessionScoped) bean... but it works only with ManagedBean – Marco May 20 '16 at 16:30
  • Better use Omnifaces' o:graphicImage, if you're able to. – jpangamarca May 21 '16 at 00:48
  • @Marco It might be a scope issue. By default, CDI uses only beans with bean defining annotations, and scope annotation is among them. Look at my answer. Also try `@RequestScoped` and if works, then it was an error in your imports. – OndroMih May 23 '16 at 09:23

1 Answers1

1

Check that you have javax.enterprise.context.ApplicationScoped in imports.

If you have a different import for @ApplicationScoped (e.g. javax.faces.bean.ApplicationScoped), then you need to configure CDI to discover all beans instead of only those with CDI annotations (which is the default)

To tun discovery for all beans, either add empty beans.xml into WEB-INF directory, or if you already have beans.xml there, add bean-discovery-mode="all" into the <beans> element, like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="annotated">
</beans>
OndroMih
  • 7,280
  • 1
  • 26
  • 44
  • you are completely right! So the problem was about CDI implementation (enterprise.contexr vs faces.beans) can you link me somewhere i can study better about CDI? thank you so much – Marco May 23 '16 at 16:32
  • For some annotations, you really need to be careful about package names. There are annotation doubles in javax packages, that have the same name but different package, and therefore different meaning. E.g. [@Singleton](http://docs.oracle.com/javaee/7/api/javax/ejb/Singleton.html) and [@Singleton](http://docs.oracle.com/javaee/7/api/javax/inject/Singleton.html). They come from different specifications, therefore they cannot be mixed. – OndroMih May 24 '16 at 09:50
  • You cn find more about CDI at this page: http://cdi-spec.org/, and specifically about the bean discovery here: http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#type_discovery_steps – OndroMih May 24 '16 at 09:50