0

My navigation case is as follows. I have a landing page, from which I can navigate to a store page. This store page can work with two different catalogues of products. This is controlled by a given Retailer object.

From the store page on, there are multiple beans and pages that will rely on the Retailer in session. The event that switches the Retailer is a new selection from the landing page. I could control the Retailer in session just by firing an action in the selection and then navigating to the store page. But this must be controlled at an action in a bean.

My problem is the case of a direct navigation from navigation bar to the store page. So I need some mechanism that initializes a given Retailer in the store page. And this must be performed at every bean that this page depends on. And this should be done via a GET param. I can have this behavior with a complex decision at preRenderView listener, but I don’t feel this is the more elegant way.

The underlying problem, thus, is how to intercept a direct navigation to a page whose data is managed by several beans (no matter the scope of them), and introduce something in the session, that will be used by these beans in their actions.

I have tried this (in SessionScoped beans):

1) If I use @PostConstruct + @ManagedProperty(with param) I receive a error while instatiating the bean because the bean is SessionScoped

2) If I use viewParam + preRenderView event, the param is injected before the event and I obtain my desired behavior, BUT this occurs every time the page is loaded.

I want this initialization takes place once and only once, and I want to use GET params.

How can I do it?

Carlos
  • 41
  • 5
  • 1
    This is definitely not an usual case given that you're doing this on a session scoped bean instead of a request or view scoped one. When you said, "this occurs every time the page is loaded", don't you actually mean postbacks (i.e. on every (ajax)form submit). If so, why is the bean not view scoped? What exactly is the concrete functional requirement? – BalusC Jan 04 '13 at 18:25
  • You are right. I'd like to convert it as ViewScoped, but I'm using Primefaces, and dynamic graphic image loading doesn't work with ViewScoped beans. – Carlos Jan 04 '13 at 18:59
  • 2
    You might want to ask a question about that instead, so that you don't need to introduce another workarounds for an existing workaround because you made a workaround instead of a solution. – BalusC Jan 04 '13 at 19:00
  • This is nornmal behaviour in PF. So I simply assume that I have to use SessionScoped. I though it might be an elegant way to implement the case in JSF, not a workaround. If you say this is indeed an unusual case, I'll try to do it another way – Carlos Jan 04 '13 at 19:04
  • Do you have any authoritative references for this "normal behavior"? It would have been a huge bug or just a misinterpretation on your side. It's still unclear what problem exactly you're talking about, but if I *guess* it right, this answer may already explain and answer it: http://stackoverflow.com/questions/10073905/problems-displaying-database-blob-image-in-primefaces-graphicimage/10161878#10161878 and http://stackoverflow.com/questions/8207325/display-image-from-database-with-pgraphicimage/12452144#12452144 – BalusC Jan 04 '13 at 19:27
  • Hi BalusC. It is stated in PF's user manual that graphicImage doesn't work in a ViewScoped. It is assumed then that it will work with other scopes. With SessionScope it works, with RequestScope doesn't (at least, I couldn't make it work). Anyway, to maintain this question On Topic, I have reoriented it and updated the original question. – Carlos Jan 05 '13 at 10:53
  • So you're talking about `` with `StreamedContent`? Have you read the given links? You can perfectly store the identifiers of the dynamic image in a view scoped bean, but the streaming should be done in a separate application or request scoped bean. – BalusC Jan 05 '13 at 11:26
  • ok. Apart from PF's graphicImage issues, I'd like to know if it exists a more elegant way of doing the case I describe. That's why I updated the question – Carlos Jan 05 '13 at 15:48
  • That's already answered. On "direct navigation" (basically, a GET request) the `isPostback()` will return `false`. The `` "issue" is easily solvable. Have you really carefully read and understood the given links? – BalusC Jan 05 '13 at 15:51

1 Answers1

1

One of the ways is to just perform the job only if the f:viewParam value is still null.

public void init() {
    if (value == null) {
        // ...
    }
}

An alternative is to perform the job only on non-postbacks:

public void init() {
    if (!FacesContext.getCurrentInstance().isPostback()) {
        // ...
    }
}

Note that this would still be executed when a new GET request is fired within the same session and that you'd normally like to use a @ViewScoped bean for this to prevent multiple browser tabs/windows interfering with each other.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555