I have a JSF login page (for Spring Security) and a global message to display a message for wrong login user/pass.
Here is my form:
<f:event listener="#{loginBean.updateMessages}" type="preRenderView"/>
<h:form prependId="false" >
<h:outputLabel value="User Name:" for="username"/>
<h:inputText id="username" required="true" value="#{loginBean.name}"/>
<h:message id="usernMsg" for="username"/> <br/>
<h:outputLabel value="Password:" for="password"/>
<h:inputSecret id="password" value="#{loginBean.password}" required="true"/>
<h:message id="passMsg" for="password"/><br/>
<h:messages id="glbMsg" globalOnly="true"/><br/>
<h:commandButton value="Submit" action="#{loginBean.doLogin}"/>
</h:form>
I update messages with updateMessages()
:
public void updateMessages() {
Exception ex = (Exception) FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
.get(WebAttributes.AUTHENTICATION_EXCEPTION);
if (ex != null) {
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
setUsername("");
setPassword("");
}
}
Problem is when user enter wrong credentials, the message displays, but when user refresh the login page (either with F5
or clicking on submit button while text fields are empty ) , the previous global message (glbMsg
) value doesn't removes.
I tried ajax render="..."
in submit button and it not worked.