我有以下背景:
xhtml页面,如下所示:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>My Event</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</h:head>
<body>
<f:event listener="#{filterEventCreator.check}" type="preRenderView" />
...
这是filterEventCreator:
public void check(ComponentSystemEvent event) {
if(amIComingFromDispatcher()){
SYSO_Testing.syso("FilterEventCreator in this case, everything it's ok");
sessionUtility.setNotComingFromDispatcher();
}
else{
try {
SYSO_Testing.syso("I'm not coming from redirect");
sessionUtility.sessionLogout();
response.sendRedirect(errorPath);
}
catch (IOException ex) {
Logger.getLogger(FilterEventCreator.class.getName()).log(Level.SEVERE, null, ex);
}
}
private boolean amIComingFromDispatcher() {
if(this.loggedUser==null)return false;
return sessionUtility.getComingFromDispatcher();
}
其中sessionUtility是以下内容:
@Named
@SessionScoped
public class SessionUtility implements Serializable {
private String loggedUserName, loggedUserPassword;
private int errorCode, eventID;
private boolean error, comingFromDispatcher;
@PostConstruct
public void init() {
error = false;
comingFromDispatcher = false;
}
public void addUser(String username) {
loggedUserName = username;
SYSO_Testing.syso("User added in cache");
}
public String getLoggedUser() {
return loggedUserName;
}
public void sessionLogout() {
SYSO_Testing.syso("starting logout from Session bean");
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
loggedUserName = null;
error = false;
request.getSession().invalidate();
}
public void setError(int value) {
errorCode = value;
error = true;
}
public boolean isAnError() {
return error;
}
public int getError() {
return errorCode;
}
public void showedError() {
error = false;
}
public void setNotComingFromDispatcher() {
comingFromDispatcher = false;
}
public void setComingFromDispatcher() {
comingFromDispatcher = true;
}
public boolean getComingFromDispatcher() {
return comingFromDispatcher;
}
如果我直接连接到xhtml页面,过滤器会理解我既没有记录也没有重定向但是当它尝试重定向时出现以下错误: 错误渲染视图[/Error.xhtml] java.lang.NullPointerException
我调试了这个项目,除了loggedUser之外,响应和任何变量都不为null。我很感激任何可以帮助我的人。