Managed Bean隐式路由无法正常工作?

问题描述 投票:0回答:1

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

我的托管bean定义如下:

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的persistance中定义的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>
java-ee managed-bean wildfly-9
1个回答
1
投票

我可能会用这个:

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

您的按钮实际应该做的是,表单提交一些数据(登录/密码),如果没有 - 重定向到其他页面,或者如果auth失败 - 到某个错误页面让我们说。所以commandButton是一个更好的选择。

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

您也可以尝试使用outcome属性来查看它是否有任何区别:

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

但在你的情况下commandButton是一个更好的选择。

© www.soinside.com 2019 - 2024. All rights reserved.