Reputation: 425
I am implementing a login form with primefaces but my messages are not displayed, what can be?
I've tried using the id field or passing null in method addMessage ().
my xhtml code:
<?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:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head>
<title>Test</title>
</h:head>
<h:body>
Wellcome<br/>
<h:form id="login">
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />
<p:growl id="msg" showDetail="true" life="3000" />
<p:panel header="Login" style="width: 360px;">
<h:panelGrid id="loginPanel" columns="2">
<h:outputText value="Email:" for="email" />
<p:inputText id="email" value="#{loginBean.email}" ></p:inputText>
<p:spacer></p:spacer>
<p:message for="email" />
<h:outputText value="Senha" for="senha" />
<p:password id="senha" value="#{loginBean.senha}" feedback="false" minLength="1"></p:password>
<p:spacer></p:spacer>
<p:message for="senha" />
<p:spacer></p:spacer>
<p:commandButton action="#{loginBean.loginAction}" value="Login" update="loginForm" ajax="true"></p:commandButton>
</h:panelGrid>
</p:panel>
</h:form>
</h:body>
</html>
my bean:
@ManagedBean
@RequestScoped
public class LoginBean {
private static final long serialVersionUID = 1L;
private String email;
private String senha;
@ManagedProperty(value="#{usuarioSessionBean}")
private UsuarioSessionBean usuarioSessao;
public String loginAction()
{
//Valida preenchimento dos campos de email e senha
boolean campoBranco = false;
if((email == null) || (email.trim().length() == 0))
{
campoBranco = true;
FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR, "Email não informado!","Preencha o email e tente novamente!"));
}
if((senha == null) || (senha.trim().length() == 0))
{
campoBranco = true;
FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR, "Senha não informada!","Preencha a senha e tente novamente!"));
}
if(campoBranco)
return "login";
}
}
Can anyone tell me what is wrong? He returns to the login page but does not show the messages.
Upvotes: 1
Views: 9265
Reputation: 9266
It seems like you're updating the wrong id.
<h:form id="login" >
<p:commandButton value="Login" update="loginForm"
action="#{loginBean.loginAction}"/>
</h:form>
In this case, you need to change loginForm
to login
or change the id
of your form to loginForm
.
Upvotes: 3
Reputation: 1725
If my understanidng is correct you mean to say you want to show the message to the user, try following:
if((senha == null) || (senha.trim().length() == 0))
{
campoBranco = true;
FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR, "Senha não informada!","Preencha a senha e tente novamente!"));
return null;
}
this is what you have to add return null;
so that it will stay in the same page and show up the message, As you are trying to navigate to the login page once agin after getting the error also, it will simply navigate rather than showing error messages.
as well try to modify in the xhtml as suggested below:
and in your xhtml:
<p:growl id="growlLoginValidationStatus" showDetail="true" sticky="false" autoUpdate="true" life="4000" redisplay="false" showSummary="true" globalOnly="false" />
and remove your <p:message/>
tag and make ajax as false in your command button.
try this, if still not able to show the message, let me know in comment
Upvotes: 3