Bean没有实例化 [英] Bean not instantiated

查看:162
本文介绍了Bean没有实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在使用的平台




  • Fedora 20;

  • mariadb-5.5.34-2.fc20.x86_64;

  • 来自www.eclipse.org的Eclipse Kepler服务版本;

  • Apache TomEE plus 1.6 .0。



我正在实施示例



见这里



我正努力设法登录界面。我的问题是Login.xhtml的Java Bean尚未实例化。这怎么可能?我对java Beans非常熟悉,我检查了错误,这个例子来自一位专业的Java程序员。
我发现了这个问题,因为我没有设法登录,调试器没有显示任何变量。
Eclipse:调试Java EE中没有显示变量



我附上了login.xhtml和Java bean。我省略了GUI库,因为我认为它们不是问题的一部分。



Login.xhtml

 < ui:composition template =/ templates / layout.xhtml
xmlns =http://www.w3.org/1999/xhtml
xmlns:f =http://java.sun.com/jsf/core
xmlns:h =http://java.sun.com/jsf/html
xmlns:ui =http://java.sun.com/jsf/facelets
xmlns:p =http://primefaces.org/ui
>
< ui:define name =content>
< h:form styleClass =loginPanelStyle>
< p:growl id =msgsshowDetail =truesticky =false/>
< p:panelGrid columns =2>
< f:facet name =header>
登录面板
< / f:facet>
< h:outputText value =用户名:>< / h:outputText>
< p:inputText id =usernamevalue =#{loginController.username}required =truerequiredMessage =请输入用户名!消息= FC >
< f:validateLength minimum =1/>
< / p:inputText>
< h:outputText value =密码:>< / h:outputText>
< p:password id =passwordvalue =#{loginController.password}required =truerequiredMessage =Please Enter password!>
< f:validateLength minimum =1/>
< / p:密码>
< f:facet name =footer>
< p:commandButton value =Submitupdate =msgsactionListener =#{loginController.login}type =submiticon =ui-icon-checkstyle =margin:0 >< / p:的commandButton>
< / f:facet>
< / p:panelGrid>
< / h:form>
< / ui:define>
< / ui:composition>

LoginController.java

 包控制器; 

