JSF不会与导航案例重定向

时间:2016-07-03 12:24:57

标签: jsf-2.2

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

我在从表单操作goToWelcome()调用后,在LoginBeanCDI bean中使用注释<h:commandButton value="Login" id="combtn"action="#{loginBeanCDI.goToWelcome()}"/>测试页面的funktioniliy,在这种情况下,我被重定向到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;
    }
}

面-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>

项目结构 enter image description here

1 个答案:

答案 0 :(得分:1)

导航案例中有拼写错误。

更改

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

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