Greeting for all,
Please note the XHTML page;
<ui:composition template="/WEB-INF/template/LayoutSystem.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<ui:define name="titulo">Novas Notícias</ui:define>
<ui:define name="corpo">
<h:form>
<h1>Novas Notícias</h1>
<p:messages autoUpdate="true" closable="true" />
<p:toolbar style="margin-top: 20px">
<p:toolbarGroup>
<p:button value="Novo" />
<p:commandButton value="Salvar" id="botaoSalvar"
action="#{cadastroNoticiaBean.salvar}" />
</p:toolbarGroup>
<p:toolbarGroup align="right">
<p:button value="Pesquisa"
outcome="/noticias/PesquisaNoticias.xhtml" />
</p:toolbarGroup>
</p:toolbar>
<div id="wrap">
<div class="left-sidebar">
<p:panelGrid columns="2" id="painel1"
style="width: 50%; margin-top: 20px" columnClasses="rotulo, campo">
<p:outputLabel value="Titulo" for="titulo" />
<p:inputText id="titulo" size="20" maxlength="20"
value="#{cadastroNoticiaBean.noticia.titulo_noticia}" />
<p:outputLabel value="Data" for="data" />
<p:inputText id="data" size="20" maxlength="20" />
<p:outputLabel value="#{cadastroNoticiaBean.noticia.data_noticia}" />
<h:panelGroup>
<p:fileUpload mode="simple" skinSimple="true" />
<p:commandButton value="Submit" ajax="false" disabled="true" />
</h:panelGroup>
<p:outputLabel value="Descrição" for="descricao" />
<p:inputText id="descricao" size="20" maxlength="20"
value="#{cadastroNoticiaBean.noticia.desc_noticia}"/>
</p:panelGrid>
</div>
</div>
</h:form>
</ui:define>
</ui:composition>
I'm building a project in Maven and include the BeanValidation library as you can see;
<!-- Implementacao do Bean Validation -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.1.Final</version>
<scope>compile</scope>
</dependency>
Here's the Bean class.
package br.com.vendelancha.controller;
import java.io.Serializable;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
import br.com.vendelancha.model.Noticia;
@Named
@ViewScoped
public class CadastroNoticiaBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Noticia noticia;
public CadastroNoticiaBean() {
noticia = new Noticia();
}
public void salvar() {
}
public Noticia getNoticia() {
return noticia;
}
}
Attention would be to test the save button to see if the validations are working, but nothing happens, see how my entity is;
package br.com.vendelancha.model;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
@Entity
public class Noticia implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;
private String titulo_noticia;
private Date data_noticia;
private String foto_noticia;
private String desc_noticia;
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@NotNull
@Column(nullable = false, length = 100)
public String getTitulo_noticia() {
return titulo_noticia;
}
public void setTitulo_noticia(String titulo_noticia) {
this.titulo_noticia = titulo_noticia;
}
@NotNull
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
public Date getData_noticia() {
return data_noticia;
}
public void setData_noticia(Date data_noticia) {
this.data_noticia = data_noticia;
}
public String getFoto_noticia() {
return foto_noticia;
}
public void setFoto_noticia(String foto_noticia) {
this.foto_noticia = foto_noticia;
}
@Column(columnDefinition = "text")
public String getDesc_noticia() {
return desc_noticia;
}
public void setDesc_noticia(String desc_noticia) {
this.desc_noticia = desc_noticia;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Noticia other = (Noticia) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}