Controller Layer

In Event Registration System, we have created one Controller class with name EventController by using @Controller Spring Annotations. This class is used for define the API end points to hit the API through the Web Browser. Here every API have different request types like GET, POST. It holds all the business logic and provide the output on the web page by using Thymeleaf Framework.

EventController.java:

Java
package com.event.management.app.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; // Use Controller for Thymeleaf
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.event.management.app.domain.EventRegistration;
import com.event.management.app.service.EventService;

@Controller // Change to Controller for Thymeleaf views
public class EventController {

    @Autowired
    private EventService service;

    @GetMapping("/")
    public String showForm(Model model) {
        // Providing an empty EventRegistration object for form binding
        model.addAttribute("eventRegistration", new EventRegistration());
        return "index";
    }

    @GetMapping("/register")
    public String signupPage() {
        return "signup";
    }

    @GetMapping("/dashboard")
    public String showBookings(Model model) {
        var events = service.getAllEvents();
        model.addAttribute("events", events);
        return "dashboard";
    }

    @PostMapping("/register")
    public String registerEvent(@ModelAttribute EventRegistration eventRegistration, BindingResult bindingResult,
            RedirectAttributes redirectAttributes) {
        if (bindingResult.hasErrors()) {
            redirectAttributes.addFlashAttribute("error", "Failed to register event. Please correct the errors.");
            return "redirect:/register";
        }

        try {
            // Save the registration data
            service.saveEvent(eventRegistration);
            redirectAttributes.addFlashAttribute("success", "Event successfully registered!");
            return "redirect:/register";  // Redirect to clear form data and show flash message
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "An error occurred during registration. Please try again.");
            return "redirect:/register";
        }
    }
}


In the above Controller class we have different API end points for different purpose in our project. Below we provide details information about that APIs.

  • @GetMapping(“/”): This API URL is used for by default display the index page by using Thymeleaf.
  • @GetMapping(“/register”): This API URL is used for display the Event Registration Form to the end user.
  • @GetMapping(“/dashboard”): This API URL is used for display the Dashboard page with available event Registrations details to the end user.
  • @PostMapping(“/register”): This API URL is used for save the event Registration details in the MongoDB by using POST mapping. When data is successfully saved then you got one alert message and also when got error then also you got one alert message for representing the error.

APIs Information

In this application, we use different types APIs with various mappings for different purpose. Below we provide a detailed information for each API with related output for better understanding the concept.

1. @GetMapping(“/”):

This API is used for display the home page by default to the end user where Hit this URL through browser. And Its mapping type is GET Mapping then only user can view the HTML page.

API: http://localhost:8080/

    @GetMapping("/")
public String showForm(Model model) {
// Providing an empty EventRegistration object for form binding
model.addAttribute("eventRegistration", new EventRegistration());
return "index";
}


@GetMapping will display the Index page of the Application as shown below:


2. @GetMapping(“/register”):

This API is used for display the Event Registration page to the end user where Hit this URL through browser. And Its mapping type is GET Mapping then only user can view the HTML page.

http://localhost:8080/register

@GetMapping("/register")
public String signupPage() {
return "signup";
}


This Endpoint will redirect us to the Event Registration Page as shown below:


3. @GetMapping(“/dashboard”):

This API is used for display the Event dashboard page to the end user where Hit this URL through browser. And Its mapping type is GET Mapping then only user can view the HTML page. In this page we can to view all registered event details.

@GetMapping("/dashboard")
public String showBookings(Model model) {
var events = service.getAllEvents();
model.addAttribute("events", events);
return "dashboard";
}


This Endpoint will redirect us to the Event Dashboard page as shown below:


4. @PostMapping(“/register”):

This API is used for save the event details by the help of the Thymeleaf where hit this URL through browser. And Its mapping type is POST Mapping then only data is saved into the MongoDB. And we got alert message for success or even fail. In this API method we call the save method from event repository interface, It can handle the all database operations.

@PostMapping("/register")
public String registerEvent(@ModelAttribute EventRegistration eventRegistration, BindingResult bindingResult,
RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
redirectAttributes.addFlashAttribute("error", "Failed to register event. Please correct the errors.");
return "redirect:/register";
}

try {
// Save the registration data
service.saveEvent(eventRegistration);
redirectAttributes.addFlashAttribute("success", "Event successfully registered!");
return "redirect:/register"; // Redirect to clear form data and show flash message
} catch (Exception e) {
redirectAttributes.addFlashAttribute("error", "An error occurred during registration. Please try again.");
return "redirect:/register";
}
}


Using this API we can register a new event as shown below:




Event Registration System using Spring Boot

Event Registration System plays an important role in the Event Management Business for tracking the Event related details and also we can adjust our time table also according to the events data. In this Article we will explain about the Event Registration System is works with a good example with related output images. First we create a beautiful home page in this page we provide a welcome message and there are two buttons. The Get Started button is used for redirecting the Event Registration page to Book the Slot and other one is Booked Slots button which is used for view the all registered event details.

Here we use Thymeleaf is a java library which is used for handle the render the dynamic data by using the Spring MVC pattern in the this Application. And we gather below event details include ticket and payment details. The Event Registration fields are Event Name, Date and Time, Address, vent Organizer Name, Phone Number, Email, Ticket Type, Total Price, Ticket Price, Quantity, Payment Method.

Ticket & Payment Information:

  • Ticket Type
    • VIP
    • Student
    • General
  • Ticket Price
    • VIP – 500
    • Student – 100
    • General – 300
  • Payment Method
    • Credit Card
    • Debit Card
    • UPI
    • Online Banking

Prerequisites:

  • Spring Boot Framework
  • Thymeleaf
  • MongoDB
  • Spring MVC Pattern
  • Bootstrap Framework
  • Project Type is Gradle
  • Spring Tool Suite IDE (STS)

Gradle Dependencies:

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}


Spring Initializr:

Using Spring Initializr we will will create our project structure.

  • Here we have selected the Project type that is Gradle.
  • Then we filled the Metadata of the Project that are: Group Id, Artifact Id, Name, Description, Java version.
  • Also, we have added here all the required Gradle dependencies which we can see in the build.gradle file.




Project Folder Structure:


Database Connection:

Here we use MongoDB as a database. And below we provide the database connection logic with thymeleaf configuration for handling html files which are located in the template in the resource folder. For the database connection, we need three attribute values that are Host name, Port number of MongoDB.

# database properties
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=eventdb

# thymeleaf configuration
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

Note: Here eventdb is the Database name, 27017 is the port number of MongoDB, and we have used Localhost for running the system.

Similar Reads

Model Layer

Here we created one POJO class for handling database operation with repository interface. The POJO class name is EventRegistration and we used one dependency that is lombok which is available in Spring Boot. This is used for Parameterized and non-Parameterized constructors using @Data, @AllArgsConstructor, @NoArgsConstructor, @Document....

View Layer

In this layer, we have create a view by using HTML, CSS and Bootstrap 5. And we use Thymeleaf Java framework to integrate the both view layer and controller layer with help of the model layer. And The Thymeleaf provide dynamic rendering the application while any change in the controller layer and reflected on the view layer....

Repository

In this application, we have created Mongo Repository interface named EventRepo using @Repository Annotation for Handling CRUD operations in this application. It interacts with Database. This Connection extends to MongoRepository....

Controller Layer

In Event Registration System, we have created one Controller class with name EventController by using @Controller Spring Annotations. This class is used for define the API end points to hit the API through the Web Browser. Here every API have different request types like GET, POST. It holds all the business logic and provide the output on the web page by using Thymeleaf Framework....

Contact Us