ViewScoped does not work

0

I have a Bean that has @SessionScoped and works perfectly, however I want to use @ViewScoped, but when I change the annotation it stops working and it always marks me the Target Unreachable error

@ViewScoped I'm importing it with:

import javax.faces.bean.ViewScoped;

I'm not sure what is wrong.

What I want is for the variables that I use inside that Bean to die when I change my view.

This is the code of my Bean:

package mx.materiam.backend.controllers;
import java.io.Serializable;

import javax.faces.bean.ViewScoped;
import javax.inject.Named;
import mx.materiam.entities.OrdenProduccion;


@Named(value = "ordenProduccionController")
@ViewScoped
public class ordenProduccionController implements Serializable{

private OrdenProduccion ordenProduccion;


public ordenProduccionController() {
    ordenProduccion = new OrdenProduccion();
}



public void foo(){
    System.out.println("hola");
}

public OrdenProduccion getOrdenProduccion() {
    return ordenProduccion;
}

public void setOrdenProduccion(OrdenProduccion ordenProduccion) {
    this.ordenProduccion = ordenProduccion;
}

}
    
asked by gibran alexis moreno zuñiga 16.02.2017 в 18:45
source

2 answers

1

The problem is that you are using JSF and CDI, denoted by the use of @Named (CDI). With JSF 2.0 and 2.1, you could not have a managed bean managed by CDI that is @ViewScoped since it could not support it. From JSF 2.2 onwards, there is a new entry of @ViewScoped within the javax.faces.view package. Your code should look like this:

package mx.materiam.backend.controllers;
import java.io.Serializable;

//Aquí cambias el nombre del paquete
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import mx.materiam.entities.OrdenProduccion;

@Named(value = "ordenProduccionController")
@ViewScoped
public class OrdenProduccionController implements Serializable {
    /* resto de tu código */
}

If you use JSF 2.0 or 2.1, simply you can not do it .

    
answered by 16.02.2017 / 18:56
source
1

One trick to running @ViewScoped with CDI in JSF 2.0 and 2.1, and Java EE 6 is to use Apache Deltaspike. link

Deltaspike automatically recognizes the ViewScoped and other scopes of the javax.faces.bean packages ... giving it its respective scope in CDI.

To make your @ViewScoped work, simply add the core and jsf libraries of DeltaSpike to your War (be it with ant, maven, gradle, etc ...), and that's it.

You will see that a variale dswid is added in the URLs but it is normal.

    
answered by 16.02.2017 в 21:14