gpt4 book ai didi

jsf - SimpleCRUD 登录失败

转载 作者:行者123 更新时间:2023-12-04 18:24:10 26 4
gpt4 key购买 nike

我正在使用的平台:

  • 软呢帽 20;
  • mariadb-5.5.34-2.fc20.x86_64;
  • 来自 www.eclipse.org 的 Eclipse Kepler 服务版本;
  • Apache TomEE 加 1.6.0。

我正在实现示例

See here

我正在尝试管理登录界面。我的问题是 login.xhtml 网页正确获取了用户名和密码,但是方法

public void login(ActionEvent actionEvent) 

bean LoginController.java 无法正确响应网页登录按钮。特别是,该网页没有说明用户名和密码的正确性。要查看它会发生什么,请参阅前面链接中的交互式示例。 Here相反,我的计算机上发生的事情的简短视频。

为了找出发生了什么,我在 Java bean 中放置了一些控制台输出,我发现 getter/setter 方法正在工作,唯一不起作用的方法是

public void login(ActionEvent actionEvent) 

确实是打印

System.out.println("Entering in public void login(ActionEvent actionEvent)");

不会出现在控制台中。

登录.xml

<ui:composition template="/templates/layout.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
>
<ui:define name="content">
<h:form styleClass="loginPanelStyle">
<p:growl id="msgs" showDetail="true" sticky="false" />
<p:panelGrid columns="2">
<f:facet name="header">
Login Panel
</f:facet>
<h:outputText value="Username : "></h:outputText>
<p:inputText id="username" value="#{loginController.username}" required="true" requiredMessage="Please Enter Username!" message="fc">
<f:validateLength minimum="1" />
</p:inputText>
<h:outputText value="Password : "></h:outputText>
<p:password id="password" value="#{loginController.password}" required="true" requiredMessage="Please Enter password!">
<f:validateLength minimum="1" />
</p:password>
<f:facet name="footer">
<p:commandButton value="Submit" update="msgs" action="#{loginController.login}"
icon="ui-icon-check" style="margin:0" />
</f:facet>
</p:panelGrid>
</h:form>
</ui:define>
</ui:composition>

我也试过更换

<p:commandButton value="Submit" update="msgs" action="#{loginController.login}" icon="ui-icon-check" style="margin:0" />

<p:commandButton value="Submit" update="msgs" action="#{loginController.login}" icon="ui-icon-check" style="margin:0" />

但结果是一样的。

LoginController.java

package controller;

import util.DateUtility;
import java.io.IOException;
import java.io.Serializable;
import java.security.Principal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
* Login Controller class allows only authenticated users to log in to the web
* application.
*
* @author Emre Simtay <emre@simtay.com>
*/
@Named
@SessionScoped
public class LoginController implements Serializable {

@Inject
private transient Logger logger;
private String username;
private String password;

/**
* Creates a new instance of LoginController
*/
public LoginController() {
System.out.println("LoginController instantiated");
}

// Getters and Setters
/**
* @return username
*/
public String getUsername() {
System.out.println("getUsername: " + username);
return username;
}

/**
*
* @param username
*/
public void setUsername(String username) {
this.username = username;
System.out.println("setUsername sets username: " + this.username);
}

/**
*
* @return password
*/
public String getPassword() {
System.out.println("getPassword is: " + password);
return password;
}

/**
*
* @param password
*/
public void setPassword(String password) {
this.password = password;
System.out.println("setPassword sets password: " + this.password);
}

/**
* Listen for button clicks on the #{loginController.login} action,
* validates the username and password entered by the user and navigates to
* the appropriate page.
*
* @param actionEvent
*/
public void login(ActionEvent actionEvent) {
System.out.println("Entering in public void login(ActionEvent actionEvent)");
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
try {
String navigateString = "";
// Checks if username and password are valid if not throws a ServletException
request.login(username, password);
// gets the user principle and navigates to the appropriate page
Principal principal = request.getUserPrincipal();
if (request.isUserInRole("Administrator")) {
navigateString = "/admin/AdminHome.xhtml";
} else if (request.isUserInRole("Manager")) {
navigateString = "/manager/ManagerHome.xhtml";
} else if (request.isUserInRole("User")) {
navigateString = "/user/UserHome.xhtml";
}
try {
logger.log(Level.INFO, "User ({0}) loging in #" + DateUtility.getCurrentDateTime(), request.getUserPrincipal().getName());
context.getExternalContext().redirect(request.getContextPath() + navigateString);
} catch (IOException ex) {
logger.log(Level.SEVERE, "IOException, Login Controller" + "Username : " + principal.getName(), ex);
context.addMessage(null, new FacesMessage("Error!", "Exception occured"));
System.out.println("(DEBUG) CONSOLE OUTPUT: Error!, Exception occured");
}
} catch (ServletException e) {
logger.log(Level.SEVERE, e.toString());
context.addMessage(null, new FacesMessage("Error!", "The username or password you provided does not match our records."));
System.out.println("(DEBUG) CONSOLE OUTPUT: Error!, The username or password you provided does not match our records.");
}
}

/**
* Listen for logout button clicks on the #{loginController.logout} action
* and navigates to login screen.
*/
public void logout() {

HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
logger.log(Level.INFO, "User ({0}) loging out #" + DateUtility.getCurrentDateTime(), request.getUserPrincipal().getName());
if (session != null) {
session.invalidate();
}
FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null, "/Login.xhtml?faces-redirect=true");
}
}

persistence.xml

Click here

web.xml

Click here

项目来源:

您可以找到整个“登录界面”项目源HERE

TomEE 配置

TomEE 正在使用目录:

  1. TomEE 主目录:apache-tomee-plus-1.6.0-JAAS
  2. Eclipse 的 Servers 工作区目录:/workspace/Server/TomEE-JAAS-config/

apache-tomee-plus-1.6.0-JAAS/config/(1) 里面有什么:

.
├── catalina.policy
├── catalina.properties
├── context.xml
├── groups.properties
├── logging.properties
├── login.config
├── server.xml
├── server.xml.original
├── system.properties
├── tomcat-users.xml
├── tomcat-users.xml.original
├── tomee.xml
├── users.properties
└── web.xml

0 directories, 14 files

里面是什么(2):

.
├── catalina.policy
├── catalina.properties
├── context.xml
├── server.xml
├── tomcat-users.xml
└── web.xml

我上传了两个 conf 目录 HERE所以你可以咨询他们。

为了将 TomEE 配置为与 JAAS 一起运行,我询问了 following question :于是我就这样在Eclipse中配置了TomEE

TomEE 控制台:

Click here

最佳答案

将 commandButton 标签的属性更改为 actionListener 而不是 action 应该可以解决问题。

关于jsf - SimpleCRUD 登录失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21137491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com