1

I am developing a project using JSF. In an opening popup window, i want to show some details about a product but can not get view scoped bean' s value on a datatable.

Can you help me?

Thanks.

Here is my UrunuDenetlemeSayfasi.xhtml code snippet:

<h:commandLink onclick="window.open('UruneGozAt.xhtml',
'Ürün İçeriği', config='width=700, height=400, top=100, left=100,
scrollbars=no, resizable=no');"
action="#{uruneGozAtBean.urunIdsineGoreUrunIcerigiGetir}" value="Ürün İçeriğine Göz At">
<f:param name="urunid" value="#{urun.urunID}" /> 
</h:commandLink>

Here is UrunuGozAt.xhtml:

<!DOCTYPE html>
<html 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">
<h:body>
    <h:dataTable class="table table-striped"
        value="#{uruneGozAtBean.urunIcerik}" var="urun">
        <h:column>
            <f:facet name="header">
                <h:outputText value="barkod no" />
            </f:facet>
            <h:outputText value="#{urun.barkodNo}" />
        </h:column>
    </h:dataTable>
</h:body>
</html>

Here is UruneGozAtBean.java

    UrunDenetlemeSayfasiBean urunDenetle = new UrunDenetlemeSayfasiBean();
    UrunDenetleService urunService = new UrunDenetleService();
    private UrunIcerik urunIcerik = new UrunIcerik();
    private Long urunIdParametre;

    public UrunIcerik getUrunIcerik() {
    return urunIcerik;
    }
    public void setUrunIcerik(UrunIcerik urunIcerik) {
    this.urunIcerik = urunIcerik;
    }
    public Long getUrunIdParametre() {
        return urunIdParametre;
    }
    public void setUrunIdParametre(Long urunIdParametre) {
        this.urunIdParametre = urunIdParametre;
    }
    public void urunIdsineGoreUrunIcerigiGetir() {
        setUrunIcerik(urunService.urunIdsineGoreUrunIcerigiGetir(urunIdEldeEt()));
    }
    public Long urunIdEldeEt(){      
        FacesContext fc = FacesContext.getCurrentInstance();
        setUrunIdParametre(getUrunIdParametre(fc));
        return getUrunIdParametre();
    }
    public Long getUrunIdParametre(FacesContext fc){
        Map<String, String> parametre =        fc.getExternalContext().getRequestParameterMap();
return Long.valueOf(parametre.get("urunid")).longValue();
    }

EDIT:

This is now my current implementation, it returns null.

i am developing a project using JSF. In an opening popup window, i want to show some details about a product but can not get view scoped bean' s value on a datatable.

Can you help me?

Thanks.

Here is my UrunuDenetlemeSayfasi.xhtml code snippet:

<h:commandLink onclick="window.open('UruneGozAt.xhtml','Ürün İçeriği',
config='width=700, height=400, top=100, left=100, scrollbars=no, resizable=no');"
value="Ürün İçeriğine Göz At"> <f:param name="urunId" value="#{urun.urunID}" />
</h:commandLink>

Here is UruneGozAt.xhtml:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">

<f:metadata>
<f:viewParam name="urunId" value="#{uruneGozAtBean.urunId}"
    required="false" />
<f:viewAction action="#{uruneGozAtBean.urunIdsineGoreUrunIcerigiGetir()}" />
</f:metadata>

<h:head>
<title>Ürün İçeriği</title>
<!-- add this always, even if it's empty -->
</h:head>

<h:body>
<h:dataTable class="table table-striped"
    value="#{uruneGozAtBean.urunIcerik}" var="urun">
    <h:column>
        <f:facet name="header">
            <h:outputText value="barkod no" />
        </f:facet>
        <h:outputText value="#{urun.barkodNo}" />
    </h:column>
</h:dataTable>
</h:body>
</html>

Here is UruneGozAtBean.java

