gpt4 book ai didi

java - primefaces 消息不起作用

转载 作者:行者123 更新时间:2023-11-29 07:09:00 26 4
gpt4 key购买 nike

我正在使用 primefaces 实现一个登录表单,但我的消息没有显示,这是怎么回事?

我尝试过使用 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>

我的 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";
}
}

谁能告诉我哪里出了问题?他返回到登录页面,但没有显示消息。

最佳答案

看来您正在更新错误的 ID。

<h:form id="login" >
<p:commandButton value="Login" update="loginForm"
action="#{loginBean.loginAction}"/>
</h:form>

在这种情况下,您需要将 loginForm 更改为 login 或将表单的 id 更改为 loginForm.

关于java - primefaces 消息不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16243699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com