Eclipse 找不到与from view id'/xhtml';采取行动'#{user.login}';结果';管理员用户';

Eclipse 找不到与from view id'/xhtml';采取行动'#{user.login}';结果';管理员用户';,eclipse,jsf-2,tomcat7,javabeans,Eclipse,Jsf 2,Tomcat7,Javabeans,下面是我的web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLoca

下面是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    <display-name>Portal</display-name>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>cupertino</param-value>
    </context-param> 

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <welcome-file-list>
        <welcome-file>/faces/index.xhtml</welcome-file>
    </welcome-file-list>

    <!-- Map these files with JSF -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <error-page>
        <exception-type>javax.faces.application.ViewExpiredException</exception-type>
        <location>/faces/index.xhtml</location>
    </error-page>

</web-app>
用户Bean在下面-

package com.ravij.security;

import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpSession;

public class AuthorizationListener implements PhaseListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public void afterPhase(PhaseEvent event) {

        FacesContext facesContext = event.getFacesContext();
        String currentPage = facesContext.getViewRoot().getViewId();

        boolean isLoginPage = (currentPage.lastIndexOf("index.xhtml") > -1);
        HttpSession session = (HttpSession) facesContext.getExternalContext()
                .getSession(false);

        if (session == null) {
            NavigationHandler nh = facesContext.getApplication()
                    .getNavigationHandler();
            nh.handleNavigation(facesContext, null, "LOGIN_PAGE");
        }

        else {
            Object currentUser = session.getAttribute("username");

            if (!isLoginPage && (currentUser == null || currentUser == "")) {
                NavigationHandler nh = facesContext.getApplication()
                        .getNavigationHandler();
                nh.handleNavigation(facesContext, null, "LOGIN_PAGE");
            }
        }
    }

    @Override
    public void beforePhase(PhaseEvent event) {

    }

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;
    }
}
package com.ravij;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    private String username;
    private String password;
    private String email;
    private String isAdmin;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getIsAdmin() {
        return isAdmin;
    }

    public void setIsAdmin(String isAdmin) {
        this.isAdmin = isAdmin;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String logout() {
        // FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
        // "Thank you", "You are successfully Logged out!");
        // FacesContext.getCurrentInstance().addMessage(null, msg);
        // index.xhtml?faces-redirect=true
        FacesContext.getCurrentInstance().getExternalContext()
                .invalidateSession();
        return "LOGOUT";
    }

    private void resetAllFields() {
        setEmail("");
        setIsAdmin("");
        setPassword("");
        setUsername("");
    }

    public String login() {
    //userFromDB is fetched from DB. Hibernate is taking care of that.
        if (username != null && password != null && userFromDB != null
                && username.equals(userFromDB.getUsername())
                && hash.equals(userFromDB.getPassword())) {

            resetAllFields();

            FacesContext.getCurrentInstance().getExternalContext()
                    .getSessionMap().put("username", username);

            if (userFromDB.getIsAdmin().equals("true")) {
                return "ADMIN_USER";
            }
            return "NORMAL_USER";
        }

        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN,
                "Login Error", "Invalid credentials");
        FacesContext.getCurrentInstance().addMessage(null, msg);

        return "INVALID_USER";
    }

}
请尝试替换:

<navigation-rule>
    <from-view-id>/faces/index.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>ADMIN_USER</from-outcome>
        <to-view-id>/faces/admin.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

/faces/index.xhtml
管理员用户
/faces/admin.xhtml


/index.xhtml
管理员用户
/admin.xhtml
或者甚至移除线路

<from-view-id>/faces/index.xhtml</from-view-id>
/faces/index.xhtml

这很有帮助。。但是tomcat控制台向我显示了
警告:JSF1015:请求路径'/faces/admin.xhtml'以一个或多个FacesServlet前缀路径映射'/faces'开始。
但是,每当我试图使用
localhost:8080/Portal访问它时,tomcat也会抛出500个内部服务器错误
localhost:8080/Portal/faces/index.xhtml
工作正常。能否粘贴发生500次错误时发生的异常对于警告,请参阅我编辑的答案:remove/faces from以查看ID对于错误(500),我不确定,但请尝试更改web.xml中的欢迎文件,如下所示:/index.xhtml sorry,我在其间睡过觉。访问
http://localhost:8080/Portal
正在提供-
HTTP状态500-//在ExternalContext中找不到作为资源的faces/index.xhtml
。现在,按照您在上面的建议删除
面之后,web将以URL
http://localhost:8080/Portal/index.xhtml
<navigation-rule>
    <from-view-id>/index.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>ADMIN_USER</from-outcome>
        <to-view-id>/admin.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>
<from-view-id>/faces/index.xhtml</from-view-id>