views:

24

answers:

1

I don't really understand why this simple stuff won't work. I can see that I set the constructor is called everytime one property is set. The problem is that when I reach the sendInvitation method then all inviteEmails is null.

@Named(value = "adminBean")
@RequestScoped
public class AdminBean implements Serializable {

    private String inviteEmail1;
    private String inviteEmail2;
    private String inviteEmail3;
    private BasicUser administrator;
    @EJB
    private MessageBeanRemote messageBean3;

    /** Creates a new instance of AdminBean */
    public AdminBean() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().
                getExternalContext().getSession(true);
        LoginBean loginBean = (LoginBean) session.getAttribute("loginBean");
        administrator = loginBean.getUser();
    }

    public String getInviteEmail1() {
        return inviteEmail1;
    }

    public void setInviteEmail1(String inviteEmail) {
        this.inviteEmail1 = inviteEmail;
    }

    public String getInviteEmail2() {
        return inviteEmail2;
    }

    public void setInviteEmail2(String inviteEmail) {
        this.inviteEmail2 = inviteEmail;
    }

    public String getInviteEmail3() {
        return inviteEmail3;
    }

    public void setInviteEmail3(String inviteEmail) {
        this.inviteEmail3 = inviteEmail;
    }

    public void sendInvitations() { 
    ...
    }
}

The page which is using this bean is a component that is is part of another page:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"&gt;
    <ui:component id="adminComp">
        <h3>Adminstration page</h3>
        <h:form id="inviteForm">
            <h:panelGrid columns="1">
                Invite new members to your group. Type in their emails in the fields below.
                <h:inputText id="email1" value="#{adminBean.inviteEmail1}" size="40"/>
                <h:inputText id="email2" value="#{adminBean.inviteEmail2}" size="40"/>
                <h:inputText id="email3" value="#{adminBean.inviteEmail3}" size="40"/>
                <h:commandButton value="invite" action="#{adminBean.sendInvitations}"/>
            </h:panelGrid>
        </h:form>
    </ui:component>


</html>

The adminBean is actually initialized many times and it looks kind of weird. "Constructor" is the adminBean constructor, "Get 1" is getInviteEmail1 and so on.

INFO: START PHASE RESTORE_VIEW 1
INFO: END PHASE RESTORE_VIEW 1
INFO: START PHASE APPLY_REQUEST_VALUES 2
INFO: END PHASE APPLY_REQUEST_VALUES 2
INFO: START PHASE PROCESS_VALIDATIONS 3
INFO: Constructor
INFO: Constructor
INFO: Constructor
INFO: Constructor
INFO: Constructor
INFO: Constructor
INFO: Get 2
INFO: Constructor
INFO: Constructor
INFO: Constructor
INFO: Get 3
INFO: END PHASE PROCESS_VALIDATIONS 3
INFO: START PHASE UPDATE_MODEL_VALUES 4
INFO: Constructor
INFO: Set 1
INFO: Constructor
INFO: Set 2
INFO: Constructor
INFO: Set 3
INFO: END PHASE UPDATE_MODEL_VALUES 4
INFO: START PHASE INVOKE_APPLICATION 5
INFO: Constructor
SEVERE: NullPointerException....
INFO: END PHASE INVOKE_APPLICATION 5
INFO: START PHASE RENDER_RESPONSE 6
INFO: Constructor
INFO: Constructor
INFO: Get 2
INFO: Constructor
INFO: Get 3
INFO: END PHASE RENDER_RESPONSE 6

Any help is appriciated.

+1  A: 

I don't know what that @Named thing is doing there, but it is not normal that the bean is constructed everytime it is been accessed by a value expression. It should be constructed only once in beginning of the HTTP request. Probably the configuration behind @Named is been wrong.

Try just declaring the bean the JSF way using @ManagedBean and ensure that @RequestScoped is also from the javax.faces.bean package.

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class AdminBean implements Serializable {
    // ...
}

Apart from the problem, your way of obtaining the session scoped bean is not recommended. The normal JSF way would be the following:

@ManagedProperty(value="#{loginBean}")
private LoginBean loginBean;

if necessary with the following getter

public BasicUser getAdministrator() {
    return loginBean.getUser();
}
BalusC
It's because my netbeans automatically made that @Named when i make i new ManagedBean. I just keep overlooking those things :)
AnAmuser