2016-07-03 5 views
0

Ich versuche, die Umleitung mit Hilfe der navigation-rule in der faces-config.xml-Datei zu tun, aber ich habe das Problem, dass ich nicht auf die Welcome.xhtml-Seite von der weitergeleitet werden index.xhtml Seite nach dem Klicken auf die Login-Schaltfläche. Die login() sowie werden in der LoginBeanCDI Bean aufgerufen und ich bekomme seccuss als Ergebnis zurück, aber die Neuerung passiert nicht.JSF leitet nicht mit Navigation-Case

Ich teste die Funktionalität der Seiten mit kommentierten goToWelcome() in der LoginBeanCDI Bean nach dem Aufruf von der Formularaktion <h:commandButton value="Login" id="combtn"action="#{loginBeanCDI.goToWelcome()}"/> und in diesem Fall werde ich ohne Probleme auf die Welcome.xhtml Seite umgeleitet. Daher denke ich, dass das Problem mit der Navigationsregel in der faces-config.xml-Datei ist.

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> 

starken Text

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

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

Projektstruktur enter image description here

Antwort

1

Sie haben einen Tippfehler in Ihrem Navigations Fall.

ändern

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

Um

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