1

I have this f:viewParam to set value and do search in back bean in view:

<f:metadata>
<f:viewParam name="id"
    value="#{editorBean.id}"
    required="true" />
<f:event type="preRenderComponent"
    listener="#{editorBean.search}" />
...

Back bean:

private String id; // getters setters

public void search(ComponentSystemEvent event) {


    if (id != null) {
            //search data in DB to construct TreeNode finBy(id)...
...

In browser I can't expand the second level of tree, because in backing Bean the id is null..

enter image description here

Debug:

enter image description here

How to f:viewParam be set in all calls?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Eric Garcia
  • 190
  • 1
  • 1
  • 8

1 Answers1

3

It's caused because the <h:form> submits by default to an URL without the query string.

Either put the bean in the view scope,

@ManagedBean
@ViewScoped
public class EditorBean {

and skip the prerenderview during postback

public void search(ComponentSystemEvent event) {   
    if (FacesContext.getCurrentInstance().isPostback()) {
        return;
    }

    // ...
} 

A view scoped bean lives as long as you interact with the same view and thus the properties doesn't need to be initialized again and again.

Or make use of OmniFaces <o:form> which offers an includeViewParams attribute to include view parameters in form action URL:

<o:form includeViewParams="true">

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I'm using Spring Beans. View Scope dosen't exists – Eric Garcia May 24 '13 at 13:29
  • Then just go for the alternative. – BalusC May 24 '13 at 13:30
  • @BalusC: Does it make sense that like the title describes, it happens **after** the second call? Logic and your answer makes me think it happens after the first or rather during the first ajax call. I'm going to create a test case but https://stackoverflow.com/questions/61762633/why-does-the-first-ajax-call-reset-my-view-parameter already has this behaviour. Is the title by accident wrong or could have been like this in the earlier days and the behaviour we see nowadays is the more logical one? – Kukeltje May 19 '20 at 18:29
  • @Kukeltje: you're right, the title was ambiguous. The OP likely meant to say "after second request". This will happen when you open the page normally and then fire a postback on it. – BalusC May 19 '20 at 22:21
  • @BalusC: Seems that the original title _**was correct**_ I tried https://stackoverflow.com/questions/61762633/why-does-the-first-ajax-call-reset-my-view-parameter on Mojarra 2.3.9SP02 (Wildfly 16) and on the first ajax call it is indeed not transferred but.... The setter _**IS**_ called. I'm currently checking if it is related to https://stackoverflow.com/questions/10692404 and the referenced https://stackoverflow.com/questions/3933786 In this the https://github.com/javaserverfaces/mojarra/issues/2266 is referenced. I'm checking if it is related but maybe you know. – Kukeltje May 24 '20 at 11:02
  • `o:viewParam` works btw, and there is a lot of info in the `o:viewParam` about the postback behaviour and references to issues in JSF <= 2.2 and the "interpret as null" activation in 2.3 but still weird why it happens once on postback and not always... (or never) – Kukeltje May 24 '20 at 11:40