Setting view scope on JSF 2.2

1k views Asked by At

On JSF 2.2 we don't have the option to set the View Scope on faces-config.xml .

So how should it be done? Is the view scope missing on JSF 2.2 ?

Thank you!

1

There are 1 answers

0
09Q71AO534 On

Use @ViewScoped annotation on managed bean:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class AViewScopedBean {
    //managed bean contents...
}

If you don't like the annotations configuration (really odd), you can just set the view scope on faces-config.xml

<managed-bean>
    <managed-bean-name>aViewScopedBean<managed-bean-name>
    <managed-bean-class>some.package.AViewScopedBean</managed-bean-class>
    <managed-bean-scope>view</managed-bean-scope>
</managed-bean>

Note that this only works on JSF 2. Check that your faces-config file is configured to handle JSF 2.x version:

<!-- relevant part of faces-config.xml file for this Q/A -->
<faces-config ... version="2.1">

Note: Warning make sure its Serializable

The error message is pretty straightforward:

java.io.NotSerializableException: com.bean.StatusBean2

This means that your com.bean.StatusBean2 must also implement the Serializable interface. From java.io.Serializable documentation:

When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object. (this is the error you're getting)

You can learn more about Java Serialization here: Java Serialization

From your question: is it necessary to implement serializable?, BalusC already posted a good answer/explanation: JSF backing bean should be serializable? Thanks for @Luiggi Mendoza