0% found this document useful (0 votes)
18 views

Exp 10-Web Application

Lab exp.

Uploaded by

khushi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Exp 10-Web Application

Lab exp.

Uploaded by

khushi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

EXPERIMENT-10

OBJECTIVE: Building web application using Spring Boot in an Object-Oriented Programming


(OOP) approach with Java.

Step-by-Step Guide

1. Set Up Your Project

1. Create a New Spring Boot Project: Use Spring Initializr (https://start.spring.io/) to


create a new Spring Boot project with the following dependencies:
o Spring Web
o Spring Data JPA
o Thymeleaf (for front-end)
o H2 Database (for an in-memory database)
2. Import the Project into Your IDE: Open your IDE (IntelliJ IDEA, Eclipse, etc.) and
import the project.

2. Define Your Domain Model

Define your main domain objects. For this example, we will create a simple Student Management
System.

Create the Student Class:

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() {}

public Student(String name, String email) {


this.name = name;
this.email = email;
}
// Getters and Setters
public Long getId() {
return id;
}

public void setId(Long id) {


this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getEmail() {


return email;
}

public void setEmail(String email) {


this.email = email;
}
}

3. Create the Repository

Create a repository interface to interact with the database.

Create StudentRepository Interface:

package com.example.demo.repository;

import com.example.demo.model.Student;
import org.springframework.data.jpa.repository.JpaRepository;

public interface StudentRepository extends JpaRepository<Student, Long> {


}

4. Create Service Layer

The service layer contains the business logic of your application.

Create StudentService Class:

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 {

private final StudentRepository studentRepository;

@Autowired
public StudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}

public List<Student> getAllStudents() {


return studentRepository.findAll();
}

public Student getStudentById(Long id) {


return studentRepository.findById(id).orElse(null);
}

public Student createStudent(Student student) {


return studentRepository.save(student);
}

public Student updateStudent(Long id, Student studentDetails) {


Student student = studentRepository.findById(id).orElse(null);
if (student != null) {
student.setName(studentDetails.getName());
student.setEmail(studentDetails.getEmail());
return studentRepository.save(student);
}
return null;
}

public void deleteStudent(Long id) {


studentRepository.deleteById(id);
}
}

5. Create Controller Layer

The controller layer handles HTTP requests and responses.

Create StudentController Class:

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 {

private final StudentService studentService;

@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";
}
}

6. Create Thymeleaf Templates

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>

7. Run the Application

1. Run the Spring Boot Application:


o Use your IDE to run the main method in the DemoApplication class (or similar
class generated by Spring Initializr).
2. Access the Application:
o Open your browser and go to http://localhost:8080/students to see the list
of students and interact with your web application.

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.

You might also like