我在从托管 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";
}
}
页面刷新和查询正常执行,您可以看到 wildfly 服务器日志在持久性中定义hibernate.show_sql。然而,它会返回登录页面,因为 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(的表单,如果没问题 - 重定向到其他页面,或者如果身份验证失败 - 比方说到某个错误页面。因此,commandButton
是为此目的的更好选择。
<h:commandButton>
生成一个类似 HTML <input type="submit">
按钮,该按钮默认使用 POST 方法提交父<h:form>
,并调用附加到操作或操作监听器的操作。
您也可以尝试使用outcome
属性来查看它是否有任何区别:
<h:button ... outcome="#{loginManagedBean.login()}" />
但在你的情况下,命令按钮是一个更好的选择。