@ManagedBean
@ViewScoped
public class UruneGozAtBean {

public UrunDenetlemeSayfasiBean urunDenetle = new UrunDenetlemeSayfasiBean();

public UrunDenetleService urunService = new UrunDenetleService();

private ArrayList<UrunIcerik> urunIcerik = new ArrayList<UrunIcerik>();

private Long urunId;

public Long getUrunId() {
    return urunId;
}

public void setUrunId(Long urunId) {
    this.urunId = urunId;
}

public ArrayList<UrunIcerik> getUrunIcerik() {
    return urunIcerik;
}

public void setUrunIcerik(ArrayList<UrunIcerik> urunIcerik) {
    this.urunIcerik = urunIcerik;
}

public void urunIdsineGoreUrunIcerigiGetir() {
    setUrunIcerik(urunService.urunIdsineGoreUrunIcerigiGetir(urunIdEldeEt()));
    System.out.print("aaa");
}

public Long urunIdEldeEt() {
    FacesContext fc = FacesContext.getCurrentInstance();
    setUrunId(getUrunId(fc));
    return getUrunId();
}

public Long getUrunId(FacesContext fc) {
    Map<String, String> parametre =     fc.getExternalContext().getRequestParameterMap();
return Long.valueOf(parametre.get("urunId")).longValue();
}
}
Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
devgirl
  • 39
  • 10

2 Answers2

3

@ViewScoped beans are alive per view. If you open a popup window from your current view, then you're opening a new view, so even if it uses the same managed bean to display the data, since they're different views, they use different instances of the same class.

In cases like this, you should pass a parameter through query string, then receive it in your view and process it to load the desired data. In this case, your code would be like this (note: make sure you send the parameter with name "urunId"):

UrunuGozAt.xhtml:

<!DOCTYPE html>
<html 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">
<h:head>
    <!-- add this always, even if it's empty -->
    <f:metadata>
        <f:viewParam name="urunId" value="#{uruneGozAtBean.urunId}"
            required="false" />
        <f:viewAction action="#{uruneGozAtBean.loadData}" />
    </f:metadata>
</h:head>
<h:body>
    <h:dataTable class="table table-striped"
        value="#{uruneGozAtBean.urunIcerik}" var="urun">
        <h:column>
            <f:facet name="header">
                <h:outputText value="barkod no" />
            </f:facet>
            <h:outputText value="#{urun.barkodNo}" />
        </h:column>
    </h:dataTable>
</h:body>
</html>

UruneGozAtBean managed bean:

@ViewScoped
@ManagedBean
public class UruneGozAtBean {
    //your current fields, getters and setters...
    private Long urunId;
    //getter and setter for this field...

    public void loadData() {
        if (urunId != null) {
            //load the data for the table...
        }
    }
}

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
  • well, thank you so much for the reply. but is there any possibility for throwing null exception because metadata returns null? i think i have such a problem. – devgirl Mar 08 '14 at 11:53
  • @devgirl are you sure you're sending the right query string parameter? – Luiggi Mendoza Mar 08 '14 at 17:13
  • i tested it. when i add the `metadata` part, it returns null. when i removed it, it returns a blank page. – devgirl Mar 09 '14 at 23:21
  • @devgirl what do you mean by *it returns `null`*? Please edit the question accordingly and post the stacktrace. – Luiggi Mendoza Mar 09 '14 at 23:33
  • @devgirl you missed the stacktrace where it says `null`. Otherwise, nor me or anybody else will be able to analyze your problem. Also, when editing your question, do not remove the old content, instead add the new one below. – Luiggi Mendoza Mar 10 '14 at 00:05
  • so.. what is your suggested solution for this problem, have any ideas? thanks. – devgirl Mar 10 '14 at 13:53
  • @devgirl again, I can't see any problem for your current given code. Please post any related stacktrace in order to analyze your current issues. – Luiggi Mendoza Mar 10 '14 at 14:24
0

DataTable expects a list to iterate through, but as far as I can see you return an UrunIcerik object.

Rosty
  • 130
  • 1
  • 7