2

I am having this problem in this sample jsf project I created. Managed beans do not get instantiated. Bean class:

@ManagedBean(name="loginMB")
@RequestScoped
public class LoginMB extends AbstractMB {

    private static final long serialVersionUID = -8523135776442886000L;
    @ManagedProperty("#{userMB}")
    private UserMB userMB;

    //getters and setters 

   public String login() {      
        UserSupport userSupport = new UserSupportImpl();        
        User user = userSupport.isValidLogin(email, password);      
        if (user != null) {
            getUserMB().setUser(user);
            FacesContext context = FacesContext.getCurrentInstance();
            HttpServletRequest request = (HttpServletRequest) context
                    .getExternalContext().getRequest();
            request.getSession().setAttribute("user", user);
            return "loggedIn";
            //return "/pages/protected/index.xhtml";
        }
        displayErrorMessageToUser("Check your email/password");
        return null;
    }
}

ManagedBean annotation and RequestScope annotation have been imported from

import javax.faces.bean.*;

this is how i ve used above bean,

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
    <p>#{bundle.loginHello}</p>
    <h:form>
        <p:growl showDetail="false" life="3000" />
        <h:panelGrid columns="2">
            <h:outputLabel value="#{bundle.loginUserName}" />
            <h:inputText value="#{loginMB.email}" label="Email" id="email" required="true">             
                <f:validateLength minimum="6" />
            </h:inputText>
            <h:outputLabel value="#{bundle.loginPassword}" />

            <h:inputSecret value="#{loginMB.password}" label="Password" id="senha" required="true" autocomplete="off" >         
                <f:validateLength minimum="6" />
            </h:inputSecret>


        </h:panelGrid>
        <p:commandButton action="#{loginMB.login}" value="Log in" ajax="false" />
    </h:form>
</h:body>
</html>

Other managed bean

@SessionScoped
@ManagedBean(name = "userMB")
public class UserMB implements Serializable {
    public static final String INJECTION_NAME = "#{userMB}";
    private static final long serialVersionUID = 1L;

    private User user;
        .......
}

Exception:

javax.el.PropertyNotFoundException: /login.xhtml @14,83 value="#{loginMB.email}": Target Unreachable, identifier 'loginMB' resolved to null
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)

faces-config.xml

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd"
    version="2.1">

    <application>
        <resource-bundle>
            <base-name>messages</base-name>
            <var>bundle</var>
        </resource-bundle>
        <message-bundle>messages</message-bundle>
    </application>

    <navigation-rule>
        <from-view-id>/login.xhtml</from-view-id>
        <navigation-case>
            <from-action>#{loginMB.login}</from-action>
            <from-outcome>loggedIn</from-outcome>
            <to-view-id>/index.xhtml</to-view-id>
            <redirect />
        </navigation-case>      
    </navigation-rule>
    <!-- <managed-bean>
        <managed-bean-name>loginMB</managed-bean-name>
        <managed-bean-class> com.sample.jsfjpa.beans.LoginMB</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>userMB</managed-bean-name>
        <managed-bean-class> com.sample.jsfjpa.beans.UserMB</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean> -->
</faces-config>

enter image description here

Andrei Nicusan
  • 4,388
  • 1
  • 20
  • 35
