JSF2.0 @ManagedBean annotation not working

I am trying to write my first JSF2.0 project (using EJB3.1). I do not understand why my @ManagedBean annotation does not work.

I always get an error message when I run the application on Glassfish v3

exceptions

javax.servlet.ServletException: /login.xhtml @ 34,133 value = "# {loginBean.login}": Target unreachable, id 'loginBean' allowed null

The main reason

javax.el.PropertyNotFoundException: /login.xhtml @ 34,133 value = "# {loginBean.login}": Target not available, identifier 'loginBean' allowed null

If I define a managed bean in faces-config.xml, it will work. But I want to use annotation.

Maybe I'm using the wrong libraries in my poses?

Managed example (this will be the transfer object):

package edu.tsystems.vmmail.web.core.domain; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; import java.io.Serializable; @ManagedBean @ViewScoped public class LoginBean implements Serializable { private String login; private String password; public LoginBean() {} public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } 

login.xhtml (where I can try to use it):

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <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:p="http://primefaces.org/ui"> <f:loadBundle var="common" basename="edu.tsystems.vmmail.web.ui.MessageResources" /> <h:head> <title>Welcome to VMMail Web Interface</title> <link type="text/css" href="#{request.contextPath}/css/style.css" rel="stylesheet" /> </h:head> <h:body> <f:view> <h:form id="loginForm" method="post"> <p:panelGrid id="mainLogin" styleClass="noInnerBorderTable"> <f:facet name="header"> <p:row> <p:column colspan="4"> <h:outputText value="#{common['login.welcome']}" /><br/> <h:message for="loginBean" id="login1Error" /> </p:column> </p:row> </f:facet> <p:row> <p:column rowspan="2"> <div class="logoCell"></div> </p:column> <p:column> <h:outputText value="#{common['field.login']}" for="loginBean" /> </p:column> <p:column> <p:inputText id="loginBean" required="true" value="#{loginBean.login}" requiredMessage="#{common['field.login.required']}" /> </p:column> <p:column rowspan="2"> <div class="submitButtonCell"> <p:commandLink styleClass="loginAnchor" title="#{common['field.loginButton']}" action="#{userController.loggingIn(login)}" ajax="false" /> </div> </p:column> </p:row> <p:row> <p:column> <h:outputText for="password" value="#{common['field.password']}" /> </p:column> <p:column> <p:password id="password" required="true" value="#{loginBean.password}" requiredMessage="#{common['field.password.required']}" /> </p:column> </p:row> <f:facet name="footer"> <p:row> <p:column colspan="4"> <h:outputText value="#{common['login.notHave']}" /> <a href="#{request.contextPath}/registration.xhtml"> <h:outputText value="#{common['login.registerNow']}" /> </a> </p:column> </p:row> </f:facet> </p:panelGrid> </h:form> </f:view> </h:body> </html> 

UserController Class:

 package edu.tsystems.vmmail.web.core.controllers; import edu.tsystems.vmmail.web.core.dao.UserDAO; import edu.tsystems.vmmail.web.core.domain.LoginBean; import edu.tsystems.vmmail.web.core.model.UserEntity; import javax.ejb.EJB; import javax.ejb.Stateless; import javax.faces.application.FacesMessage; import javax.faces.bean.ViewScoped; import javax.faces.context.FacesContext; import javax.servlet.http.HttpSession; @Stateless @ViewScoped public class UserController { @EJB private UserDAO userDAO; private UserEntity user; public boolean isLoggedIn() { return user != null; } public String loggingIn(LoginBean loginBean) { FacesContext context = FacesContext.getCurrentInstance(); if(userDAO == null) { context.addMessage("loginForm:login1Error", new FacesMessage("DAO IS NULL!")); // return "/loginBean.xhtml?faces-redirect=true&error=1"; } user = userDAO.getUserByLoginAndPassword(loginBean.getLogin(), loginBean.getPassword()); if (user != null) { FacesContext facesContext = FacesContext.getCurrentInstance(); HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false); session.setAttribute("user", user.getId()); return "/mail/mail.xhtml?faces-redirect=true"; } else { return "/loginBean.xhtml?faces-redirect=true"; } } public String logout() { FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); return "/login.xhmtl?faces-redirect=true"; } } 

I really don't understand why this is not working :( What am I doing wrong?

UPD: Stack Trace: http://pastebin.com/istJmMHr

Source code can be downloaded from my Google drive: https://docs.google.com/file/d/0B4Am7SXJwmtKNVc0LVhWVlEyMVk/view

+4
source share
2 answers

This happened because my @ManagedBean was placed in an EJB package, not a WAR package.

When I moved all @ManagedBeans to my WAR module, everyone worked!

0
source

I think you can start with a really small example to understand the essence of things. There is a lot of wrong code in the code.

To get started, @Stateless bean could not be viewed. Think about it for a moment. What would really mean that a stateless object is limited to a bean? Why did you consider what you needed first?

There should be one bean support in the view, and this view is often viewed. Any DTOs you may need for this view should not be scanned, but should simply be instance variables of the main bean support. Thus, they automatically depend on this area.

In your case, make the loginBean an instance variable, just like a user variable.

0
source

Source: https://habr.com/ru/post/1468736/


All Articles