我无法像示例Simple CRUD Web Application with JSF 2.1, PrimeFaces 3.5, EJB 3.1, JPA (ORM) / EclipseLink, JAAS , MySQL那样实现Login界面 在TomEE的邮件列表中,我被告知我正在使用的LoginController.java尝试注入Logger,但Logger注入不是由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");
}
}
答案 0 :(得分:4)
当您使用@Inject
时,CDI会尝试为您创建所请求类型的实例。最简单的解决方案:调用默认构造函数并返回该实例。
java.util.logging.Logger
的问题:它没有可见的默认构造函数。因此,您必须通过向类路径添加Producer来告诉CDI如何满足依赖性。虽然建议使用焊接记录器@JohnAment也是我的首选解决方案,但如果您首先添加自己的生产商,那么鉴于您目前的知识状态可能最适合您。
在控制器旁边,创建一个新类(添加包,导入,......自己)
public class LoggerProducer {
@Produces
public Logger getLogger(InjectionPoint p) {
return Logger.getLogger(p.getClass().getCanonicalName());
}
}
这告诉CDI容器:每当需要注入java.util.logging.Logger
时,使用此方法通过获取需要该记录器引用的类的fqn名称来创建一个。
这应该可以解决您的问题。一旦你明白了,想想你是否真的想要/需要使用java.util.logging,或者如果你想切换到 slf4j 。在这种情况下,修改控制器导入,删除刚编写的LoggerProducer,然后将 weld-logger jar导入到部署中。
答案 1 :(得分:1)
它们是正确的,没有可用的标准注射器。您可以从Weld看一下如何注入客户记录器的示例。请随意修改它以使用java.util.logging。