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).

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • http://stackoverflow.com/q/10261453, http://stackoverflow.com/q/9844526, http://stackoverflow.com/q/6377798 – BalusC Apr 12 '14 at 06:40

1 Answers1

3

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

See

See also

Community
  • 1
  • 1
  • 1
    In which namespace was `` defined? It is not available in `http://java.sun.com/jsf/core`. This namespace `http://xmlns.jcp.org/jsf/core` as stated by [this](http://stackoverflow.com/a/18352652/1391249) 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. – Tiny Apr 11 '14 at 21:44
  • 1
    The namespaces like `http://xmlns.jcp.org/...` became available only when I upgraded the NetBeans IDE from [7.2.1](https://netbeans.org/downloads/7.2.1/) to the latest release [8.0](https://netbeans.org/downloads/). Like Eclipse, It was a [quirk](https://netbeans.org/bugzilla/show_bug.cgi?id=228064) in NetBeans. – Tiny Apr 12 '14 at 06:44