4

I've got an xhtml page which uses a backing bean to get it's information. In this backing bean I've got a method defined with a @PostConstruct annotation. However this method is never called and I can't figure out why. The log doesn't show any errors.

I am using Java EE 7 and Payara 5.181.

I have tried the following things:

  • Removed all the generated data & cache in the server.
  • Removed the inject of the ModeratorService.
  • Checked the documentation of PostConstruct to see if everything is setup as it should.

Here is my code:

moderator.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html   xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:p="http://primefaces.org/ui"
        >

    <h:head>
        <title>Moderator Pagina</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <link rel="stylesheet" type="text/css" href="style.css"/>
    </h:head>
    <h:body>
        <p:dataTable var="user" value="#{moderatorManager.twitterUsers}">
            <p:column headerText="Username">
                <h:outputText value="#{user.username}" />
            </p:column>
        </p:dataTable>
    </h:body>
</html>

ModeratorManager.java

package com.guido.web;

import com.guido.models.TwitterUser;
import com.guido.services.ModeratorService;
import java.io.Serializable;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@SessionScoped
public class ModeratorManager implements Serializable {

    @Inject
    ModeratorService moderatorService;

    private Set<TwitterUser> twitterUsers;

    @PostConstruct
    public void init() {
        System.out.println("Postconstruct method is called!"); // <-- This does not show up in the server log.
        twitterUsers = moderatorService.getUsersWithRole();
        System.out.println("Initialized moderatorManager: " + twitterUsers.toString()); // <-- This does not show up in the server log.
    }

    public Set<TwitterUser> getTwitterUsers() {
        return twitterUsers;
    }

    public void setTwitterUsers(Set<TwitterUser> twitterUsers) {
        this.twitterUsers = twitterUsers;
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Guido
  • 1,161
  • 3
  • 12
  • 33
  • 1
    Your code looks OK. Only check if you use javax.annotation.PostConstruct and no some other PostConstruct annotation. The postConstruct method should be called the first time ModeratorManager is used in an HTTP session. Try to delete all cookies for your application or restart your browser and access moderator.xhtml - you should see "Postconstruct method is called!" somewhere in the log output. – OndroMih May 22 '18 at 07:30
  • This problem needs to be treated the same way as "Target Unreachable, identifier 'bean' resolved to null" in abovelinked duplicate. You're most probably simply missing a physical `beans.xml` file. – BalusC Jan 19 '22 at 13:59

0 Answers0