gpt4 book ai didi

jsf - 记录器,从@Inject 转换为生产者

转载 作者:行者123 更新时间:2023-12-02 15:34:00 26 4
gpt4 key购买 nike

我在实现登录界面时遇到了问题,例如 Simple CRUD Web Application with JSF 2.1, PrimeFaces 3.5, EJB 3.1, JPA (ORM) / EclipseLink, JAAS , MySQL在 TomEE 的邮件列表中,有人告诉我我正在使用的 LoginController.java 试图注入(inject) Logger,但 Logger 注入(inject)不是由 CDI 管理的。有人告诉我改用制作人。不知道是什么,我在网上搜索了一下,发现this example但我仍然对此不太满意,所以请解释我必须修改哪些内容才能为记录器实现生产者。

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("test");
}

// Getters and Setters
/**
* @return username
*/
public String getUsername() {
return username;
}

/**
*
* @param username
*/
public void setUsername(String username) {
this.username = username;
}

/**
*
* @return password
*/
public String getPassword() {
return password;
}

/**
*
* @param password
*/
public void setPassword(String password) {
this.password = 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("CONSOLE PRINT TEST");

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"));
}
} 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."));
}
}

/**
* 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");
}
}

最佳答案

当您使用@Inject 时,CDI 会尝试为您创建请求类型的实例。最简单的解决方案:它调用默认构造函数并返回该实例。

java.util.logging.Logger 的问题:它没有可见的默认构造函数。因此,您必须通过将 Producer 添加到您的类路径来告诉 CDI 如何满足依赖关系。尽管使用@JohnAment 建议的焊接记录器也是我的首选解决方案,但考虑到您目前的知识水平,如果您首先添加自己的生产商,它可能最适合您。

所以在你的 Controller 旁边,创建一个新类(添加包,导入,......你自己)

public class LoggerProducer {
@Produces
public Logger getLogger(InjectionPoint p) {
return Logger.getLogger(p.getClass().getCanonicalName());
}
}

这会告诉 CDI 容器:每当您需要注入(inject) java.util.logging.Logger 时,使用此方法通过获取需要该记录器引用的类的 fqn 名称来创建一个.

这应该可以解决您的问题。一旦您有了想法,请考虑您是否真的想要/需要使用 java.util.logging,或者您是否想切换到 slf4j。在这种情况下,修改您的 Controller 导入,删除您刚刚编写的 LoggerProducer,并将 weld-logger jar 导入到您的部署中。

关于jsf - 记录器,从@Inject 转换为生产者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21076836/

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