我有一个 SessionScoped bean 来管理用户身份验证。如果用户通过身份验证,主页会显示一些附加文本
<h:outputText value="welcome " rendered="#{loginBean.isLoggedIn}" />
当页面加载时,我可以看到@PostConstruct在START PHASE RENDER_RESPONSE 6之后被调用(如预期的那样)
当用户单击登录时,我加载了一个表单,我可以看到在用户提交登录时调用了登录方法(身份验证也有效)。此后,用户被重定向回原始页面。在START PHASE RENDER_RESPONSE 6之后,再次调用 loginBean的@PostConstruct !
INFO: START PHASE RESTORE_VIEW 1
INFO: END PHASE RESTORE_VIEW 1
INFO: START PHASE RENDER_RESPONSE 6
INFO: Constructor called 1 times !
INFO: In constructor FROM is greeting.xhtml
INFO: Get is logged in called and value is false
INFO: Get is logged in called and value is false
INFO: END PHASE RENDER_RESPONSE 6
INFO: START PHASE RESTORE_VIEW 1
INFO: END PHASE RESTORE_VIEW 1
INFO: START PHASE APPLY_REQUEST_VALUES 2
INFO: END PHASE APPLY_REQUEST_VALUES 2
INFO: START PHASE PROCESS_VALIDATIONS 3
INFO: END PHASE PROCESS_VALIDATIONS 3
INFO: START PHASE UPDATE_MODEL_VALUES 4
INFO: END PHASE UPDATE_MODEL_VALUES 4
INFO: START PHASE INVOKE_APPLICATION 5
INFO: END PHASE INVOKE_APPLICATION 5
INFO: START PHASE RENDER_RESPONSE 6
INFO: START PHASE RESTORE_VIEW 1
INFO: END PHASE RESTORE_VIEW 1
INFO: START PHASE APPLY_REQUEST_VALUES 2
INFO: END PHASE APPLY_REQUEST_VALUES 2
INFO: START PHASE PROCESS_VALIDATIONS 3
INFO: END PHASE PROCESS_VALIDATIONS 3
INFO: START PHASE UPDATE_MODEL_VALUES 4
INFO: END PHASE UPDATE_MODEL_VALUES 4
INFO: START PHASE INVOKE_APPLICATION 5
INFO: END PHASE INVOKE_APPLICATION 5
INFO: START PHASE RENDER_RESPONSE 6
INFO: END PHASE RENDER_RESPONSE 6
INFO: END PHASE RENDER_RESPONSE 6
INFO: START PHASE RESTORE_VIEW 1
INFO: END PHASE RESTORE_VIEW 1
INFO: START PHASE RENDER_RESPONSE 6
INFO: END PHASE RENDER_RESPONSE 6
INFO: START PHASE RESTORE_VIEW 1
INFO: END PHASE RESTORE_VIEW 1
INFO: START PHASE APPLY_REQUEST_VALUES 2
INFO: END PHASE APPLY_REQUEST_VALUES 2
INFO: START PHASE PROCESS_VALIDATIONS 3
INFO: Getting sendto=<greeting.xhtml>
INFO: END PHASE PROCESS_VALIDATIONS 3
INFO: START PHASE UPDATE_MODEL_VALUES 4
INFO: END PHASE UPDATE_MODEL_VALUES 4
INFO: START PHASE INVOKE_APPLICATION 5
INFO: Session is not null
INFO: login method of the login bean has been called
INFO: login success
INFO: END PHASE INVOKE_APPLICATION 5
INFO: START PHASE RESTORE_VIEW 1
INFO: END PHASE RESTORE_VIEW 1
INFO: START PHASE RENDER_RESPONSE 6
INFO: Constructor called 2 times !
我检查了导入 import javax.faces.bean.SessionScoped;
我已经阅读了许多关于这个问题的答案,但是仍然不确定如何解决这个问题。有任何想法吗 ?
这是主页
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<body>
<ui:composition template="./newTemplate.xhtml">
<ui:define name="title">
Greeting
</ui:define>
<ui:define name="top">
<div class="menu">
<ul id="list-nav">
<li><a href="http://localhost:8080/MaxThePuppyCompany- war/faces/greeting.xhtml">Home</a></li>
<li>
<p:lightBox name="myLightbox" id="myLightbox">
<h:outputLink value="#">
<h:outputText value="Video Content"/>
</h:outputLink>
<f:facet name="inline">
<p:media value="http://vimeo.com/moogaloop.swf?clip_id=18079550" width="400" height="225" player="flash"/>
</f:facet>
</p:lightBox>
</li>
<li>
<p:lightBox widgetVar="myPopup" id="mPopup">
<h:outputLink value="#" onclick="myPopup.show();">
<h:outputText value="Products"/>
</h:outputLink>
<f:facet name="inline">
<ui:include src="/popups/products.xhtml" />
</f:facet>
</p:lightBox>
</li>
<li>
<p:lightBox widgetVar="myLoginPopup" >
<h:outputLink value="#" onclick="loadLogin();">
<h:outputText value="Login"/>
</h:outputLink>
<f:facet name="inline">
<div id="mLoginPopup"></div>
</f:facet>
</p:lightBox>
</li>
<li>
<h:outputText value="#{loginBean.isLoggedIn}"/>
</li>
</ul>
</div>
</ui:define>
<ui:define name="left">
</ui:define>
<ui:define name="right">
right
</ui:define>
<ui:define name="content">
content
</ui:define>
<ui:define name="bottom">
bottom
</ui:define>
</ui:composition>
</body>
</html>
这是 SessionScoped Bean
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import javax.annotation.PostConstruct;
import javax.faces.bean.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@ManagedBean(name = "loginBean")
@SessionScoped
public class LoginBean implements Serializable {
/**
*
*/
private String username;
private String password;
private boolean isLoggedIn = false;
private static int calls;
//@ManagedProperty(value="#{param.from}")
private String sendto;
//private String from;
private int nthOccurrence(String str, char c, int n) {
int pos = str.indexOf(c, 0);
while (n-- > 0 && pos != -1) {
pos = str.indexOf(c, pos+1);
}
return pos;
}
public LoginBean(){
calls++;
System.out.println("Constructor called " + calls + " times !");
//this.sendto = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("from");
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
String tmp = (String) ec.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI);
if(tmp != null) {
int index= this.nthOccurrence(tmp, '/', 1);
this.sendto = tmp.substring(index);
} else {
this.sendto = "greeting.xhtml";
}
System.out.println("In constructor FROM is " + this.sendto);
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
if(session != null){
System.out.println("In Constructor Session is " + session.toString());
//session.invalidate();
}
}
/**
* Check to see if the user has been logged in
* @return
*/
public boolean getIsLoggedIn() {
System.out.println("Get is logged in called and value is " + this.isLoggedIn);
return this.isLoggedIn;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/*
public String getFrom() {
return this.from;
}
public void setFrom (String from) {
this.from = from;
}
*/
public String getSendto() throws UnsupportedEncodingException {
//
//this.sendto = "/admin/aminConsole.xhtml";
System.out.println("Getting sendto=<" + this.sendto + ">");
return this.sendto;
}
public void setSendto(String sendto) throws UnsupportedEncodingException {
//this.sendto = sendto;
}
public String register() {
return "Register";
}
public String login(){