0
Greeting to all,
I created an application that is saving the records in the database, as shown in the figure;
This application is succeeding in saving the title, date and description, and is managing to upload the image to the folder that determined, but I want to save the folder path in the database, as I do to do this?
Here is the entity Noticia;
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;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotBlank;
@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;
}
@NotBlank
@Size(max = 80)
@Column(nullable = false, length = 50)
public String getTitulo_noticia() {
return titulo_noticia;
}
public void setTitulo_noticia(String titulo_noticia) {
this.titulo_noticia = titulo_noticia;
}
@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;
}
@NotBlank
@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;
}
}
Here is the XHTML page;
<ui:composition template="/WEB-INF/template/LayoutSystem.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<!-- xmlns:h="http://java.sun.com/jsf/html" -->
<ui:define name="titulo">Novas Notícias</ui:define>
<ui:define name="corpo">
<h:form id="form" enctype="multipart/form-data">
<h1>Novas Notícias</h1>
<p:messages autoUpdate="true" closable="true" />
<p:toolbar style="margin-top: 20px">
<p:toolbarGroup>
<p:button value="Novo" />
<h:commandButton value="Salvar" id="botaoSalvar"
action="#{cadastroNoticiaBean.salvar}">
<f:ajax execute="@form" render="@all"/>
</h:commandButton>
</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="50"
value="#{cadastroNoticiaBean.noticia.titulo_noticia}" />
<p:outputLabel value="Data" for="data_noticia" />
<p:calendar id="data_noticia" pattern="dd/MM/yyyy"
value="#{cadastroNoticiaBean.noticia.data_noticia}" />
<p:outputLabel value="Descrição" for="descricao" />
<p:inputText id="descricao" size="20" maxlength="250"
value="#{cadastroNoticiaBean.noticia.desc_noticia}" />
<p:outputLabel value="Foto" />
<h:inputFile value="#{cadastroNoticiaBean.arquivo}" />
</p:panelGrid>
</div>
</div>
</h:form>
</ui:define>
</ui:composition>
Here’s my Bean class;
package br.com.vendelancha.controller;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.Part;
import br.com.vendelancha.model.Noticia;
import br.com.vendelancha.service.CadastroNoticiaService;
import br.com.vendelancha.util.jsf.FacesUtil;
@Named
@ViewScoped
public class CadastroNoticiaBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Noticia noticia;
private Part arquivo;
private String nomeArquivoSaida;
@Inject
private CadastroNoticiaService cadastroNoticiaService;
public CadastroNoticiaBean() {
limpar();
}
public void limpar(){
noticia = new Noticia();
}
public void salvar() {
this.noticia = cadastroNoticiaService.salvar(this.noticia);
upload();
limpar();
FacesUtil.addInfoMessage("Noticia salva com sucesso! ");
}
public void upload() {
nomeArquivoSaida = "C:/workspace Web/Projetos Profissionais/Fotos para teste/" + arquivo.getSubmittedFileName();
try (InputStream is = arquivo.getInputStream();
OutputStream out = new FileOutputStream(nomeArquivoSaida)) {
int read = 0;
byte[] bytes = new byte[1024];
while ((read = is.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
} catch (IOException e) {
FacesUtil.addErrorMessage("Erro ao enviar arquivo.");
}
}
public Noticia getNoticia() {
return noticia;
}
public Part getArquivo() {
return arquivo;
}
public void setArquivo(Part arquivo) {
this.arquivo = arquivo;
}
public String getNomeArquivoSaida() {
return nomeArquivoSaida;
}
public void setNomeArquivoSaida(String nomeArquivoSaida) {
this.nomeArquivoSaida = nomeArquivoSaida;
}
}
According to your case I assume you will need to access these photos in your application, correct? Then I have a question: you are saving the images inside a folder in your application?
– user6476
I’m still not saving the folder within my application, but this is no problem and only change the path, the path is described in the post. I will need to access these photos.
– wladyband