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

时间:2019-04-28 13:21:26

标签: java-ee managed-bean wildfly-9

我无法从托管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定义的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>

1 个答案:

答案 0 :(得分:1)

我可能会用到这个:

commandButton

您的按钮实际上应该做的是提交带有一些数据的表单(登录/密码),并且在可以的情况下-重定向到其他页面,或者如果auth失败-到某个错误页面。因此,<h:commandButton>是实现此目的的更好选择。

<input type="submit">生成一个类似<h:form>的HTML按钮,该按钮默认使用POST方法提交父outcome,并调用附加到action或actionListener的动作。

您还可以尝试使用<h:button ... outcome="#{loginManagedBean.login()}" /> 属性来查看它是否有所不同:

{{1}}

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