[英]primefaces message not working
我正在實現一個帶有撇號的登錄表單,但是我的消息沒有顯示,怎么辦?
我嘗試使用id字段或在方法addMessage()中傳遞null。
我的xhtml代碼:
<?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>
我的豆子:
@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";
}
}
誰能告訴我這是怎么回事? 他返回登錄頁面,但不顯示消息。
如果我的理解是正確的,則意味着要向用戶顯示消息,請嘗試以下操作:
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;
}
這就是您必須添加的內容return null;
這樣一來,它就會停留在同一頁面上並顯示消息。當您嘗試再次獲得錯誤后再次嘗試登錄到登錄頁面時,它只會進行導航而不是顯示錯誤消息。 並嘗試按照以下建議在xhtml中進行修改:
並在您的xhtml中:
<p:growl id="growlLoginValidationStatus" showDetail="true" sticky="false" autoUpdate="true" life="4000" redisplay="false" showSummary="true" globalOnly="false" />
並刪除您的<p:message/>
標記,並在命令按鈕中將ajax設置為false。 試試這個,如果仍然無法顯示消息,請在評論中讓我知道
好像您要更新錯誤的ID。
<h:form id="login" >
<p:commandButton value="Login" update="loginForm"
action="#{loginBean.loginAction}"/>
</h:form>
在這種情況下,您需要將loginForm
更改為login
或將表單的id
更改為loginForm
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.