【问题标题】:ManagedBean property not found找不到 ManagedBean 属性
【发布时间】:2012-07-26 17:48:03
【问题描述】:

在我的 webapp 中,当我点击登录链接时,Tomcat 网络服务器会抛出以下异常:

exception
javax.servlet.ServletException: /aluno_jsf.xhtml: Property 'logout' not found on type
br.com.aluno.controller.LoginMB

javax.faces.webapp.FacesServlet.service(FacesServlet.java:422)

root cause
javax.el.ELException: /aluno_jsf.xhtml: Property 'logout' not found on type
br.com.aluno.controller.LoginMB
[...]

这是我的@ManagedBean:

package br.com.aluno.controller;

import java.io.Serializable;
import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import br.com.aluno.dao.DaoFactory;
import br.com.aluno.entity.Usuario;
import br.com.aluno.util.JsfUtil;

@ManagedBean
public class LoginMB implements Serializable {
private static final long serialVersionUID = 6667329439219329074L;

private Usuario usuario;

@PostConstruct
public void init() {
    usuario = new Usuario();
}

public void logar() {
    try {
        Collection<Usuario> usuarios = DaoFactory
                .getDAOFactory(DaoFactory.JPA).getUsuarioDao()
                .validarLogin(usuario);
        if (usuarios != null && usuarios.size() == 1) {

            usuario = usuarios.iterator().next();

            HttpSession session = (HttpSession) FacesContext
                    .getCurrentInstance().getExternalContext()
                    .getSession(true);
            if (session != null) {
                session.setAttribute("usuario", usuario);
                session.setAttribute("usuarioTipo", usuario.getClass()
                        .getName());
            }
        } else {
            JsfUtil.addErrorMessage("Login ou senha invalida",
                    "Login ou senha invalida");
        }
    } catch (Exception e) {
        JsfUtil.addFatalMessage("Entre em contato com o administrador",
                e.getMessage());
    }
}

public String logout() {
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
            .getExternalContext().getSession(false);
    if (session != null) {
        session.invalidate();
    }
    return "/login?faces-redirect=true";
}

public Usuario getUsuario() {
    return usuario;
}

public void setUsuario(Usuario usuario) {
    this.usuario = usuario;
}
}

我在 index.html 中有一个链接,它重定向到以下 login.xhtml:

<ui:decorate xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui" template="/aluno_jsf.xhtml">

<ui:define name="conteudo">
    <h:form rendered="#{sessionScope.usuario == null}">
        <h3>Login</h3>
        <p:messages id="messages" showDetail="true" autoUpdate="true" />
        <h:panelGrid columns="2">
            <p:outputLabel for="login" value="Login:" />
            <p:inputText id="login" value="#{loginMB.usuario.login}" required="true" 
requiredMessage="Digite um login" />
            <p:outputLabel for="senha" value="Senha:" />
            <p:password id="senha" value="#{loginMB.usuario.senha}" required="true" 
requiredMessage="Digite uma senha" />
            <p:commandButton action="#{loginMB.logar}" value="Logar" ajax="false" />
        </h:panelGrid>
    </h:form>
</ui:define>

验证后,应呈现以下页面:

<!DOCTYPE html>
<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:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui" >
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>aluno_jsf</title>
</h:head>
<h:body>
<f:view>
    <h:form>
        <p:menubar
            rendered="#{sessionScope.usuarioTipo == 'br.com.aluno.entity.Administrador'}">
            <p:menuitem url="/administrador/cadastroCurso.xhtml"
value="Cadastro de curso" />
            <p:menuitem url="/administrador/cadastroDisciplina.xhtml" 
value="Cadastro de disciplinas" />
            <p:menuitem url="/administrador/cadastroAluno.xhtml" 
value="Cadastro de alunos" />
            <p:menuitem url="/administrador/atribuiNota.xhtml" 
value="Atribuicao de notas para alunos" />
            <p:menuitem action="#{loginMB.logout}" value="Logout" />
        </p:menubar>
        <p:menubar
rendered="#{sessionScope.usuarioTipo == 'br.com.aluno.entity.Aluno'}">
            <p:menuitem url="/aluno/nota.xhtml" value="Consulta de notas" />
            <p:menuitem url="/aluno/senha.xhtml" value="Alteracao de senha" />
            <p:menuitem action="#{loginMB.logout}" value="Logout" />
        </p:menubar>
    </h:form>
    <ui:insert name="conteudo">
    conteudo
    </ui:insert>
</f:view>
</h:body>
</html>

由于我正在调整这个网络应用程序 - 我之前已经有一些错误配置 - 有人知道现在发生了什么吗?

提前致谢。

正如你所问,她是新的例外:

message Could not initialize class br.com.fiap.aluno.util.JpaUtil

description The server encountered an internal error (Could not initialize class     br.com.fiap.aluno.util.JpaUtil) that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Could not initialize class br.com.fiap.aluno.util.JpaUtil
javax.faces.webapp.FacesServlet.service(FacesServlet.java:422)

root cause

