gpt4 book ai didi

JSF2.0 注释@ManagedBean 不起作用

转载 作者:行者123 更新时间:2023-12-03 18:29:20 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable

(18 个回答)


5年前关闭。




我尝试编写我的第一个 JSF2.0 项目(使用 EJB3.1)。我不明白为什么我的 @ManagedBean 注释不起作用。

在 Glassfish v3 上运行应用程序时,总是出现错误

exception

javax.servlet.ServletException: /login.xhtml @34,133 value="#{loginBean.login}": Target Unreachable, identifier 'loginBean' resolved to null

root cause

javax.el.PropertyNotFoundException: /login.xhtml @34,133 value="#{loginBean.login}": Target Unreachable, identifier 'loginBean' resolved to null



如果我在 faces-config.xml 中定义了一个托管 bean - 它会起作用。但我想使用注释。

可能是我在我的 poms 中使用了错误的库?

managedbean 示例(它将是一个传输对象):
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(我可以尝试使用它):
<!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>

用户 Controller 类:
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";
}
}

我真的不明白为什么它不起作用:(我做错了什么?

UPD:堆栈跟踪: http://pastebin.com/istJmMHr

源代码可以从我的谷歌驱动器下载: https://docs.google.com/file/d/0B4Am7SXJwmtKNVc0LVhWVlEyMVk/view

最佳答案

我认为你可以更好地从一个非常小的例子开始来了解事物。您的代码中有很多地方不太正确。

首先,@Stateless bean 不能是 View 范围的。对此稍加思考。拥有一个无状态 View 范围的 bean 实际上意味着什么?为什么你认为你首先需要一个?

一个 View 应该有一个支持 bean,而这个通常是 View 范围的。该 View 可能需要的任何 DTO 都不应该是 View 范围的,而应该只是主支持 bean 的实例变量。这样他们就会自动依赖于那个范围。

在您的情况下,使 loginBean 成为一个实例变量,就像用户变量一样。

关于JSF2.0 注释@ManagedBean 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15365118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com