0

Can you tell me what is the order of events on page load in JSF?

I have a problem with passing parameter with f:param

I am using Eclipse, postgresql, Wildfly17 and primefaces 7.0.

I have two databases, connected with OneToMany relation. In first are pests with their properties. In second database are names of every pest in different languages (index of English is 1, German is 4, ...)

On first page is searching in first database (side One) for specific pests. Results are displayed in datatable under it. In last column of datatable is Edit button. By clicking on it you go on second page with data from both databases of specific pest.

Entity SmPests is:

Integer idPest;
String latinName;
String code:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "smPests", cascade = CascadeTyoe.ALL, orphanRemoval = true)
List<SmPestsNames> smPestsNames = new ArrayList<SmPestsName>();
...

Entity SmPestsNames is:

Integer idPest;
int idLanguage;
String name;

In SmPestList.xhtml i have:

<p:button outcome="SmPestsEditable"update="pestsListEdit" value="Edit" style="fontsize:3em" title="edit">
<f:param name="id" value="#{zuzek.idPest}" />
<f:param name="code" value="#{zuzek.code}" />
<f:param name="lname" value="#{zuzek.latinName}" />

In SmPestsEdit.xhtml I have:

<f:viewParam name="id" value="#{smPestsEditable.idPest}" />
<f:viewParam name="code" value="#{smPestsEditable.code}" />
<f:viewParam name="lname" value="#{smPestsEditable.latinName}" />

My problem is that I when second page loads, idPest (passed by f:param) is not yet detected by backing bean and if I want to display names from second database I get

java.lang.NullPointerException

If i set static idPest it works but it doesn't display names of current element.

I want to se idPest before page loads.

Thank you in advance.

AFTER the page loads program does find correct children, but I want it happen BEFORE page loads.

I tried with ManagedBean but it says it is deprecated.

I also tried with viewAction

<f:viewParam name="id" value="#{smPestsEditable.idPest}" />
<f:viewParam name="code" value="#{smPestsEditable.code}" />
<f:viewParam name="lname" value="#{smPestsEditable.latinName}" />
<f:viewAction action="#{smPestsEditable.onload}" />
public void onload() {
   idPest = id; // to avoid NullPointerException I use static idPest and try to change it to current before page loads
}

UPDATE: Problem is that parameter "id" is identifier for OneToMany relation. I want to load this OneToMany relation BEFORE the page loads. In the backing bean of the second page I have this:

    private int idPest = 55;  // This one is set so there is something set
    @ManagedProperty(value="{param.id}")
    private Long Id; 
    @ManagedProperty(value="{param.id}")
    private int id;

    public void onload() {
        System.out.println(" ONLOAD: id: "+id);
            System.out.println(" idpest: "+idPest);
        System.out.println(" Id: "+Id);
    }
    

I tried to get parameter using @ManagedBean using variables both int and Long type but what I get is:

ONLOAD: id: 0
idpest: 55
Id: null

Right now it seems to me I can't get (integer) value of parameter "id" in onload

Freeky
  • 11
  • 1
  • 5
  • 1
    Just move code which you currently have in `@PostConstruct` method into `onload()` method? This was explained in your previous question. – BalusC Nov 20 '20 at 17:09
  • Problem is that parameter "id" is identifier for OneToMany relation – Freeky Nov 23 '20 at 18:53
  • That's not the problem. Your problem is that you tried to use the `id` in `@PostConstruct` method instead of in `onload()` method. The `id` is only available in `onload()` method, it is not available in `@PostConstruct` method. – BalusC Nov 23 '20 at 19:18
  • I misstyped before. Now I updated the question. Now it seems to me I just cannot bring parameter "id" in the onload() method. – Freeky Nov 23 '20 at 22:03
  • As always, you gave good comment. Now that onload isn't in @Postconstruct, it load ok. Thank you – Freeky Dec 14 '20 at 12:30

0 Answers0