托管 Bean 隐式路由不起作用?

Managed Bean implicit routing not working?

我在从托管 bean 隐式导航时遇到问题,该页面是一个包含用户名和密码字段的登录页面,用于检查 phpmyadmin 数据库;

My managed bean is defined like so:

package backManagedBean;

import java.io.Serializable;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

import com.esprit.entities.Users;
import com.esprit.service.AuthenticationService;


@SuppressWarnings({ "serial", "unused" })
@RequestScoped
@ManagedBean(name="loginManagedBean", eager=true )
public class loginManagedBean implements Serializable {

    /**
     * 
     */

    private static Users connectedUser;
    private String emailField;
    private String passwordField;
    @EJB
    AuthenticationService authService;


    public String getEmailField() {
        return emailField;
    }
    public void setEmailField(String emailField) {
        this.emailField = emailField;
    }
    public String getPassworField() {
        return passwordField;
    }
    public void setPassworField(String passwordField) {
        this.passwordField = passwordField;
    }
    public AuthenticationService getAuthService() {
        return authService;
    }
    public void setAuthService(AuthenticationService authService) {
        this.authService = authService;
    }

    public String login(){
        //connectedUser = authService.authenticate(emailField, passwordField);
        return "/Back/index?faces-redirect=true";
    }



}

页面刷新和查询正常执行,您可以看到在 hibernate.show_sql 的持久性中定义的 wildfly 服务器日志。然而返回登录页面,因为 login() 函数没有返回任何内容。

login.xhtml :

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
  <ui:composition template="../template/facesTemplate.xhtml">
    <ui:define name="head">
        <title>Welcome To Petroca</title>

    </ui:define>


    <ui:define name="body">
    <div class="accountbg">
        <div class="content-center">
            <div class="content-desc-center">
                <div class="container">
                    <div class="row justify-content-center">
                        <div class="col-lg-5 col-md-8">
                            <div class="card">
                                <div class="card-body">
                                    <h3 class="text-center mt-0 m-b-15"><a href="index.html"
                                            class="logo logo-admin"><img src="../assets/images/logo-dark.png" height="30"
                                                alt="logo" /></a></h3>
                                    <h4 class="text-muted text-center font-18"><b>Sign In</b></h4>
                                    <div class="p-2">
                                        <h:form class="form-horizontal m-t-20" >
                                            <div class="form-group row">
                                                <div class="col-12"><h:inputText class="form-control" type="text" required="" value="#{loginManagedBean.emailField}" placeholder="Username" /></div>
                                            </div>
                                            <div class="form-group row">
                                                <div class="col-12">
                                                <h:inputSecret class="form-control" required="" value="#{loginManagedBean.passworField}" placeholder="Password" /></div>
                                            </div>
                                            <div class="form-group text-center row m-t-20">
                                                <div class="col-12">
                                                    <h:button class="btn btn-primary btn-block waves-effect waves-light" action="#{loginManagedBean.login()}">
                                                        Log In
                                                    </h:button>
                                                </div>
                                            </div>
                                            <div class="form-group m-t-10 mb-0 row">
                                                <div class="col-sm-7 m-t-20"><a href="passwordRecovery.jsf"
                                                        class="text-muted"><i class="mdi mdi-lock"></i> Forgot your
                                                        password?</a></div>
                                            </div>
                                        </h:form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div><!-- end row -->
                </div>
            </div>
        </div>
    </div>
    </ui:define>

  </ui:composition> 
</html>

我可能会用这个:

<h:commandButton ... action="#{loginManagedBean.login}" />

你的按钮实际上应该做什么,是提交一些数据的表单(login/pwd),如果没问题 - 重定向到其他页面,或者如果 auth 失败 - 比如说错误页面。因此 commandButton 是更好的选择。

<h:commandButton> 生成一个 HTML like <input type="submit"> 按钮,默认情况下使用 POST 方法提交父 <h:form> 并调用附加到操作的操作或 actionListener.

您也可以尝试使用 outcome 属性 看看是否有任何不同:

<h:button ... outcome="#{loginManagedBean.login()}" />

但在您的情况下,commandButton 是更好的选择。