Created
April 11, 2019 12:50
-
-
Save Pietrz/cfb0c2daa2af8aae776871978f49cf9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.casadeleilao.loja.conf; | |
import org.springframework.context.MessageSource; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.support.ReloadableResourceBundleMessageSource; | |
import org.springframework.format.datetime.DateFormatter; | |
import org.springframework.format.datetime.DateFormatterRegistrar; | |
import org.springframework.format.support.DefaultFormattingConversionService; | |
import org.springframework.format.support.FormattingConversionService; | |
import org.springframework.web.multipart.MultipartResolver; | |
import org.springframework.web.multipart.support.StandardServletMultipartResolver; | |
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | |
import org.springframework.web.servlet.view.InternalResourceViewResolver; | |
import br.com.casadeleilao.loja.controllers.HomeController; | |
import br.com.casadeleilao.loja.dao.ProdutoDao; | |
import br.com.casadeleilao.loja.infra.FileSaver; | |
@EnableWebMvc | |
@ComponentScan(basePackageClasses = { HomeController.class, ProdutoDao.class, FileSaver.class }) | |
public class AppWebConfiguration { | |
@Bean | |
public InternalResourceViewResolver internalResourceViewResolver() { | |
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); | |
resolver.setPrefix("/WEB-INF/views/"); | |
resolver.setSuffix(".jsp"); | |
return resolver; | |
} | |
@Bean | |
public MessageSource messageSource() { | |
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); | |
messageSource.setBasename("/WEB-INF/messages"); | |
messageSource.setDefaultEncoding("UTF-8"); | |
messageSource.setCacheSeconds(1); | |
return messageSource; | |
} | |
@Bean | |
public FormattingConversionService mvcConversionService() { | |
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(); | |
DateFormatterRegistrar formatterRegistrar = new DateFormatterRegistrar(); | |
formatterRegistrar.setFormatter(new DateFormatter("dd/MM/yyyy")); | |
formatterRegistrar.registerFormatters(conversionService); | |
return conversionService; | |
} | |
@Bean | |
public MultipartResolver multipartResolver() { | |
return new StandardServletMultipartResolver(); | |
} | |
} | |
package br.com.casadeleilao.loja.conf; | |
import javax.servlet.Filter; | |
import javax.servlet.MultipartConfigElement; | |
import javax.servlet.ServletRegistration.Dynamic; | |
import org.springframework.web.filter.CharacterEncodingFilter; | |
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; | |
public class ServeletSpringMvc extends AbstractAnnotationConfigDispatcherServletInitializer { | |
@Override | |
protected Class<?>[] getRootConfigClasses() { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
@Override | |
protected Class<?>[] getServletConfigClasses() { | |
// TODO Auto-generated method stub | |
return new Class[] { AppWebConfiguration.class, JpaConfiguration.class }; | |
} | |
@Override | |
protected String[] getServletMappings() { | |
// TODO Auto-generated method stub | |
return new String[] { "/" }; | |
} | |
@Override | |
protected Filter[] getServletFilters() { | |
CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter(); | |
encodingFilter.setEncoding("UTF-8"); | |
return super.getServletFilters(); | |
} | |
@Override | |
protected void customizeRegistration(Dynamic registration) { | |
// TODO Auto-generated method stub | |
registration.setMultipartConfig(new MultipartConfigElement("")); | |
} | |
} | |
package br.com.casadeleilao.loja.controllers; | |
import java.util.List; | |
import javax.validation.Valid; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.validation.BindingResult; | |
import org.springframework.web.bind.WebDataBinder; | |
import org.springframework.web.bind.annotation.InitBinder; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.multipart.MultipartFile; | |
import org.springframework.web.servlet.ModelAndView; | |
import org.springframework.web.servlet.mvc.support.RedirectAttributes; | |
import br.com.casadeleilao.loja.dao.ProdutoDao; | |
import br.com.casadeleilao.loja.infra.FileSaver; | |
import br.com.casadeleilao.loja.model.Produto; | |
import br.com.casadeleilao.loja.validation.ProdutoValidation; | |
@RequestMapping("/produtos") | |
@Controller | |
public class ProdutosController { | |
@Autowired | |
private ProdutoDao dao; | |
@Autowired | |
private FileSaver fileSaver; | |
@InitBinder | |
public void initBinder(WebDataBinder binder) { | |
binder.addValidators(new ProdutoValidation()); | |
} | |
@RequestMapping("form") | |
public ModelAndView form() { | |
return new ModelAndView("/produtos/form"); | |
} | |
@RequestMapping(method = RequestMethod.POST) | |
public ModelAndView gravar(MultipartFile capaDoProduto, @Valid Produto produto, BindingResult bindingResult, | |
RedirectAttributes redirectAttributes) { | |
if (bindingResult.hasErrors()) { | |
return form(); | |
} | |
String path = fileSaver.write("arquivos", capaDoProduto); | |
produto.setImagemDoProduto(path); | |
dao.gravar(produto); | |
ModelAndView modelAndView = new ModelAndView("redirect:produtos"); | |
redirectAttributes.addFlashAttribute("sucesso", "Produto cadastrado com sucesso"); | |
return modelAndView; | |
} | |
@RequestMapping(method = RequestMethod.GET) | |
public ModelAndView listar() { | |
List<Produto> produtos = dao.listar(); | |
ModelAndView modelAndView = new ModelAndView("produtos/lista"); | |
modelAndView.addObject("produtos", produtos); | |
return modelAndView; | |
} | |
} | |
package br.com.casadeleilao.loja.model; | |
import java.math.BigDecimal; | |
import java.util.Calendar; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import org.springframework.format.annotation.DateTimeFormat; | |
@Entity | |
public class Produto { | |
private String nome; | |
private String descricao; | |
private BigDecimal preco; | |
private String imagemDoProduto; | |
@DateTimeFormat | |
private Calendar dataDeInicio; | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private int id; | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public String getDescricao() { | |
return descricao; | |
} | |
public void setDescricao(String descricao) { | |
this.descricao = descricao; | |
} | |
public BigDecimal getPreco() { | |
return preco; | |
} | |
public void setPreco(BigDecimal preco) { | |
this.preco = preco; | |
} | |
public Calendar getDataDeInicio() { | |
return dataDeInicio; | |
} | |
public void setDataDeInicio(Calendar dataDeInicio) { | |
this.dataDeInicio = dataDeInicio; | |
} | |
public String getImagemDoProduto() { | |
return imagemDoProduto; | |
} | |
public void setImagemDoProduto(String imagemDoProduto) { | |
this.imagemDoProduto = imagemDoProduto; | |
} | |
} | |
<%@ page language="java" contentType="text/html; charset=UTF-8" | |
pageEncoding="UTF-8"%> | |
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> | |
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> | |
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Casa de leilão</title> | |
</head> | |
<body> | |
<form:form action="${s:mvcUrl('PC#gravar').build() }" method="post" commandName="produto" enctype="multipart/form-data"> | |
<td> | |
<div> | |
<label>Produto</label> | |
<input type="text" name="nome"> | |
<form:errors path="nome"/> | |
</div> | |
<div> | |
<label>Descrição</label> | |
<form:errors path="descricao"/> | |
<textarea rows="10" cols="20" name="descricao"></textarea> | |
</div> | |
<div> | |
<label>Preço</label> | |
<form:errors path="preco"/> | |
<input type="text" name="preco"> | |
</div> | |
<div> | |
<label>Data de Inicio</label> | |
<form:errors path="dataDeInicio"/> | |
<input type="text" name="dataDeInicio"> | |
</div> | |
<div> | |
<label>Imagem do Produto</label> | |
<form:errors path="imagemDoProduto"/> | |
<input type="file" name="imagemDoProduto"> | |
</div> | |
</td> | |
<div class="button"> | |
<button type="submit">Cadastrar</button> | |
</div> | |
</form:form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment