BAEL-1666 Working with custom attributes in Thymeleaf (#5089)

* BAEL-1666 Working with custom attributes in Thymeleaf

* Delete listCourses.html
This commit is contained in:
josephine-barboza 2018-08-28 01:49:59 +05:30 committed by maibin
parent 710a40be08
commit c209eecd5e
4 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package com.baeldung.thymeleaf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.baeldung.thymeleaf.model.Course;
/**
* Handles requests for the student model.
*
*/
@Controller
public class CourseRegistrationController {
@RequestMapping(value = "/registerCourse", method = RequestMethod.POST)
public String register(@ModelAttribute Course course, Model model) {
model.addAttribute("successMessage", "You have successfully registered for course: " + course.getName() + ".");
return "courseRegistration.html";
}
@RequestMapping(value = "/registerCourse", method = RequestMethod.GET)
public String register(Model model) {
model.addAttribute("course", new Course());
return "courseRegistration.html";
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.thymeleaf.model;
import java.util.Date;
public class Course {
private String name;
private String description;
private Date startDate;
private Date endDate;
private String teacher;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
}

View File

@ -0,0 +1,42 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Register for course</title>
<script>
function validateForm() {
var e = document.getElementById("course");
var value = e.options[e.selectedIndex].value;
if (value == '') {
var error = document.getElementById("errormesg");
error.textContent = e.getAttribute('data-validation-message');
return false;
}
return true;
}
</script>
</head>
<body>
<h3>Register Here</h3>
<form action="#" th:action="@{/registerCourse}" th:object="${course}"
method="post" onsubmit="return validateForm();">
<span id="errormesg" style="color: red"></span> <span
style="font-weight: bold" th:text="${successMessage}"></span>
<table>
<tr>
<td><label th:text="#{msg.courseName}+': '"></label></td>
<td><select id="course" th:field="*{name}"
th:data-validation-message="#{msg.courseName.mandatory}">
<option th:value="''" th:text="'Select'"></option>
<option th:value="'Mathematics'" th:text="'Mathematics'"></option>
<option th:value="'History'" th:text="'History'"></option>
</select></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>

View File

@ -24,6 +24,9 @@
<tr> <tr>
<td><a th:href="@{/listBooks}" th:text="#{msg.ListBooks}" /></td> <td><a th:href="@{/listBooks}" th:text="#{msg.ListBooks}" /></td>
</tr> </tr>
<tr>
<td><a th:href="@{/registerCourse}" th:text="#{msg.CourseRegistration}" /></td>
</tr>
</table> </table>
</body> </body>
</html> </html>