0

I'm starting to work with PrimeFaces 5.2, my IDE is Eclipse Luna 4.4.2, Java 1.7 and JSF 2.1.

I have a view with a DataTable, there's a column called holding two CommandButtons, one for editing and another one for deleting. When the edit button is pressed I show a modal Dialog filled with an external xhtml file which also has its own ManagedBean.

I want to pass the selected object from the DataTable to the external xhtml's ManagedBean so its fields are filled with the information from it.

My Excepciones.xhtml looks like this:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    template="/WEB-INF/facelets/templates/plantillaPrincipal.xhtml">
    <ui:define name="mainBody">
        <h:form id="form">
            <p:dataTable var="excepcion" value="#{excepcionesView.lazyModel}"
                paginator="true" rows="10" paginatorPosition="bottom"
                paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                rowsPerPageTemplate="5,10,15" selectionMode="single"
                selection="#{excepcionesView.selectedExcepcion}" id="excepcionTable"
                lazy="true">
                <p:ajax event="rowSelect" listener="#{excepcionesView.onRowSelect}"
                    update=":form:excepcionDetail"
                    oncomplete="PF('excepcionDialog').show()" />
                <p:column headerText="Id" sortBy="#{excepcion.id}">
                    <h:outputText value="#{excepcion.id}" />
                </p:column>
                ....
                <p:column headerText="Acciones">
                    <p:commandButton icon="ui-icon-pencil" action="#{excepcionesView.viewExcepcionEditar}" >
                        <f:setPropertyActionListener value="#{excepcion}"
                                        target="#{excepcionesView.selectedExcepcion}" />
                    </p:commandButton>
                    ...
                </p:column>
            </p:dataTable>
            ...
        </h:form>
    </ui:define>
</ui:composition>

It's ManagedBean:

@ManagedBean(name="excepcionesView")
@ViewScoped
public class ExcepcionesView  implements Serializable{

    private LazyDataModel<Excepcion> lazyModel;

    private Excepcion selectedExcepcion;

    @ManagedProperty("#{excepcionService}")
    private ExcepcionService service;

    @PostConstruct
    public void init() {
        lazyModel = new ExcepcionDataModel(service.createExcepciones(200));
    }

    public void viewExcepcionEditar() {     
        FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("selectedExcepcion", selectedExcepcion);

        Map<String,Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("draggable", false);
        options.put("resizable", false);
        options.put("contentHeight", 320);

        RequestContext.getCurrentInstance().openDialog("ExcepcionEdicion", options, null);
    } 
     ...
}

As you can see, I call the method viewExcepcionEditar to store the selected row and display a dialog that will load my ExcepcionEdicion.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:p="http://primefaces.org/ui"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">
<h:head>
        <h:outputStylesheet library="theme" name="css/style.css" />
</h:head>
<h:body>
    <h:form id="productoEditarForm">
            <p:panelGrid id="productoEditarGrid" styleClass="tablaSinBorde" columns="2" >
                <p:outputLabel for="nombreTxt"  value="Nombre: "/>
                <h:inputText id="nombreTxt" value="#{editarExcepcionView.excepcion.nombre}" />
                <p:outputLabel for="ldcTxt" value="Línea de Crédito: "/>
                <h:inputText id="ldcTxt" value="#{editarExcepcionView.excepcion.lineaCredito}"/>
            </p:panelGrid>

            <p:commandButton id="aceptarBtn"    value="Aceptar" />
            <p:commandButton id="cancelarBtn"   value="Cancelar" />
    </h:form>
</h:body>
</html>

and it's bean contains:

@ManagedBean(name="editarExcepcionView")
@ViewScoped
public class EditarExcepcionView implements Serializable{
    private String excepcionId;
    private Excepcion excepcion;

    public EditarExcepcionView(){

        System.out.println("hey");
        excepcion = (Excepcion) FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("selectedExcepcion");
        System.out.println("excepcion " + excepcion.getNombre());
    }
    ....
}

In its constructor I try to read the parameter I placed but it always comes as null.

How can I pass this object between these beans so the information from said object can be read and displayed on my modal Dialog?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Uriel Arvizu
  • 1,876
  • 6
  • 37
  • 97
  • http://incepttechnologies.blogspot.ca/p/view-parameters-in-jsf-20.html – DavidS May 13 '15 at 23:17
  • 1
    @DavidS if the information on that link answers the question, please write an answer including enough detail in the answer text itself. Just providing a link is not so useful in the longer term, as links may change or become invalid. – Jeen Broekstra May 13 '15 at 23:53
  • It does not answer the question, @JeenBroekstra. I just provided it because it may help Uriel. – DavidS May 13 '15 at 23:59
  • Ah, no worries then. – Jeen Broekstra May 14 '15 at 00:00
  • Thanks for the link, but it doesn't really offer much to my problem, if you check my code you can tell I'm already using the method expression to pass the object but even though I'm setting the object in the request map in one bean, when I try to recover it from the other bean it is always null. My problem lies in the object being passed as null – Uriel Arvizu May 14 '15 at 00:18
  • I think the injected managed-bean "editarExcepcionView" should have a broader scope (e.g `@SessionScoped` ) than the container one. – Omar May 14 '15 at 01:50
  • 2
    There's too much going on here. Try to trim the code down to the minimum necessary to reproduce the problem. At first glance however, it looks like you should just use the Flash Scope. See [this](http://stackoverflow.com/questions/26121421/which-scope-to-use-for-a-series-of-pages-each-dependent-on-the-previous-page/26121647#26121647) for a brief intro – kolossus May 14 '15 at 02:14
  • @Omar, using `@SessionScoped` for view matters is plainly wrong. He should use either view parameters: http://stackoverflow.com/questions/15435434/view-parameter-when-navigating-to-another-page or the flash scope: http://stackoverflow.com/questions/11194112/understand-flash-scope-in-jsf2 – Aritz May 14 '15 at 07:27
  • Or use the cdi ([deltaspike](http://deltaspike.apache.org/documentation/jsf.html)) `@Viewaccessscoped`. Easier to use imo than to pass requestparameters forth an back – Kukeltje May 14 '15 at 10:27
  • @XtremeBiker I tried using passing the parameters using the Flash scope, but when I get it on my other bean, the returned object is null too. – Uriel Arvizu May 14 '15 at 15:52
  • @kolossus trimmed my code, check my edited question – Uriel Arvizu May 14 '15 at 15:57
  • Can you post your attempt with the flash scope @UrielArvizu ? – kolossus May 19 '15 at 14:27

0 Answers0