3

I'm using JBoss Wildfly 8.2.0 and noticed that <f:viewParam> is called twice on my page:

  • The first time is when I access my page with the given param in the URL
  • The second time is upon the first AJAX request I make from that page

Bean:

@Named
@javax.faces.view.ViewScoped
public class MessageBean implements Serializable {

    private String message;
    private int count;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        System.out.println("setting message");
        this.message = message;
    }

    public void increment() {
        this.count++;
    }

    public int getCount() {
        return count;
    }
}

XHTML:

<!DOCTYPE html ...>
<html ...>
    <f:metadata>
        <f:viewParam name="message" value="#{messageBean.message}" />
    </f:metadata>
    <h:head>
        <title>Message</title>
    </h:head>
    <h:body>
        <h:form>
            <h:outputText value="#{messageBean.message} (#{messageBean.count} time(s))" />
            <h:commandButton value="increment">
                <f:ajax render="@form" listener="#{messageBean.increment}" />
            </h:commandButton>
        </h:form>
    </h:body>
</html>

I don't understand why AJAX requests are triggering the <f:viewParam> and why only the first one.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Xavier Dury
  • 1,530
  • 1
  • 16
  • 23
  • 1
    It also happens to me with Mojarra 2.2.10 + Tomcat. I've found this related question (which doesn't explain the behaviour BTW): http://stackoverflow.com/questions/21222348/using-fviewparam-with-required-attribute-and-commands – Aritz Apr 30 '15 at 08:02
  • Thanks for the link. This is really weird because I thought `` would have had the same problem but it doesn't. So I think this is a bug in mojarra. If only somebody (@BalusC ?) could confirm... – Xavier Dury Apr 30 '15 at 08:15
  • 1
    [Process f:viewParam only on page load](http://stackoverflow.com/q/23903351/1391249). – Tiny Apr 30 '15 at 10:08
  • @Tiny: Yes! I had that answer in mind while searching for a dupe, but I couldn't find it. – BalusC Apr 30 '15 at 11:24

1 Answers1

3

This is caused by statefulness of the <f:viewParam>.

JSF utility library OmniFaces has solved this with its <o:viewParam>.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555