Archie92
Archie92

Reputation: 39

JSF: f:viewParam doesn't call setter, f:viewAction doesn't call business-method

I'm totally stuck with using <f:viewParam> and <f:viewAction>.

I've read several similiar Questions like this, but couldn't get it working, yet.

Simple example as follows:

test.xhtml

<h:head>
    <title>Test</title>
</h:head>
<h:body>

    <f:view>
        <f:viewParam name="reason" value="#{testBean.reason}"/>
        <f:viewAction action="#{testBean.examineReason}"/>
    </f:view>

    <h:outputText value="Reason=#{testBean.reason}"/>
</h:body>

TestBean

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@RequestScoped
public class TestBean {

    private String reason;

    @PostConstruct
    private void init() {
        System.out.println("urlParameter: "
                        + FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("reason"));

    }

    public void examineReason() {
        System.out.println("Reason is " + reason);
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
        System.out.println("Reason set: " + reason);
    }

}

The GET-Request ist initiated by:

<h:button outcome="/pages/test.jsf" value="GET">
    <f:param name="reason" value="abcd" />
</h:button>

System.out reports only:

urlParameter: abcd

Seems that neither f:viewParam nor f:viewAction is working at all.

I tested with Mojarra 2.2 and MyFaces 2.2 on Tomcat 8.0.32

What am I missing?

EDIT:

Problem solved: the <f:metadata>-tag was missing. Furthermore, the <f:metadata> must not be a child of <h:body>. With the following modifications in test.xhtml it worked for me:

<h:head>
    <title>Test</title>
</h:head>
<body>

    <f:view>
        <f:metadata>
        <f:viewParam name="reason" value="#{testBean.reason}"/>
        <f:viewAction action="#{testBean.examineReason}"/>
        </f:metadata>
    </f:view>

    <h:outputText value="Reason=#{testBean.reason}"/>
</body>

Upvotes: 1

Views: 82

Answers (0)

Related Questions