import util.DateUtility;
import java.io.IOException;
import java.io.Serializable;
import java.security.Principal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/ **
*登录控制器类仅允许经过身份验证的用户登录Web
*应用程序。
*
* @author Emre Simtay< emre@simtay.com>
* /
@Named
@SessionScoped
公共类LoginController实现Serializable {

@Inject
private transient Logger logger;
private String username;
私有字符串密码;

/ **
*创建一个新的LoginController实例
* /
public LoginController(){
System.out.println(che diamine );
}

// Getters and Setters
/ **
* @return username
* /
public String getUsername(){
返回用户名;
}

/ **
*
* @param username
* /
public void setUsername(String username){
this.username = username;
}

/ **
*
* @return密码
* /
public String getPassword(){
return密码;
}

/ **
*
* @param密码
* /
public void setPassword(String password){
this.password =密码;
}

/ **
*听取#{loginController.login}动作的按钮点击,
*验证用户输入的用户名和密码,导航到
*相应的页面。
*
* @param actionEvent
* /
public void login(ActionEvent actionEvent){
System.out.println(CONSOLE PRINT TEST);

FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request =(HttpServletRequest)context.getExternalContext()。getRequest();
try {
String navigateString =;
//检查用户名和密码是否有效,如果没有抛出ServletException
request.login(用户名,密码);
//获取用户原则并导航到适当的页面
Principal principal = request.getUserPrincipal();
if(request.isUserInRole(Administrator)){
navigateString =/ admin /AdminHome.xhtml;
} else if(request.isUserInRole(Manager)){
navigateString =/ manager / ManagerHome.xhtml;
} else if(request.isUserInRole(User)){
navigateString =/ user / UserHome.xhtml;
}
尝试{
logger.log(Level.INFO,用户({0})登录#+ DateUtility.getCurrentDateTime(),request.getUserPrincipal()。getName() );
context.getExternalContext()。redirect(request.getContextPath()+ navigateString);
} catch(IOException ex){
logger.log(Level.SEVERE,IOException,Login Controller+Username:+ principal.getName(),ex);
context.addMessage(null,new FacesMessage(Error!,Exception Ocured));
}
} catch(ServletException e){
logger.log(Level.SEVERE,e.toString());
context.addMessage(null,new FacesMessage(Error!,您提供的用户名或密码与我们的记录不符。));
}
}

/ **
*听取#{loginController.logout}动作
*的退出按钮点击并导航到登录界面。
* /
public void logout(){

HttpSession session =(HttpSession)FacesContext.getCurrentInstance()。getExternalContext()。getSession(false);
HttpServletRequest request =(HttpServletRequest)FacesContext.getCurrentInstance()。getExternalContext()。getRequest();
logger.log(Level.INFO,User({0})loging out#+ DateUtility.getCurrentDateTime(),request.getUserPrincipal()。getName());
if(session!= null){
session.invalidate();
}
FacesContext.getCurrentInstance()。getApplication()。getNavigationHandler()。handleNavigation(FacesContext.getCurrentInstance(),null,/ LOGin.xhtml?facess-redirect = true);
}
}

TomEE控制台



见这里



web.xml



请看这里



文件夹树

  $ tree 
。 $ b $b├──build$ b $b│└──class$ b $b│├──Controller$ b $b││└──LoginController.class$ b $b│├──实体
││├──地址.class $ b $b││├──BaseEntity.class$ b $b││├──OrRole.class $ b $b││└──User.class$ b $b│ ├──META-INF $ b $b││└──persistence.xml$ b $b│└──util$ b $b│└──DateUtility.class$ b $b├──src
│├──控制器$ b $b││└──LoginController.java$ b $b│├──实体$ b $b││├──地址.java $ b $b││├──BaseEntity.java $ b $b││├──Role.java$ b $b││└──User.java$ b $b│├──META-INF $ b $b││└──persistence.xml
│└──util$ b $b│└──DateUtility.java $ b $b└──WebContent$ b $b├──admin$ b $b│├──AdminHome.xhtml$ b $b│└──UserList.xhtml$ b $b├──ErrorAccessDenied.xhtml$ b $b├──Login.xhtml$ b $b├──经理$ b $b│└──ManagerHome.xhtml$ b $b├──META-INF $ b $b│└──MANIFEST.MF$ b $b├──资源$ b $b│├──css$ b $b││└──default.css$ b $b│└──primefaces-nz $ b $b│├──图片
││├──ui-bg_flat_30_cccccc_40x100.png $ b $b││├──ui-bg_flat_50_5c5c5c_40x100.png $ b $b││├──ui-bg_glass_40_ffc73d_1x400.png $ b $b││├──ui- bg_highlight-hard_20_16475f_1x100.png $ b $b││├──ui-bg_highlight-soft_33_1258bf_1x100.png $ b $b││├──ui-bg_highlight-soft_35_222222_1x100.png
│├──ui-bg_highlight-soft_44_444444_1x100.png $ b $b││├──ui-bg_highlight-soft_80_1442c8_1x100.png $ b $b││├──ui-bg_inset-hard_30_dedede_1x100.png $ b $b││├ ──ui-icons_222222_256x240.png $ b $b││├──ui-icons_292cd1_256x240.png $ b $b││├──ui-icons_a83300_256x240.png $ b $b││├──ui-icons_cccccc_256x240.png $ b $b││└──ui-icons_ffffff_256x240.png $ b $b│└──meas.css$ b $b├──模板$ b $b│├──sqown.xhtml$ b $b│└─ ─瓷砖$ b $b│└──LeftMenu.xhtml$ b $b├──用户$ b $b│└──UserHome.xhtml$ b $b└──WEB-INF $ b $b├──面-config.xml $ b $b├──lib$ b $b└──web.xml

24个目录,41个文件


解决方案

你的web.xml是什么?你在* .html或* .jsf或其他地方映射了jsf页面吗?



如果你希望使用CDI,你在WEB-INF中有一个beans.xml吗? / p>

Platform I am using:

  • Fedora 20;
  • mariadb-5.5.34-2.fc20.x86_64;
  • Eclipse Kepler Service Release from www.eclipse.org;
  • Apache TomEE plus 1.6.0.

I am implementing example

See here

and I am trying to manage to work the login interface. My problem is that the Java Bean of Login.xhtml has not been instantiated. How is it possible? I am quite familiar with java Beans, I checked for errors and the example comes from an expert Java programmer. I found out this trouble because I did not manage to work the login, and the debugger showed no variables. Eclipse: no shown variables in debugging Java EE

I attach the login.xhtml and the Java bean. I omit the GUI libraries because I think they are not part of the problem.

Login.xhtml

<ui:composition template="/templates/layout.xhtml"
     xmlns="http://www.w3.org/1999/xhtml"
     xmlns:f="http://java.sun.com/jsf/core"
     xmlns:h="http://java.sun.com/jsf/html"
     xmlns:ui="http://java.sun.com/jsf/facelets"
     xmlns:p="http://primefaces.org/ui"
>
     <ui:define name="content">
         <h:form styleClass="loginPanelStyle">
                 <p:growl id="msgs" showDetail="true" sticky="false" />                        
                <p:panelGrid columns="2">
                <f:facet name="header">
                    Login Panel
                </f:facet>
                <h:outputText value="Username : "></h:outputText>
                <p:inputText id="username" value="#{loginController.username}" required="true" requiredMessage="Please Enter Username!" message="fc">
                    <f:validateLength minimum="1" />  
                </p:inputText>
                <h:outputText value="Password : "></h:outputText>
                <p:password id="password" value="#{loginController.password}" required="true" requiredMessage="Please Enter password!">
                    <f:validateLength minimum="1" />  
                </p:password>
                <f:facet name="footer">
                <p:commandButton value="Submit" update="msgs" actionListener="#{loginController.login}" type="submit" icon="ui-icon-check" style="margin:0"></p:commandButton>
                </f:facet> 
            </p:panelGrid>
        </h:form>
     </ui:define>
</ui:composition>

LoginController.java

package controller;

import util.DateUtility;
import java.io.IOException;
import java.io.Serializable;
import java.security.Principal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Login Controller class allows only authenticated users to log in to the web
 * application.
 *
 * @author Emre Simtay <emre@simtay.com>
 */
@Named
@SessionScoped
public class LoginController implements Serializable {

    @Inject
    private transient Logger logger;
    private String username;
    private String password;

    /**
     * Creates a new instance of LoginController
     */
    public LoginController() {
        System.out.println("che diamine");
    }

    //  Getters and Setters
    /**
     * @return username
     */
    public String getUsername() {
        return username;
    }

    /**
     *
     * @param username
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     *
     * @return password
     */
    public String getPassword() {
        return password;
    }

    /**
     *
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Listen for button clicks on the #{loginController.login} action,
     * validates the username and password entered by the user and navigates to
     * the appropriate page.
     *
     * @param actionEvent
     */
    public void login(ActionEvent actionEvent) {
        System.out.println("CONSOLE PRINT TEST");

        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        try {
            String navigateString = "";
            // Checks if username and password are valid if not throws a ServletException
            request.login(username, password);
            // gets the user principle and navigates to the appropriate page
            Principal principal = request.getUserPrincipal();
            if (request.isUserInRole("Administrator")) {
                navigateString = "/admin/AdminHome.xhtml";
            } else if (request.isUserInRole("Manager")) {
                navigateString = "/manager/ManagerHome.xhtml";
            } else if (request.isUserInRole("User")) {
                navigateString = "/user/UserHome.xhtml";
            }
            try {
                logger.log(Level.INFO, "User ({0}) loging in #" + DateUtility.getCurrentDateTime(), request.getUserPrincipal().getName());
                context.getExternalContext().redirect(request.getContextPath() + navigateString);
            } catch (IOException ex) {
                logger.log(Level.SEVERE, "IOException, Login Controller" + "Username : " + principal.getName(), ex);
                context.addMessage(null, new FacesMessage("Error!", "Exception occured"));
            }
        } catch (ServletException e) {
            logger.log(Level.SEVERE, e.toString());
            context.addMessage(null, new FacesMessage("Error!", "The username or password you provided does not match our records."));
        }
    }

    /**
     * Listen for logout button clicks on the #{loginController.logout} action
     * and navigates to login screen.
     */
    public void logout() {

        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        logger.log(Level.INFO, "User ({0}) loging out #" + DateUtility.getCurrentDateTime(), request.getUserPrincipal().getName());
        if (session != null) {
            session.invalidate();
        }
        FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null, "/Login.xhtml?faces-redirect=true");
    }
}

TomEE console:

See here

web.xml

See here

folder tree:

$ tree
.
├── build
│   └── classes
│       ├── controller
│       │   └── LoginController.class
│       ├── entity
│       │   ├── Address.class
│       │   ├── BaseEntity.class
│       │   ├── Role.class
│       │   └── User.class
│       ├── META-INF
│       │   └── persistence.xml
│       └── util
│           └── DateUtility.class
├── src
│   ├── controller
│   │   └── LoginController.java
│   ├── entity
│   │   ├── Address.java
│   │   ├── BaseEntity.java
│   │   ├── Role.java
│   │   └── User.java
│   ├── META-INF
│   │   └── persistence.xml
│   └── util
│       └── DateUtility.java
└── WebContent
    ├── admin
    │   ├── AdminHome.xhtml
    │   └── UserList.xhtml
    ├── ErrorAccessDenied.xhtml
    ├── Login.xhtml
    ├── manager
    │   └── ManagerHome.xhtml
    ├── META-INF
    │   └── MANIFEST.MF
    ├── resources
    │   ├── css
    │   │   └── default.css
    │   └── primefaces-nz
    │       ├── images
    │       │   ├── ui-bg_flat_30_cccccc_40x100.png
    │       │   ├── ui-bg_flat_50_5c5c5c_40x100.png
    │       │   ├── ui-bg_glass_40_ffc73d_1x400.png
    │       │   ├── ui-bg_highlight-hard_20_16475f_1x100.png
    │       │   ├── ui-bg_highlight-soft_33_1258bf_1x100.png
    │       │   ├── ui-bg_highlight-soft_35_222222_1x100.png
    │       │   ├── ui-bg_highlight-soft_44_444444_1x100.png
    │       │   ├── ui-bg_highlight-soft_80_1442c8_1x100.png
    │       │   ├── ui-bg_inset-hard_30_dedede_1x100.png
    │       │   ├── ui-icons_222222_256x240.png
    │       │   ├── ui-icons_292cd1_256x240.png
    │       │   ├── ui-icons_a83300_256x240.png
    │       │   ├── ui-icons_cccccc_256x240.png
    │       │   └── ui-icons_ffffff_256x240.png
    │       └── theme.css
    ├── templates
    │   ├── layout.xhtml
    │   └── tiles
    │       └── LeftMenu.xhtml
    ├── user
    │   └── UserHome.xhtml
    └── WEB-INF
        ├── faces-config.xml
        ├── lib
        └── web.xml

24 directories, 41 files

解决方案

What's your web.xml? did you map jsf pages on *.html or *.jsf or other?

Do you have a beans.xml in WEB-INF if you expect to use CDI?

这篇关于Bean没有实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