java.lang.NoClassDefFoundError: Could not initialize class br.com.fiap.aluno.util.JpaUtil
br.com.fiap.aluno.dao_jpa.JpaUsuarioDao.validarLogin(JpaUsuarioDao.java:46)
br.com.fiap.aluno.controller.LoginMB.logar(LoginMB.java:30)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
javax.el.BeanELResolver.invoke(BeanELResolver.java:484)
javax.el.CompositeELResolver.invoke(CompositeELResolver.java:161)
org.apache.el.parser.AstValue.getValue(AstValue.java:159)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
com.sun.faces.facelets.el.ELText$ELTextVariable.toString(ELText.java:214)
com.sun.faces.facelets.compiler.AttributeInstruction.write(AttributeInstruction.java:89)
com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:302)
com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1756)
javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1756)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759)
  com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401)
com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:410)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.29 logs.

我会尝试修复它,但如果您能提供一些提示,我将不胜感激。

【问题讨论】:

  • 你评论中的stacktrace和上面的stacktrace一样吗?注释 stacktrace 提到了 javax/persistence/Persistence。
  • 实际上,正如 BalusC 在 cmets 中指出的那样,您的堆栈跟踪是一个不同的问题。你能在 StackOverflow 上发布一个新问题吗?

标签: jsf el


【解决方案1】:

javax.el.E​​LException: /aluno_jsf.xhtml: 在类型上找不到属性“注销” br.com.aluno.controller.LoginMB

这表明#{loginMB.logout} 被评估为ValueExpression 而不是MethodExpression

例如,如果您使用 &lt;h:outputText value="#{loginMB.logout}" /&gt;,或者即使由于缺少或不正确的 XML 命名空间声明或包含未包含组件的 JAR 文件而无法解析 &lt;x:someComponent action="#{loginMB.logout}" /&gt; 后面的 x 命名空间,也会发生这种情况包含在运行时类路径中。

到目前为止发布的代码中看不到原因,但我最好的猜测是您没有正确使用 Facelets 模板。该代码显示了&lt;ui:decorate&gt; 的奇怪用例。前往此答案以了解如何正确使用模板组合:How to include another XHTML in XHTML using JSF 2.0 Facelets?

【讨论】:

  • 我一定会检查这个链接。再次感谢。
【解决方案2】:

注销不是属性访问器,它是一种方法。

试试这个:#{loginMB.logout()} 代替

【讨论】:

  • 因为括号根本不是必需的,也没有任何区别。
  • 是的,确实如此,这不是解决问题的唯一方法,但它是一个正确的答案。
  • @exabrial,现在看起来还可以,但我遇到了另一个异常,我将尝试修复:异常 javax.servlet.ServletException: javax/persistence/Persistence javax.faces.webapp.FacesServlet.service (FacesServlet.java:422) 根本原因 java.lang.NoClassDefFoundError: javax/persistence/Persistence br.com.fiap.aluno.util.JpaUtil.(JpaUtil.java:12) br.com.fiap.aluno。 dao_jpa.JpaUsuarioDao.validarLogin(JpaUsuarioDao.java:46) br.com.fiap.aluno.controller.LoginMB.logar(LoginMB.java:30) [...] 谢谢。
  • @BalusC 我一直使用括号调用来调用具有该签名的方法。我很好奇为什么你认为这不会有什么不同。
  • 顺便说一句,我已经使用了“无括号”方法并且它有效。我想知道为什么现在会发生这种情况......
【解决方案3】:

我遇到了同样的问题,我通过删除添加到我的 xhtml 文件中的 cmets 解决了这个问题

page找到解决方案

【讨论】:

    【解决方案4】:

    我想提供一些您可能在 xhtml 中的 EL 方法引用中获得 javax.el.ELException 的原因。将此视为参考问题,也许其他人会继续贡献。

    Java EE 版本不匹配

    • 在您的 AppServer 上运行的 Java EE 版本。这应该是您的 AppServer 文档的一部分,或者如果您对其进行了自定义(例如在 tomcat 之上构建自己的),您应该知道。
    • 使用的标签库,可以通过&lt;html&gt;标签中使用的xmlns来识别。

    例如,如果您使用的是 Java EE 6,那么这是错误的:

    <html 
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    

    应该改为

    <html 
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    

    注意使用 sun.com 而不是 jcp。对于 Java EE 7 以上,您应该使用 jcp。

    请注意,persistence.xml 中的 &lt;persistence&gt; 和 beans.xml 中的 &lt;beans&gt; 存在类似问题!

    不支持 MethodExpression 的标签属性

    研究标签库的文档,了解您正在使用的确切版本。您将有几种可能性:

    1. 标签属性未包含在规范中。这可能是因为它仅在标签库的更高版本中添加。在这种情况下,代码将假定它是一个常规属性,并将在渲染阶段作为常规属性进行评估(寻找一个 getter)。这也是Java EE版本不匹配失败的原因。
    2. 标记属性已记录在案,但未指定它“必须评估为javax.el.MethodExpression”。在这种情况下,它期望提供的属性是不同的类型并尝试对其进行评估。如果提供了 Method,则它必须使用 () 语法,并且该方法将仅在检索预期类型时执行。
    3. tag 属性已记录在案,但没有记录为 javax.el.ValueExpression 类型,在这种情况下,它甚至不接受 EL 语法中的表达式,并且评估将立即发生并且仅在呈现期间进行。

    对于这个特殊的问题,我们可以参考PrimeFaces 3.4 Tag Documentation。遗憾的是没有提供使用的确切版本。

    EL 实现不支持方法引用

    在这种情况下,没有() 的方法引用将默认解释为常规属性,并尝试访问getter/setter。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 2018-05-06
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多