在JSF中的导航

3

早上好!我是一个初学者。我有一个简单的JSF应用程序,但它并没有按照预期工作。 这是我的login.xhtml:

<h:body>
    <h:outputText value="Please enter your login and password"/>
    <h:form id="loginForm">
        <h:panelGrid columns="2">       
                <h:outputText value="User name"/>                   
                <h:inputText value="#{textBean.login}" required="true"/>
                <h:outputText value="Password"/>
                <h:inputSecret value="#{textBean.password}" required="true"/>
                <h:commandButton value="Submit" action="#{textBean.doLogin}"/>
        </h:panelGrid> 
    </h:form>
</h:body>

这是我的error.xhtml文件:
<h:body>
    <h:outputText value="You have entered incorrect data"/>
    <h:form id="errorForm">    
                <h:commandLink value="Back to login page" action="#{TextBean.backToLogin}"/>
    </h:form>
</h:body>

这是我的Java Bean类TextBean.java: public class TextBean实现了Serializable接口 {
private static final long serialVersionUID = 1L;

private String login;

private String password;

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;
}

public String doLogin() {
    if ("admin".equals(login) && "mypass".equals(password)) {
        return "welcome";
    } else {
        return "error";
    }

}

public String backToLogin() {
        return "login";
}

为什么错误页面不能返回登录页面?当您点击“返回登录页面”链接时,会出现异常。我做错了什么? 这是我的异常:

        The server encountered an internal error () that prevented it from fulfilling this request.
javax.servlet.ServletException: javax.el.PropertyNotFoundException: /error.xhtml @14,80 action="#{TextBean.doMyLogin}": Target Unreachable, identifier 'TextBean' resolved to null
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:521)
javax.faces.el.EvaluationException: javax.el.PropertyNotFoundException: /error.xhtml @14,80 action="#{TextBean.doMyLogin}": Target Unreachable, identifier 'TextBean' resolved to null
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:95)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:787)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1252)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:508)
javax.el.PropertyNotFoundException: /error.xhtml @14,80 action="#{TextBean.doMyLogin}": Target Unreachable, identifier 'TextBean' resolved to null
    com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:107)
        javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:787)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1252)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:508)

亲爱的朋友们,请帮帮我!任何帮助都将不胜感激。

该内容与IT技术无关。

2
除了具体的问题,为什么不使用<h:message>在同一表单中显示错误消息呢?应尽量避免在POST请求中进行前向导航。您可以在这篇文章中找到一些指针:http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html - BalusC
@BalusC - 不错的文章 :) - Petar Minchev
1个回答

6

你的bean的名称是textBean而不是TextBean。这就是为什么JSF不能解析它的原因:标识符'TextBean'解析为空


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接