1

I am trying to pass id (long) from one.xhtml to another .xhtml. Both the backing beans are @ViewScoped and I am trying to share long id between them.

I am getting error with <f:viewParam/>

com.sun.faces.mgbean.ManagedBeanCreationException: Unable to create managed bean saleOrder. The following problems were found: - The scope of the object referenced by expression #{param.foo}, request, is shorter than the referring managed beans (saleOrder) scope of view.

I am have following code.

@ManagedBean    
public class InvoiceView{
    private long number;
    // setter getter.
}

@ManagedBean
@ViewScoped
public SearchInvoice{
    private List<InvoiceView> views;
    private InvoiceView selectedView;  // this is coming from <p:dataTable>
}

@ManagedBean
@ViewScoped
public class SaleOrder {

    @ManagedProperty("#{param.foo}")
    private String number;

    @PostConstruct
    public void init(){
       //sysout number;
    }
}

I have following code in searchInvoice.xhtml file.

    <!-- I have not desclared <f:metadata/> -->
<h:commandButton value="Place Sale Order"
                 action="#{searchInvoice.forwardToSaleOrder}" <!-- this return saleOrder.xhtml string -->
                 rendered="#{not empty searchInvoice.views}">
        <f:viewParam name="foo" value="#{searchInvoice.selectedView.number}" />
</h:commandButton>
Tiny
  • 27,221
  • 105
  • 339
  • 599
Bhushan
  • 567
  • 2
  • 5
  • 14
  • `#{param}` on `@ManagedProperty` requires a request scoped JSF managed bean which is in your case is a view scoped bean. If you still want to stick to a view scoped JSF managed bean with `@ManagedProperty` then, you may take another helper request scoped JSF managed bean and inject this view scoped JSF managed bean `SaleOrder` into that helper request scoped JSF managed bean and set the parameter value to the view scoped bean therein or use ``. See for example, [this](http://stackoverflow.com/a/6137638/1391249) and [this](http://stackoverflow.com/a/4889226/1391249) respectively. – Tiny Dec 26 '14 at 17:46
  • I changed scope of SaleOrder to '@RequestScoped', exception is removed but getting null for number. – Bhushan Dec 27 '14 at 09:33
  • I completely overlooked that you were using `` embedding inside ``. `` is meant for query-string parameters, if you were to happen to pass any. You cannot use it in a way you are showing in the question. In this case, you could better pass that value to the bean using a parameterized action(Listener) method (EL 2.2 or higher) or using ``. Replace ``. (No `@ManagedProperty` is needed - just a getter and a setter for number). – Tiny Dec 27 '14 at 15:48
  • No need to change the bean scope. You could stick at the view scope as it was. – Tiny Dec 27 '14 at 15:57
  • I tried this as well but it did not work. I am doubtful that, does make 'number' available inside @PostConstruct init(). – Bhushan Dec 28 '14 at 08:27
  • You want to set that property (`number`), **only when** you click that button (``). Don't you? If it is that then, why do you even need to access that property in the method decorated by `@PostConstruct`? Why don't you simply move that logic associated with the `number` property to the `action` or `actionListener` method of that ``? Can't you do this? – Tiny Dec 28 '14 at 11:27

1 Answers1

0

You can use <f:viewParam> (JSF 2) in saleOrder.xhtml, which works with view scoped beans. Check out this article.

BlindNW
  • 315
  • 2
  • 10
  • thank for reply. I removed @ManagedProperty and use as described in doc but it return null for 'number'. – Bhushan Dec 26 '14 at 15:39
  • If it is null in your PostConstruct anotated method this is correct. You need to use , it will be setted before call of this method. – BlindNW Dec 26 '14 at 15:49
  • I tried with same as doc but it did not work. Still value is null. – Bhushan Dec 26 '14 at 15:58
  • Is f:metaData placed to the saleOrder.xhtml (not to searchInvoice.xhtml )? – BlindNW Dec 26 '14 at 16:12
  • Is there any logic in searchInvoice.forwardToSaleOrder method? If it just returns "saleOrder.xhtml" string, can you try to replace this command button with h:link to saleOrder view (with nested f:param), and try it with in saleOrder.xhtml? – BlindNW Dec 29 '14 at 11:16