JSF 不使用 navigation-case 重定向

JSF does not redirect with navigation-case

我正在尝试借助 faces-config.xml 文件中的 navigation-rule 进行重定向,但我遇到了问题,我没有被重定向到 [=单击登录按钮后从 index.xhtml 页面返回 50=] 页面。 login() 以及 validateUser() 正在 LoginBeanCDI bean 中被调用,结果我得到 seccuss 但没有发生重新定向。

我在从表单操作 <h:commandButton value="Login" id="combtn"action="#{loginBeanCDI.goToWelcome()}"/> 调用 LoginBeanCDI bean 后用注释 goToWelcome() 测试页面的功能,在这种情况下,我被重定向到 welcome.xhtml页面没有任何问题。因此我认为问题出在 faces-config.xml 文件中的导航规则。

index.xhtml

 <!DOCTYPE html>
<html xmlns="htpp://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core">


<ui:composition template="/WEB-INF/templates/template.xhtml">
    <ui:define name="css">
        <h:outputStylesheet library="css" name="mainStlye.css" />
    </ui:define>
    <ui:define name="content">
        <h:form id="login-form" prependId="false">
            <h:panelGrid columns="5">
                <h:outputLabel value="Username" for="username" />
                <h:inputText id="username" value="#{loginBeanCDI.user.username}" />
                <h:outputLabel value="Password" for="pwd" />
                <h:inputSecret id="pwd" value="#{loginBeanCDI.user.password}" />
                <h:commandButton value="Login" id="combtn"
                    action="#{loginBeanCDI.login()}" />
            </h:panelGrid>
        </h:form>
        <br />

    </ui:define>
</ui:composition>
</html>

强文本

<!DOCTYPE html>
<html xmlns="htpp://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">


<ui:composition template="/WEB-INF/templates/template.xhtml">

 <ui:define name="content"> 
    Welcome back #{loginBeanCDI.user.username} 
 </ui:define>

</ui:composition>
</html>

LoginBeanCDI

package com.beans;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.inject.Named;

import com.entities.User;


@Named(value = "loginBeanCDI")
@SessionScoped
public class LoginBeanCDI implements Serializable {
    private User user;

    public LoginBeanCDI() {

    }

    @PostConstruct
    public void initialize() {
        user = new User();

    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

//  public String goToWelcome() {
//      System.out.println("Inside goToWelcome(): " + user.getUsername() + ", " + user.getPassword());
//      return "/pages/welcome?faces-redirect=true";
//  }

    public String login() {
        System.out.println("login() was called");
        if(validateUser()){
            System.out.println("login() was called. success");
            return "success";
        }else{
            System.out.println("login() was called. fail");
            return "fail";
        }       
    }

    private boolean  validateUser(){
        System.out.println("validateUser() was called");
        return true;
    }
}

faces-config.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

    <application>
        <resource-bundle>
            <base-name>com.stack.bundle</base-name>
            <var>bundle</var>
        </resource-bundle>
    </application>

    <navigation-rule>
        <from-view-id>/index.xhtml</from-view-id>

        <navigation-case>
            <from-action>#{loginBeanCBI.login()}</from-action>
            <from-outcome>success</from-outcome>
            <to-view-id>/pages/welcome</to-view-id>
            <redirect />
        </navigation-case>

        <navigation-case>
            <from-action>#{loginBeanCBI.login()}</from-action>
            <from-outcome>fail</from-outcome>
            <to-view-id>/pages/fail</to-view-id>
            <redirect />
        </navigation-case>

    </navigation-rule>

</faces-config>

项目结构

您的导航盒中有错字。

改变

 <from-action>#{loginBeanCBI.login()}</from-action>

 <from-action>#{loginBeanCDI.login()}</from-action>