【问题标题】:Bean not instantiatedBean 未实例化
【发布时间】:2014-01-27 20:55:01
【问题描述】:

我正在使用的平台

  • Fedora 20;
  • mariadb-5.5.34-2.fc20.x86_64;
  • 来自 www.eclipse.org 的 Eclipse Kepler 服务版本;
  • Apache TomEE plus 1.6.0。

我正在实现示例

See here

我正在尝试设法使用登录界面。我的问题是 Login.xhtml 的 Java Bean 没有被实例化。这怎么可能?我对 Java Beans 非常熟悉,我检查了错误,示例来自专家 Java 程序员。 我发现这个麻烦是因为我没有设法登录,并且调试器没有显示任何变量。 Eclipse: no shown variables in debugging 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="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 控制台

See here

web.xml

See here

文件夹树

$ 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

【问题讨论】:

  • 查看FacesServlet 类,它被击中了吗?如果没有,您可能没有正确配置 JSF。
  • Jsf 通常在给定的 jar 中包含其源代码。只需搜索类型(正如您提到的,您使用 Eclipse,它是 ctrl+shift+t)并在其service 方法中放置一个断点。这样你就可以看到服务器是否被击中了。
  • 我发现 FacesServlet.class 在里面我得到 javax.faces.webapp - /home/caterpillar/apache-tomee-plus/myfaces-api.jar/javax.faces.webapp/ 但显然它包含只有字节码。确实尝试编辑它返回:>
  • 浏览到 Mojarra 的 nexus repo 和 here 你有带有源代码的 jars。只需下载它们并将它们添加到您的项目中点击“附加源”

标签: jsf tomcat jakarta-ee javabeans apache-tomee


【解决方案1】:

你的 web.xml 是什么?您是否将 jsf 页面映射到 *.html 或 *.jsf 或其他?

如果您希望使用 CDI,您是否在 WEB-INF 中有 beans.xml?

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
  • 1970-01-01
相关资源
最近更新 更多