request parameter setter (not getter!) called multiple times

UI Components for JSF
Post Reply
hybris
Posts: 17
Joined: 20 Oct 2010, 14:29

05 Jan 2011, 17:47

I have this request scoped bean:

Code: Select all

<managed-bean>
        <managed-bean-name>perioddetail</managed-bean-name>
        <managed-bean-class>
            manager.PeriodDetail
        </managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
        <managed-property>
            <property-name>selectedPeriodId</property-name>
            <value>#{param.selectedPeriodId}</value>
        </managed-property>
    </managed-bean>
The page is called by clicking on a link:

Code: Select all

<h:outputLink value="periodDetail.xhtml" rendered="#{phaseStatus.shift != null}">
                                <h:outputText style="text-decoration:underline"  value="Dettaglio"/>
                                <f:param name="selectedPeriodId" value="shift.#{phaseStatus.shift.id}"/>
                            </h:outputLink>
And i have 2 problems and the second is killing me:
1- the setter is called multiple times. I can live with that but why? Is it the same reason regarding getters in JSF?
2- The last call instead of the actual parameter, the setter is called with NULL. This kinda breaks my logic.

Primefaces 2.2RC2, Glassfish V3 with Mojarra 2.0.3

Any hint?

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

05 Jan 2011, 18:12

Why dont you use viewparams and GET support of JSF 2.0, #{param.something} is an old hack way of doing it with JSF 1.2. And also use h:link instead of h:outputLink.

hybris
Posts: 17
Joined: 20 Oct 2010, 14:29

11 Jan 2011, 17:47

Thanks! Now the setter is called 1 time with the correct value.
I have still a lot to learn about JSF in general, not only about JSF2.

But i have still a problem, in the page i link i have this:

Code: Select all

<p:graphicImage rendered="#{perioddetail.periodYieldChart != null}" value="#{perioddetail.periodYieldChart}"/>
That calls this method:

Code: Select all

public StreamedContent getPeriodYieldChart() {
        System.err.println("get chart " + selectedPeriodId);
        Set<PeriodicStatistics> sp = getSelectedPeriod().getPeriodicStatistics();
        List<PeriodicStatistics> l = new ArrayList<PeriodicStatistics>();
        l.addAll(sp);
        Collections.sort(l, new Comparator<PeriodicStatistics>() {

            @Override
            public int compare(PeriodicStatistics o1, PeriodicStatistics o2) {
                return o1.getStatisticsTimestamp().compareTo(o2.getStatisticsTimestamp());
            }
        });
        JFreeChart chart = Charts.createPeriodYieldGraph(selectedPeriod, l, 0, l.size());
        return chartToStreamedContent(chart);
    }

As you can see i put a println at the beginning.

This is the output i get:

Code: Select all

GRAVE: get chart product.8bdb8d85-efa0-48de-b3dc-82df43a3b177
GRAVE: get chart product.8bdb8d85-efa0-48de-b3dc-82df43a3b177
GRAVE: get chart product.8bdb8d85-efa0-48de-b3dc-82df43a3b177
GRAVE: get chart product.8bdb8d85-efa0-48de-b3dc-82df43a3b177
GRAVE: get chart product.8bdb8d85-efa0-48de-b3dc-82df43a3b177
GRAVE: get chart null
Again a call with an unexpected null value that i do not know how to handle.
:?: :?: :?:

Maybe i should move the graph generation out of that method but how to handle the call with the null value? I have to return a StreamedContent and a null value is not tolerated by Primefaces

kman
Posts: 57
Joined: 03 Nov 2010, 09:21
Location: Hong Kong

12 Jan 2011, 08:14

for dealing with conditional rendering with null value, you may try using standard JSF EL Expression operator "empty" instead of comparing it to "null", for example:

Code: Select all

<p:graphicImage rendered="#{!empty perioddetail.periodYieldChart}" value="#{perioddetail.periodYieldChart}" />
cheers
PrimeFaces 3.0M1, Sun Mojarra 2.0.4 FCS, Sun GlassFish JSTL 1.2,
Apache Log4J 1.2.16, EclipseLink 2.1.1.v20100817-r8050,
Eclipse Helios

hybris
Posts: 17
Joined: 20 Oct 2010, 14:29

12 Jan 2011, 10:30

thanks but i think i should understand why i get that null call first

