0
votes

I see that primeface p:graphicImage in my form is causing a new windowId to be created with in the same browser tab.

All my beans are window scoped. @WindowScoped of Myface CODI.

Since my business logic is tied to the initial login, the environment variables are tied to the windowContext.

When p:graphicImage tries to access the bean with a new windowId/windowContext, the application is failing.

Is there a way to avoid this?

Here is my view file

        <h:form enctype="multipart/form-data">
            <p:messages />
            <p:panelGrid style="width:75%;margin:5px auto;">
                <f:facet name="header">
                    <p:row>
                        <p:column colspan="3">
                            <h:outputText value="#{title.update}" />
                        </p:column>
                    </p:row>
                </f:facet>
                <p:row>
                    <p:column>
                        <p:graphicImage alt="Your Photograph" id="photo" value="#{employeeUpdateBean.photoStream}" width="80" height="80" />
                    </p:column>
                    <p:column colspan="2">
                        <p:fileUpload mode="advanced"
                            fileUploadListener="#{employeeUpdateBean.addPhotoFile}"
                            allowTypes="/(\.|\/)(gif|jpe?g)$/" sizeLimit="100000"
                            invalidSizeMessage="Please limit photo size to 100Kb"
                            />
                    </p:column>
                </p:row>
                    <f:facet name="footer">
                    <p:row>
                        <p:column colspan="3">
                            <p:commandButton value="Update" action="#{employeeUpdateBean.employeeUpdate}" ajax="false"
                                style="margin-left:5px;margin-right:5px;" />
                        </p:column>
                    </p:row>
                </f:facet>
            </p:panelGrid>
    </h:form>

Here is part of my bean

@Named
@WindowScoped
public class EmployeeUpdateBean implements Serializable{

static final long serialVersionUID = 7l;
private static final Log log = LogFactory.getLog(EmployeeUpdateBean.class);

@Inject
private HibernateUtil hibernateUtil;

@Inject
securityHelper secHelper;

@Inject
WindowContext windowContext;

public EmployeeUpdateBean() {
}

@PostConstruct
public void getData() {
    System.out.println("In Get Data");
    System.out.println("windowId: "+this.windowContext.getId());
    //Thread.dumpStack();
    Integer id= secHelper.getEmployeeId();
    System.out.println("ID"+id);
}

Below is the output I see.

In Get Data
windowId: f75
ID11117
In Get Data
windowId: f75
ID11117
In Get Data
windowId: 1f9
IDnull

This first two 'in Get Data' are related to a preRenderView call to getData (not shown in code here) and the PostContruct call.

The third is clearly from the call to @PostConstruct when a new bean instance is created when p:graphicImage tries to access the photoStream with a new windowId.

I have tried this by removing the p:graphicImage from the form, and in that case the third call is never made.

I have to use p:graphicImage becasue I need to stream data from the DB. And I am trying to avoid writing a seperate servlet to stream images.

Also, the seperate servlet is not a good option as I need to know the employee who is logged in to be able to show the image.

Please let me know if there are any good solutions to get over this.

My full environment is Tomcat 7 Openwebbeans Myfaces CODI Myfaces + Primefaces

1

1 Answers

1
votes

there are many questions here in stackoverflow regarding the use of p:graphicImage to render images from database.. In order to make it work, the bean has to be @RequestScoped or @SessionScoped. I don't think it will work with special scoped from CODI.

I suggest you use a servlet for this matter. if you need to know the employee who is logged in, you can put that information in the session map, and then read it from server.

The following explanation is from primefaces user's guide:

Dynamic image display works as follows:
• Dynamic image puts its value expression string to the http session with a unique key.
• Unique session key is appended to the image url that points to JSF resource handler.
• Custom PrimeFaces ResourceHandler get the key from the url, retrieves the expression string like #{bean.streamedContentValue}, evaluates it to get the instance of StreamedContent from bean and streams contents to client.

As a result there will be 2 requests to display an image, first browser will make a request to load the page and then another one to the dynamic image url that points to JSF resource handler. Please note that you cannot use viewscope beans as viewscoped bean is not available in resource loading request.