Olá Pessoal, sou novo em JSF e estou com um problema.
Como faço para validar partes separadas usando os validadores do JSF?
Por favor me ajudem…
Olá Pessoal, sou novo em JSF e estou com um problema.
Como faço para validar partes separadas usando os validadores do JSF?
Por favor me ajudem…
como assim separadas ?
O código é este:
<f:view>
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:outputText value="Nome: " />
<h:inputText id="txtNome" required="true" requiredMessage="Obrigatório" value="#{agendaContatoBean.contato.nome}"/>
<h:message for="txtNome" style="color: blue"></h:message>
<br/>
<h:outputText value="Endereço: " />
<h:inputText value="#{agendaContatoBean.contato.endereco}"/>
<br/>
<h:outputText value="Sexo: " />
<h:inputText value="#{agendaContatoBean.contato.sexo}" />
<br/>
<h:commandButton
actionListener="#{agendaContatoBean.incluirContato}"
value="Incluir" />
<h:commandButton
actionListener="#{agendaContatoBean.novoContato}"
value="Novo" />
<br/><br/>
<h:dataTable id="oi" var="obj" value="#{agendaContatoBean.contatos}"
border="1" width="100%">
<h:column>
<h:commandLink value="Editar" actionListener="#{agendaContatoBean.editarContato}">
<f:param value="#{obj}" name="contato" id="contato"></f:param>
</h:commandLink>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Nome"/>
</f:facet>
<h:outputText value="#{obj.nome}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Endereço"/>
</f:facet>
<h:outputText value="#{obj.endereco}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Sexo"/>
</f:facet>
<h:outputText value="#{obj.sexo}" />
</h:column>
</h:dataTable>
</h:form>
</h:body>
</f:view>
ManagedBean
package bean;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
@javax.faces.bean.ManagedBean
@SessionScoped
public final class AgendaContatoBean {
private List<Contato> contatos;
private Contato contato;
public AgendaContatoBean() {
this.setContatos(new ArrayList<Contato>());
this.setContato(new Contato());
}
public void incluirContato(ActionEvent event) {
if (!this.getContatos().contains(this.getContato())) {
this.getContatos().add(this.getContato());
}
}
public void editarContato(ActionEvent event) {
contato = (Contato)((UIParameter)event.getComponent().findComponent("contato")).getValue();
}
public void novoContato(ActionEvent event) {
contato = new Contato();
}
public List<Contato> getContatos() {
return contatos;
}
public void setContatos(List<Contato> contatos) {
this.contatos = contatos;
}
public Contato getContato() {
return contato;
}
public void setContato(Contato contato) {
this.contato = contato;
}
}
Classe contato:
package bean;
public class Contato {
private String nome;
private String endereco;
private String sexo;
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSexo() {
return sexo;
}
public void setSexo(String sexo) {
this.sexo = sexo;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Contato other = (Contato) obj;
if ((this.nome == null) ? (other.nome != null) : !this.nome.equals(other.nome)) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 79 * hash + (this.nome != null ? this.nome.hashCode() : 0);
return hash;
}
}
Quando clico no commandLink “Editar” ele valida o nome, já tentei colocar immediate=“true” mas ele não atualiza os inputText e valida de novo o nome, porém se eu digitar algo no nome, ao clicar em “Editar” ele joga os valores do objeto nos inputText.
Você colocou o immediate = “true” no commandLink de edição e não deu certo?
Sim, na verdade, quando eu coloco o immerdiate=“true” ele valida o nome, se eu digitar algo no nome e clicar em “Editar” de novo aí sim ele atualiza os inpuText com os valores que estão no objeto.
Da uma olhada no código acima que eu acho que você vai entender melhor, eu atualizei ele antes de ver que você tinha respondido.
Obrigado…