Exp 10-Web Application
Exp 10-Web Application
Step-by-Step Guide
Define your main domain objects. For this example, we will create a simple Student Management
System.
package com.example.demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// Constructors
public Student() {}
package com.example.demo.repository;
import com.example.demo.model.Student;
import org.springframework.data.jpa.repository.JpaRepository;
package com.example.demo.service;
import com.example.demo.model.Student;
import com.example.demo.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
@Autowired
public StudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
java
Copy code
package com.example.demo.controller;
import com.example.demo.model.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/students")
public class StudentController {
@Autowired
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping
public String getAllStudents(Model model) {
List<Student> students = studentService.getAllStudents();
model.addAttribute("students", students);
return "students";
}
@GetMapping("/{id}")
public String getStudentById(@PathVariable Long id, Model model) {
Student student = studentService.getStudentById(id);
model.addAttribute("student", student);
return "student";
}
@GetMapping("/new")
public String showCreateForm(Model model) {
model.addAttribute("student", new Student());
return "create-student";
}
@PostMapping
public String createStudent(@ModelAttribute Student student) {
studentService.createStudent(student);
return "redirect:/students";
}
@GetMapping("/edit/{id}")
public String showEditForm(@PathVariable Long id, Model model) {
Student student = studentService.getStudentById(id);
model.addAttribute("student", student);
return "edit-student";
}
@PostMapping("/{id}")
public String updateStudent(@PathVariable Long id, @ModelAttribute
Student studentDetails) {
studentService.updateStudent(id, studentDetails);
return "redirect:/students";
}
@GetMapping("/delete/{id}")
public String deleteStudent(@PathVariable Long id) {
studentService.deleteStudent(id);
return "redirect:/students";
}
}
Create Thymeleaf templates to render the views. Add these HTML files in the
src/main/resources/templates directory.
students.html:
html
Copy code
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Students</title>
</head>
<body>
<h1>Students</h1>
<a th:href="@{/students/new}">Create New Student</a>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr th:each="student : ${students}">
<td th:text="${student.id}"></td>
<td th:text="${student.name}"></td>
<td th:text="${student.email}"></td>
<td>
<a
th:href="@{/students/edit/{id}(id=${student.id})}">Edit</a>
<a
th:href="@{/students/delete/{id}(id=${student.id})}">Delete</a>
</td>
</tr>
</tbody>
</table>
</body>
</html>
create-student.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Create Student</title>
</head>
<body>
<h1>Create Student</h1>
<form th:action="@{/students}" th:object="${student}" method="post">
<label>Name:</label>
<input type="text" th:field="*{name}" />
<label>Email:</label>
<input type="text" th:field="*{email}" />
<button type="submit">Create</button>
</form>
<a th:href="@{/students}">Back to list</a>
</body>
</html>
edit-student.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Edit Student</title>
</head>
<body>
<h1>Edit Student</h1>
<form th:action="@{/students/{id}(id=${student.id})}"
th:object="${student}" method="post">
<label>Name:</label>
<input type="text" th:field="*{name}" />
<label>Email:</label>
<input type="text" th:field="*{email}" />
<button type="submit">Update</button>
</form>
<a th:href="@{/students}">Back to list</a>
</body>
</html>
Conclusion
This covers creating a basic web application using Spring Boot with an OOP approach in Java. It
includes setting up the project, defining domain models, creating repositories, services, and
controllers, and using Thymeleaf for views. You can expand this application by adding more
features, handling exceptions, implementing validation, and integrating with other services or
databases.