bluelabel
  • 1,550
  • 5
  • 25
  • 40
  • Maybe it is because of the scope of the beans, I do not know. Make both SessionScoped and try ? – Koray Tugay Sep 03 '13 at 12:18
  • @KorayTugay, i tried it, but no luck, by the way i ve updated question. It seems no of the beans get instantiated. – bluelabel Sep 03 '13 at 12:22
  • Please show how you access the bean. The field is not injected when the constructor is called. – atamanroman Sep 03 '13 at 12:22
  • @atamanroman updated the question with more coding – bluelabel Sep 03 '13 at 12:35
  • 'it fails', are there any errors popping up? Did you try to use `@ManagedProperty(value = "#{userMB}")` instead of `@ManagedProperty("#{userMB}")`? Are there any members in `AbstractMB ` which could conflict with your `UserMB`? – Manuel Sep 03 '13 at 13:10
  • @Manuel: they are equivalent. – BalusC Sep 03 '13 at 13:12
  • @Manuel this is the exception iam getting,`javax.el.PropertyNotFoundException: /login.xhtml @14,83 value="#{loginMB.email}": Target Unreachable, identifier 'loginMB' resolved to null at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100) at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)` – bluelabel Sep 03 '13 at 13:29
  • So `loginMB` resolved to `null`, not the `userMB` in `loginMB`. Unfortunatelly I can't see the problem (yet) based on the code provided. – Manuel Sep 03 '13 at 13:33
  • @Manuel 1. i tried to create loginMb by adding ` loginMB com.sample.jsfjpa.beans.LoginMB request ` to faces config file, in that case loginMb get instantiated. (did the same thing to userMb as well). but when i remove both beans from faces config file, none of those beans get created – bluelabel Sep 03 '13 at 13:38
  • Show us the important parts of your faces-config. Is it version 2.0? Include it your question(=edit your question). Move the exception from the comments to the question as well. – Manuel Sep 03 '13 at 13:40
  • @Manuel updated the question with faces-config – bluelabel Sep 03 '13 at 13:44
  • Is it your entire faces-config file? If yes it's malformed.. – Xtreme Biker Sep 03 '13 at 13:47
  • @XtremeBiker, i ve missed `` end tag,will updated question with end tag – bluelabel Sep 03 '13 at 13:49
  • @bluelabel have you tried declaring the managed beans in faces-config file instead of using annotations? – Xtreme Biker Sep 03 '13 at 17:01
  • @XtremeBiker, i tried that way, but then userMB does not get injected to loginMB via `@ManagedProperty`. Gets NullPOinterException when try to use userMB – bluelabel Sep 03 '13 at 21:26
  • Then you have both problems, If you have them in the configuration file (LoginMB created) is supposed to work with my answer. – Xtreme Biker Sep 04 '13 at 05:15

1 Answers1

2

Managed beans are not created unless you specifically invoke one of their properties or methods. That happens for all the scopes, except the @ApplicationScoped ones having @ManagedBean(eager=true) which are specifically created when JSF context loads.

Here you're referencing #{loginMB} from the view, but not #{userMB} so there's no chance to have it created.

In order to instance your @SessionScoped managed bean you can add following code in your login page:

<f:metadata>
    <f:event type="preRenderView" listener="#{userMB.initialize}" />
</f:metadata>

Which sets a listener that is executed before page rendering. You can invoke here just an empty method (to execute it JSF will construct your bean if not already created).

Starting from JSF 2.2 you can replace the preRenderView method for the new f:viewAction, which has some benefits (it's parameterless and it doesn't get invoked on postbacks ):

<f:metadata>
    <f:viewAction action="#{userMB.initialize}" />
</f:metadata>
Xtreme Biker
  • 28,480
  • 12
  • 120
  • 195
  • my problem is none of the beans get created, when i click login button from login.xhtml, nothing happenes. just get the `javax.el.PropertyNotFoundException: /login.xhtml @14,83 value="#{loginMB.email}": Target Unreachable, identifier 'loginMB' resolved to null at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100) at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)` – bluelabel Sep 03 '13 at 13:32
  • Then your annotations are probably being omitted in some way. Do you have faces-config.xml file in your path? Check out this [link](http://stackoverflow.com/questions/7519151/how-to-mix-annotations-with-faces-config-xml). – Xtreme Biker Sep 03 '13 at 13:37
  • `userMB` is already referenced as managed property of a managed bean which is referenced in the view, so this can't be the cause. – BalusC Sep 03 '13 at 13:48
  • @BalusC true, it should fail while page displaying, shouldn't it? – Xtreme Biker Sep 03 '13 at 13:53