Skip to content

Commit 204d093

Browse files
Merge pull request #1 from matthieuLabaune/develop
Develop
2 parents aabd95c + 7049269 commit 204d093

File tree

8 files changed

+241
-0
lines changed

8 files changed

+241
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.ecars.microcars.controller;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.ecars.microcars.form.CarForm;
7+
import com.ecars.microcars.model.Car;
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.stereotype.Controller;
10+
import org.springframework.ui.Model;
11+
import org.springframework.web.bind.annotation.ModelAttribute;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
15+
@Controller
16+
public class MainController {
17+
18+
private static List<Car> cars = new ArrayList<Car>();
19+
20+
static {
21+
cars.add(new Car ("Honda", "Jazz"));
22+
cars.add(new Car ("Volvo", "940"));
23+
cars.add(new Car( "Opel", "Corsa"));
24+
}
25+
26+
// Injectez (inject) via application.properties.
27+
@Value("${welcome.message}")
28+
private String message;
29+
30+
@Value("${error.message}")
31+
private String errorMessage;
32+
33+
@RequestMapping(value = { "/", "/index" }, method = RequestMethod.GET)
34+
public String index(Model model) {
35+
36+
model.addAttribute("message", message);
37+
38+
return "index";
39+
}
40+
41+
@RequestMapping(value = { "/carList" }, method = RequestMethod.GET)
42+
public String carList(Model model) {
43+
44+
model.addAttribute("cars", cars);
45+
46+
return "carList";
47+
}
48+
49+
@RequestMapping(value = { "/addCar" }, method = RequestMethod.GET)
50+
public String showAddCarPage(Model model) {
51+
52+
CarForm carForm = new CarForm();
53+
model.addAttribute("carForm", carForm);
54+
55+
return "addCar";
56+
}
57+
58+
@RequestMapping(value = { "/addCar" }, method = RequestMethod.POST)
59+
public String saveCar(Model model, //
60+
@ModelAttribute("carForm") CarForm carForm) {
61+
62+
String brand = carForm.getBrand();
63+
String modele = carForm.getModele();
64+
65+
if (brand != null && brand.length() > 0 //
66+
&& modele != null && modele.length() > 0) {
67+
Car newCar = new Car(brand, modele);
68+
cars.add(newCar);
69+
70+
return "redirect:/carList";
71+
}
72+
73+
model.addAttribute("errorMessage", errorMessage);
74+
return "addCar";
75+
}
76+
77+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.ecars.microcars.form;
2+
3+
public class CarForm {
4+
5+
private String brand;
6+
private String modele;
7+
8+
public String getBrand() {
9+
return brand;
10+
}
11+
12+
public void setBrand(String brand) {
13+
this.brand = brand;
14+
}
15+
16+
public String getModele() {
17+
return modele;
18+
}
19+
20+
public void setModele(String modele) {
21+
this.modele = modele;
22+
}
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.ecars.microcars.model;
2+
3+
public class Car {
4+
private String brand;
5+
private String modele;
6+
7+
public Car(String brand, String modele) {
8+
this.brand = brand;
9+
this.modele = modele;
10+
}
11+
12+
public String getBrand() {
13+
return brand;
14+
}
15+
16+
public void setBrand(String brand) {
17+
this.brand = brand;
18+
}
19+
20+
public String getModele() {
21+
return modele;
22+
}
23+
24+
public void setModele(String modele) {
25+
this.modele = modele;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "Car{" +
31+
", brand='" + brand + '\'' +
32+
", modele='" + modele + '\'' +
33+
'}';
34+
}
35+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
server.port=8080
2+
spring.thymeleaf.cache=false
13

4+
welcome.message=Hello Thymeleaf
5+
error.message=Brand & Model is required!
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
h1 {
2+
color: #0000FF;
3+
}
4+
5+
h2 {
6+
color: #FF0000;
7+
}
8+
9+
table {
10+
border-collapse: collapse;
11+
}
12+
13+
table th, table td {
14+
padding: 5px;
15+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE HTML>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Add Car</title>
6+
<link rel="stylesheet" type="text/css" th:href="@{/css/style.css}"/>
7+
</head>
8+
<body>
9+
<h1>Create a Car:</h1>
10+
11+
<!--
12+
In Thymeleaf the equivalent of
13+
JSP's ${pageContext.request.contextPath}/edit.html
14+
would be @{/edit.html}
15+
-->
16+
17+
<form th:action="@{/addCar}"
18+
th:object="${carForm}" method="POST">
19+
Brand:
20+
<input type="text" th:field="*{brand}" />
21+
<br/>
22+
Model:
23+
<input type="text" th:field="*{modele}" />
24+
<br/>
25+
<input type="submit" value="Create" />
26+
</form>
27+
28+
<br/>
29+
30+
<!-- Check if errorMessage is not null and not empty -->
31+
32+
<div th:if="${errorMessage}" th:utext="${errorMessage}"
33+
style="color:red;font-style:italic;">
34+
...
35+
</div>
36+
37+
</body>
38+
</html>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE HTML>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Car List</title>
6+
<link rel="stylesheet" type="text/css" th:href="@{/css/style.css}"/>
7+
</head>
8+
<body>
9+
<h1>Car List</h1>
10+
<a href="addCar">Add Car</a>
11+
<br/><br/>
12+
<div>
13+
<table border="1">
14+
<tr>
15+
<th>Brand</th>
16+
<th>Model</th>
17+
</tr>
18+
<tr th:each="car : ${cars}">
19+
<td th:utext="${car.brand}">...</td>
20+
<td th:utext="${car.modele}">...</td>
21+
</tr>
22+
</table>
23+
</div>
24+
</body>
25+
</html>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE HTML>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Welcome on MicroCars</title>
6+
<link rel="stylesheet" type="text/css" th:href="@{/css/style.css}"/>
7+
</head>
8+
<body>
9+
<h1>Welcome on MicroCars</h1>
10+
<h2 th:utext="${message}">..!..</h2>
11+
12+
<!--
13+
14+
In Thymeleaf the equivalent of
15+
JSP's ${pageContext.request.contextPath}/edit.html
16+
would be @{/edit.html}
17+
18+
-->
19+
20+
<a th:href="@{/carList}">Car List</a>
21+
22+
</body>
23+
24+
</html>

0 commit comments

Comments
 (0)