JSF ajax listener -
i'm trying figure out how log out user f:ajax
call using jsf , backing managed bean. problem i'm having can't quite figure out why call order ajax listener , rerender of login form.
below simplified code. basic idea of code this
if (uid != null) { // show log out } else { // show log in }
i not understanding how ajax listeners , form rerender done.
the jsf page
<html xmlns="http://www.w3.org/1999/xhtml" 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"> <h:head> <title>facelet title</title> </h:head> <h:body> <f:view> <h:form id="loginform"> <c:choose> <c:when test="${userbean.uid != null}"> <span>hi, #{userbean.uid}</span> <h:commandbutton value="logout"> <f:ajax event="click" listener="#{userbean.logout}" render="loginform"/> </h:commandbutton> </c:when> <c:otherwise> <span>user name: </span> <h:inputtext value="#{userbean.uid}" id="uid" /> <h:commandbutton value="login" action="#{userbean.login}" /> </c:otherwise> </c:choose> </h:form> </f:view> </h:body> </html>
the bean
package test.auth; import java.io.serializable; import javax.inject.named; import javax.enterprise.context.sessionscoped; import javax.faces.event.ajaxbehaviorevent; @named(value="userbean") @sessionscoped public class userbean implements serializable { private static final long serialversionuid = -4292770567982457272l; private string uid; /** creates new instance of userbean */ public userbean() { } public string getuid() { return uid; } public void setuid(string uid) { this.uid = uid; } public string login () { return "faces/index.xhtml"; } public void logout (ajaxbehaviorevent event) { this.uid = null; } }
the problem code when clicking logout
form reloads still in logged in state though uid
has been set null
. i've checked debugger. how make render executed after ajax listener?
the jstl tags executed during view build time, not during view render time. @ moment jsf reusing view state re-render, jstl tags won't re-executed, because not there anymore.
you want use jsf rendered
attribute instead.
<h:form id="loginform"> <h:panelgroup rendered="#{userbean.uid != null}"> <span>hi, #{userbean.uid}</span> <h:commandbutton value="logout"> <f:ajax event="click" listener="#{userbean.logout}" render="loginform"/> </h:commandbutton> </h:panelgroup> <h:panelgroup rendered="#{userbean.uid == null}"> <span>user name: </span> <h:inputtext value="#{userbean.uid}" id="uid" /> <h:commandbutton value="login" action="#{userbean.login}" /> </h:panelgroup> </h:form>
Comments
Post a Comment