Skip to content

Commit c209eec

Browse files
josephine-barbozamaibin
authored andcommitted
BAEL-1666 Working with custom attributes in Thymeleaf (eugenp#5089)
* BAEL-1666 Working with custom attributes in Thymeleaf * Delete listCourses.html
1 parent 710a40b commit c209eec

4 files changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.thymeleaf.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.web.bind.annotation.ModelAttribute;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
9+
import com.baeldung.thymeleaf.model.Course;
10+
11+
/**
12+
* Handles requests for the student model.
13+
*
14+
*/
15+
@Controller
16+
public class CourseRegistrationController {
17+
18+
@RequestMapping(value = "/registerCourse", method = RequestMethod.POST)
19+
public String register(@ModelAttribute Course course, Model model) {
20+
model.addAttribute("successMessage", "You have successfully registered for course: " + course.getName() + ".");
21+
return "courseRegistration.html";
22+
}
23+
24+
@RequestMapping(value = "/registerCourse", method = RequestMethod.GET)
25+
public String register(Model model) {
26+
model.addAttribute("course", new Course());
27+
return "courseRegistration.html";
28+
}
29+
30+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.thymeleaf.model;
2+
3+
import java.util.Date;
4+
5+
public class Course {
6+
7+
private String name;
8+
private String description;
9+
private Date startDate;
10+
private Date endDate;
11+
private String teacher;
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
public String getDescription() {
22+
return description;
23+
}
24+
25+
public void setDescription(String description) {
26+
this.description = description;
27+
}
28+
29+
public Date getStartDate() {
30+
return startDate;
31+
}
32+
33+
public void setStartDate(Date startDate) {
34+
this.startDate = startDate;
35+
}
36+
37+
public Date getEndDate() {
38+
return endDate;
39+
}
40+
41+
public void setEndDate(Date endDate) {
42+
this.endDate = endDate;
43+
}
44+
45+
public String getTeacher() {
46+
return teacher;
47+
}
48+
49+
public void setTeacher(String teacher) {
50+
this.teacher = teacher;
51+
}
52+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:th="http://www.thymeleaf.org">
4+
<head>
5+
<title>Register for course</title>
6+
<script>
7+
function validateForm() {
8+
var e = document.getElementById("course");
9+
var value = e.options[e.selectedIndex].value;
10+
if (value == '') {
11+
var error = document.getElementById("errormesg");
12+
error.textContent = e.getAttribute('data-validation-message');
13+
return false;
14+
}
15+
return true;
16+
}
17+
</script>
18+
</head>
19+
<body>
20+
21+
<h3>Register Here</h3>
22+
<form action="#" th:action="@{/registerCourse}" th:object="${course}"
23+
method="post" onsubmit="return validateForm();">
24+
<span id="errormesg" style="color: red"></span> <span
25+
style="font-weight: bold" th:text="${successMessage}"></span>
26+
<table>
27+
<tr>
28+
<td><label th:text="#{msg.courseName}+': '"></label></td>
29+
<td><select id="course" th:field="*{name}"
30+
th:data-validation-message="#{msg.courseName.mandatory}">
31+
<option th:value="''" th:text="'Select'"></option>
32+
<option th:value="'Mathematics'" th:text="'Mathematics'"></option>
33+
<option th:value="'History'" th:text="'History'"></option>
34+
</select></td>
35+
</tr>
36+
<tr>
37+
<td><input type="submit" value="Submit" /></td>
38+
</tr>
39+
</table>
40+
</form>
41+
</body>
42+
</html>

‎spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ <h1>
2424
<tr>
2525
<td><a th:href="@{/listBooks}" th:text="#{msg.ListBooks}" /></td>
2626
</tr>
27+
<tr>
28+
<td><a th:href="@{/registerCourse}" th:text="#{msg.CourseRegistration}" /></td>
29+
</tr>
2730
</table>
2831
</body>
2932
</html>

0 commit comments

Comments
 (0)