Ger*_*llo 1 jsf jaas cdi apache-tomee jakarta-ee
我有实施类似例如登录接口故障的简单与JSF 2.1 CRUD Web应用程序,PrimeFaces 3.5,EJB 3.1,JPA(ORM)/ EclipseLink的,JAAS中,MySQL 在TomEE的邮件列表,我被告知,我LoginController.java我正在尝试注入 Logger,但 Logger 注入不受 CDI 管理。有人告诉我改用生产者。不知道它是什么,我在互联网上搜索并找到了这个示例 但是我仍然不满意它,所以请解释我必须修改什么才能为记录器实现生产者。
登录控制器.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");
}
}
Run Code Online (Sandbox Code Playgroud)
当您使用 时@Inject,CDI 会尝试为您创建所请求类型的实例。最简单的解决方案:它调用默认构造函数并返回该实例。
问题java.util.logging.Logger:它没有可见的默认构造函数。因此,您必须通过向类路径添加 Producer 来告诉 CDI 如何满足依赖关系。尽管使用 @JohnAment 建议的焊接记录器也是我的首选解决方案,但考虑到您目前的知识状态,如果您从添加自己的生产者开始,它可能最适合您。
所以在你的控制器旁边,创建一个新的类(添加包,导入,......你自己)
public class LoggerProducer {
@Produces
public Logger getLogger(InjectionPoint p) {
return Logger.getLogger(p.getClass().getCanonicalName());
}
}
Run Code Online (Sandbox Code Playgroud)
这告诉 CDI 容器:每当您需要注入 时java.util.logging.Logger,请使用此方法通过获取需要该记录器引用的类的 fqn 名称来创建一个。
这应该可以解决您的问题。一旦你有了这个想法,想想你是否真的想要/需要使用 java.util.logging 或者你是否想要切换到slf4j。在这种情况下,修改您的控制器导入,删除您刚刚编写的 LoggerProducer 并将焊接记录器jar导入您的部署。
| 归档时间: |
|
| 查看次数: |
3888 次 |
| 最近记录: |