faces-config.xml 파일에서 navigation-rule
의 도움을 받아 리디렉션을 시도하고 있지만 문제가 발생했습니다. welcome.xhtml 페이지로 리디렉션되지 않았습니다. index.xhtml 페이지로 이동하십시오. login()
과 validateUser()
은 LoginBeanCDI
빈에서 호출되며 결과적으로 seccuss
이 반환되지만 재 지정은 발생하지 않습니다.JSF가 내비게이션 케이스와 함께 리디렉션하지 않습니다.
폼 동작 <h:commandButton value="Login" id="combtn"action="#{loginBeanCDI.goToWelcome()}"/>
에서 호출 한 후 LoginBeanCDI 빈의 주석이있는 페이지의 기능을 테스트합니다.이 경우에는 아무런 문제없이 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>