Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faces flow not working with @Named

I'm just playing around with the simple-flow example from the Java EE7 tutorial. It has the following bean:

import javax.faces.bean.ManagedBean;

@ManagedBean
public class FlowScope {

    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

and the following scope directory structure:

simple-flow
     simple-flow.xhtml
     simple-flow-flow.xhtml ;; this file is empty
     simple-flow-return.xhml 

It works as long as the request scoped bean uses the javax.faces.bean.ManagedBean annotation. But it stops working if I use @Named. Since the javax.faces.bean.ManagedBean annotation may be removed in the future I would like to know how to make it work without using the ManagedBean annotation.

BTW: I'm using JBoss Wildfly as the container.

Regards Roger

like image 312
rogergl Avatar asked Jan 29 '26 19:01

rogergl


1 Answers

If you use @Named, your bean will be managed by CDI. For this to work you need a CDI implementation in your project. If you run your application on a Java EE 6 (or 7 if available) container like TomEE, Glassfish or JBoss CDI is available by default. If you only use a servlet container like Tomcat or Jetty, you have to add a CDI implementation like Apache OpenWebBeans or Weld yourself (or consider using TomEE for example).

To get a CDI request scoped bean you would have to annotate your class like this:

@javax.inject.Named
@javax.enterprise.context.RequestScoped
public class MyBean {
    ...
}

JSF 2.2 also provides flow scoped beans with CDI. A flow scoped bean bound to your flow simple-flow wouldlook like this:

@javax.inject.Named
@javax.faces.flow.FlowScoped(value="simple-flow")
public class MyBean {
    ...
}
like image 94
Michi Avatar answered Jan 31 '26 07:01

Michi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!