anyway looking at it better, my rendered test is useless since that method never returns null and, worst, throws an NPE

hybris
Posts: 17
Joined: 20 Oct 2010, 14:29

17 Jan 2011, 12:47

I modified the code this way with no success:

I used the viewParam feature and i removed the old style parameter declaration in the faces-config:

Code: Select all

<f:metadata>
                <f:viewParam name="selectedPeriodId" value="#{perioddetail.selectedPeriodId}"/>
            </f:metadata>

Code: Select all

public void setSelectedPeriodId(String selectedPeriodId) {
        this.selectedPeriodId = selectedPeriodId;
        System.err.println("set id " + selectedPeriodId);
        if(selectedPeriodId == null) {
            return;
        }
        try {
            String ss[] = selectedPeriodId.split("\\.");
            if("shift".equals(ss[0])) {
                selectedPeriod = dBHelperBean.find(Shift.class, ss[1]);
            }
            if("lot".equals(ss[0])) {
                selectedPeriod = dBHelperBean.find(Lot.class, ss[1]);
            }
            if("product".equals(ss[0])) {
                selectedPeriod = dBHelperBean.find(Product.class, ss[1]);
            }
            scPeriodYieldChart = createPeriodYieldChart();
            scPeriodUPHChart = createPeriodUPHChart();
            StreamedContent sc[] = createPeriodInactivitiesChart();
            scPeriodInactivitiesChart = sc[0];
            scPeriodInactivitiesDurationChart = sc[1];
        } catch(PTrackException ex) {
//FIXME
            ex.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(Utils.fullStacktraceToHtml(ex)));
        }
    }
This way the above setter is called exactly once with a NON null parameter.
In the same method i put the graph generation that before i had in the getter of the graph linked to the graphicImage tag, i also removed the null test

Code: Select all

<p:panel header="Grafici">
                    <p:graphicImage value="#{perioddetail.periodYieldChart}"/>
                </p:panel>
The getter is now very simple as it should be:

Code: Select all

   public StreamedContent getPeriodYieldChart() {
        System.err.println("get chart " + selectedPeriodId);
        return scPeriodYieldChart;
    }

As you see i put some println in the setter and getter to see what happens and here it is:

Code: Select all

GRAVE: set id shift.c6816dce-c063-407b-86ad-431757c33a36
GRAVE: get chart shift.c6816dce-c063-407b-86ad-431757c33a36
GRAVE: get chart shift.c6816dce-c063-407b-86ad-431757c33a36
GRAVE: get chart shift.c6816dce-c063-407b-86ad-431757c33a36
GRAVE: get chart null
GRAVE: get chart null
Again i have calls on the getter and the value of the id i use to generate the graphics is null.
The result is a broken image in the generated page. :evil:

hybris
Posts: 17
Joined: 20 Oct 2010, 14:29

17 Jan 2011, 18:44

update, i learnt i should use f:param but still no way to go around this bit :(

when i get the periodId parameter it is always null
i found posts about similar problems but i did not understand what they did to solve the problem

Code: Select all

<p:graphicImage id="giYiled" value="#{perioddetail.periodYieldChart}" cache="FALSE">
                        <f:param id="pgiYieldPeriodId" name="periodId" value="aaaaaaargh"/>
                    </p:graphicImage>
public StreamedContent getPeriodYieldChart() {
System.err.println("get chart " + selectedPeriodId);
Map<String,String> map = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("periodId");
System.err.println("id: " + id);
if (id == null) {
return null;
}
return createPeriodYieldChart(id);
}

Code: Select all

<img id="j_idt17:giYiled" src="" alt="" />

Code: Select all

GRAVE: set id product.b635b94a-7896-4379-99a9-9a55fa96a8e4
GRAVE: get chart product.b635b94a-7896-4379-99a9-9a55fa96a8e4
GRAVE: id: null
GRAVE: set id product.b635b94a-7896-4379-99a9-9a55fa96a8e4
GRAVE: get chart product.b635b94a-7896-4379-99a9-9a55fa96a8e4
GRAVE: id: null

ppaulraj
Posts: 1
Joined: 28 Feb 2013, 07:05

28 Feb 2013, 08:10

is anyone got solution for this problem. Please ring the bell.

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

28 Feb 2013, 09:11

please please please read the link in my signature and the one about 'new users'... PLEASE....

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 53 guests