2

I have the following XHTML page.

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">

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

    <f:metadata>
        <f:viewParam name="id" value="#{testManagedBean.id}" maxlength="20"/>
    </f:metadata>

    <h:body>
        <h:form id="form" prependId="true">

        </h:form>
    </h:body>
</html>

The managed bean corresponds to the above JSF page.

@ManagedBean
@ViewScoped
public final class TestManagedBean implements Serializable
{
    private static final long serialVersionUID = 1L;
    private Long id;

    @PostConstruct
    private void init() {
        System.out.println("id = "+id);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

I'm passing id as a query-string parameter using the URL - https://localhost:8181/Project-war/admin_side/Test.jsf?id=1.

Trying to display id in the init() method is always null.

This is a demonstration. In reality, <f:viewParam> is actually put inside <ui:define name="metaData"> and on the master template <ui:insert name="metaData"/> is defined.

What am I overlooking here?

I have several times passed such parameters and converted to a JPA entity using appropriate converters but I don't know why a scalar value is not being set to a property of a managed bean. I have tried changing the type of id to String but that also did not help either (nor @ManagedProperty(value="#{id}") worked).

1

1 Answer 1

3

The value of f:viewParam is not available during @PostConstruct. Use f:viewAction instead.

See

See also

Sign up to request clarification or add additional context in comments.

2 Comments

In which namespace was <f:viewAction> defined? It is not available in http://java.sun.com/jsf/core. This namespace http://xmlns.jcp.org/jsf/core as stated by this answer is also not available. It gives an error, No library found for namespace http://xmlns.jcp.org/jsf/core. I'm using Mojarra 2.2.6.
The namespaces like http://xmlns.jcp.org/... became available only when I upgraded the NetBeans IDE from 7.2.1 to the latest release 8.0. Like Eclipse, It was a quirk in NetBeans.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.