Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0f82358

Browse files
Aula 08 - Formatação de Datas
1 parent bdeb73b commit 0f82358

File tree

9 files changed

+116
-36
lines changed

9 files changed

+116
-36
lines changed

‎casadocodigo/.idea/artifacts/casadocodigo_war_exploded.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎casadocodigo/.idea/libraries/Maven__javax_validation_validation_api_1_0_0_GA.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎casadocodigo/.idea/libraries/Maven__org_hibernate_hibernate_validator_4_2_0_Final.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎casadocodigo/.idea/workspace.xml

Lines changed: 44 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎casadocodigo/casadocodigo.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,7 @@
6262
<orderEntry type="library" name="Maven: org.springframework:spring-tx:4.1.0.RELEASE" level="project" />
6363
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.15" level="project" />
6464
<orderEntry type="library" name="Maven: org.postgresql:postgresql:42.2.1" level="project" />
65+
<orderEntry type="library" name="Maven: javax.validation:validation-api:1.0.0.GA" level="project" />
66+
<orderEntry type="library" name="Maven: org.hibernate:hibernate-validator:4.2.0.Final" level="project" />
6567
</component>
6668
</module>

‎casadocodigo/src/main/java/br/com/casadocodigo/loja/conf/AppWebConfiguration.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import org.springframework.context.annotation.Bean;
55
import org.springframework.context.annotation.ComponentScan;
66
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
7+
import org.springframework.format.datetime.DateFormatter;
8+
import org.springframework.format.datetime.DateFormatterRegistrar;
9+
import org.springframework.format.support.DefaultFormattingConversionService;
10+
import org.springframework.format.support.FormattingConversionService;
711
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
812
import org.springframework.web.servlet.view.InternalResourceViewResolver;
913

@@ -19,7 +23,6 @@ public InternalResourceViewResolver internalResourceViewResolver() {
1923
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
2024
resolver.setPrefix("/WEB-INF/views/");
2125
resolver.setSuffix(".jsp");
22-
2326
return resolver;
2427
}
2528

@@ -32,4 +35,14 @@ public MessageSource messageSource() {
3235
messageSource.setCacheSeconds(1);
3336
return messageSource();
3437
}
38+
39+
@Bean
40+
public FormattingConversionService mvcConversionService() {
41+
DefaultFormattingConversionService conversionService =
42+
new DefaultFormattingConversionService();
43+
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
44+
registrar.setFormatter(new DateFormatter("dd/MM/yyyy"));
45+
registrar.registerFormatters(conversionService);
46+
return conversionService;
47+
}
3548
}

‎casadocodigo/src/main/java/br/com/casadocodigo/loja/controllers/ProdutosController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public void initBinder(WebDataBinder binder) {
3131
binder.addValidators(new ProdutoValidation());
3232
}
3333

34-
@RequestMapping("/form")
35-
public ModelAndView form() {
34+
@RequestMapping("form")
35+
public ModelAndView form(Produtoproduto) {
3636
ModelAndView modelAndView = new ModelAndView("produtos/form");
3737
modelAndView.addObject("tipos", TipoPreco.values());
3838

@@ -43,7 +43,7 @@ public ModelAndView form() {
4343
public ModelAndView gravar(@Valid Produto produto, BindingResult result, RedirectAttributes redirectAttributes) {
4444

4545
if (result.hasErrors()) {
46-
return form();
46+
return form(produto);
4747
}
4848

4949
produtoDAO.gravar(produto);

‎casadocodigo/src/main/java/br/com/casadocodigo/loja/models/Produto.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package br.com.casadocodigo.loja.models;
22

3+
import java.util.Calendar;
34
import java.util.List;
45

56
import javax.persistence.ElementCollection;
@@ -8,6 +9,8 @@
89
import javax.persistence.GenerationType;
910
import javax.persistence.Id;
1011

12+
import org.springframework.format.annotation.DateTimeFormat;
13+
1114
@Entity
1215
public class Produto {
1316

@@ -19,10 +22,12 @@ public class Produto {
1922
private String descricao;
2023
private int paginas;
2124

25+
@DateTimeFormat
26+
private Calendar dataLancamento;
27+
2228
@ElementCollection
2329
private List<Preco> precos;
2430

25-
2631
public String getTitulo() {
2732
return titulo;
2833
}
@@ -54,6 +59,15 @@ public List<Preco> getPrecos() {
5459
public void setPrecos(List<Preco> precos) {
5560
this.precos = precos;
5661
}
62+
63+
public Calendar getDataLancamento() {
64+
return dataLancamento;
65+
}
66+
67+
public void setDataLancamento(Calendar dataLancamento) {
68+
this.dataLancamento = dataLancamento;
69+
}
70+
5771
@Override
5872
public String toString() {
5973
return "Produto [titulo=" + titulo + ",descricao=" + descricao + ",paginas="

‎casadocodigo/src/main/webapp/WEB-INF/views/produtos/form.jsp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,29 @@
1414
<form:form action="${s:mvcUrl('PC#gravar').build()}" method="post" commandName="produto">
1515
<div>
1616
<label>Titulo</label>
17-
<input type="text"name="titulo">
17+
<form:input path="titulo"/>
1818
<form:errors path="titulo"/>
1919
</div>
2020
<div>
2121
<label>Descrição</label>
22-
<textarea rows="10" cols="20"name="descricao"></textarea>
22+
<form:textarea path="descricao"rows="10" cols="20"/>
2323
<form:errors path="descricao"/>
2424
</div>
2525
<div>
2626
<label>Páginas</label>
27-
<input type="text"name="paginas">
27+
<form:input path="paginas"/>
2828
<form:errors path="paginas"/>
2929
</div>
30+
<div>
31+
<label>Data de Lançamento</label>
32+
<form:input path="dataLancamento"/>
33+
<form:errors path="dataLancamento"/>
34+
</div>
3035
<c:forEach items="${tipos}" var="tipoPreco" varStatus="status">
3136
<div>
3237
<label>${tipoPreco}</label>
33-
<input type="text"name="precos[${status.index}].valor">
34-
<inputtype="hidden"name="precos[${status.index}].tipo" value="${tipoPreco}">
38+
<form:input path="precos[${status.index}].valor"/>
39+
<form:hiddenpath="precos[${status.index}].tipo" value="${tipoPreco}"/>
3540
</div>
3641
</c:forEach>
3742
<button type="submit">Cadastrar</button>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /